001: /**
002: *
003: */package org.sakaiproject.search.tool;
004:
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.io.OutputStream;
010:
011: import javax.servlet.ServletContext;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014:
015: import org.sakaiproject.component.cover.ServerConfigurationService;
016: import org.sakaiproject.exception.IdUnusedException;
017: import org.sakaiproject.search.api.SearchService;
018: import org.sakaiproject.search.tool.api.SherlockSearchBean;
019: import org.sakaiproject.site.api.Site;
020: import org.sakaiproject.site.api.SiteService;
021: import org.sakaiproject.tool.api.Placement;
022: import org.sakaiproject.tool.api.ToolManager;
023: import org.sakaiproject.util.FormattedText;
024:
025: /**
026: * @author ieb
027: */
028: public class SherlockSearchBeanImpl implements SherlockSearchBean {
029:
030: private static final String IMAGE_ICON = "/images/sherlock.gif";
031:
032: static final String UPDATE_URL = "/sakai.src";
033:
034: static final String UPDATE_IMAGE = "/sakai.gif";
035:
036: private HttpServletRequest request;
037:
038: private SearchService searchService;
039:
040: private SiteService siteService;
041:
042: private Placement placement;
043:
044: private String placementId;
045:
046: private String toolId;
047:
048: private String siteId;
049:
050: private Site currentSite;
051:
052: private String baseURL;
053:
054: private String portalBaseURL;
055:
056: private ServletContext context;
057:
058: public SherlockSearchBeanImpl(HttpServletRequest request,
059: ServletContext context, SearchService searchService,
060: SiteService siteService, ToolManager toolManager)
061: throws IdUnusedException {
062: this .context = context;
063: this .request = request;
064: this .searchService = searchService;
065: this .siteService = siteService;
066: this .placement = toolManager.getCurrentPlacement();
067: this .placementId = toolManager.getCurrentPlacement().getId();
068: this .toolId = toolManager.getCurrentTool().getId();
069: this .siteId = toolManager.getCurrentPlacement().getContext();
070: this .currentSite = this .siteService.getSite(this .siteId);
071: String siteCheck = currentSite.getReference();
072: baseURL = getBaseURL();
073: portalBaseURL = getPortalBaseURL();
074: }
075:
076: private String getBaseURL() {
077: return ServerConfigurationService.getPortalUrl() + "/tool/"
078: + placementId;
079: }
080:
081: private String getPortalBaseURL() {
082: return ServerConfigurationService.getPortalUrl()
083: + "/directtool/" + placementId;
084: }
085:
086: public String getSearchURL() {
087: return portalBaseURL + "/index";
088: }
089:
090: public String getSiteName() {
091: return FormattedText.escapeHtml(currentSite.getTitle(), false);
092: }
093:
094: public String getUpdateIcon() {
095: return FormattedText.escapeHtml(baseURL + UPDATE_IMAGE, false);
096: }
097:
098: public String getUpdateURL() {
099: return FormattedText.escapeHtml(baseURL + UPDATE_URL, false);
100: }
101:
102: public void sendIcon(HttpServletResponse response) {
103: String realPath = context.getRealPath(IMAGE_ICON);
104: File f = new File(realPath);
105: int nbytes = (int) f.length();
106: response.setContentLength(nbytes);
107: InputStream is = null;
108: try {
109: is = new FileInputStream(f);
110: byte[] b = new byte[4096];
111: OutputStream out = response.getOutputStream();
112: for (; nbytes > 0;) {
113: int nb = is.read(b);
114: if (nb > 0) {
115: out.write(b, 0, nb);
116: nbytes -= nb;
117: } else {
118: Thread.yield();
119: }
120: }
121: } catch (IOException e) {
122:
123: e.printStackTrace();
124: } finally {
125: try {
126: is.close();
127: } catch (Exception ex) {
128:
129: }
130: }
131:
132: }
133:
134: public String getSystemName() {
135: return FormattedText.escapeHtml(ServerConfigurationService
136: .getString("ui.service", "Sakai"), false);
137: }
138:
139: }
|