001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-tool/tool/src/java/org/sakaiproject/portal/tool/ToolPortal.java $
003: * $Id: ToolPortal.java 29143 2007-04-19 01:10:38Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.portal.tool;
021:
022: import java.io.IOException;
023:
024: import javax.servlet.ServletConfig;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServlet;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.sakaiproject.component.cover.ServerConfigurationService;
033: import org.sakaiproject.exception.IdUnusedException;
034: import org.sakaiproject.exception.PermissionException;
035: import org.sakaiproject.portal.util.ErrorReporter;
036: import org.sakaiproject.portal.util.ToolURLManagerImpl;
037: import org.sakaiproject.site.api.Site;
038: import org.sakaiproject.site.api.ToolConfiguration;
039: import org.sakaiproject.site.cover.SiteService;
040: import org.sakaiproject.tool.api.ActiveTool;
041: import org.sakaiproject.tool.api.Placement;
042: import org.sakaiproject.tool.api.Session;
043: import org.sakaiproject.tool.api.Tool;
044: import org.sakaiproject.tool.api.ToolException;
045: import org.sakaiproject.tool.api.ToolSession;
046: import org.sakaiproject.tool.api.ToolURL;
047: import org.sakaiproject.tool.cover.ActiveToolManager;
048: import org.sakaiproject.tool.cover.SessionManager;
049: import org.sakaiproject.util.StringUtil;
050: import org.sakaiproject.util.Web;
051:
052: /**
053: * @author ieb
054: * @since Sakai 2.4
055: * @version $Rev: 29143 $
056: */
057:
058: public class ToolPortal extends HttpServlet {
059: /** Our log (commons). */
060: private static Log M_log = LogFactory.getLog(ToolPortal.class);
061:
062: /**
063: * Access the Servlet's information display.
064: *
065: * @return servlet information.
066: */
067: @Override
068: public String getServletInfo() {
069: return "Sakai Tool Portal";
070: }
071:
072: /**
073: * Initialize the servlet.
074: *
075: * @param config
076: * The servlet config.
077: * @throws ServletException
078: */
079: @Override
080: public void init(ServletConfig config) throws ServletException {
081: super .init(config);
082:
083: M_log.info("init()");
084: }
085:
086: /**
087: * Shutdown the servlet.
088: */
089: @Override
090: public void destroy() {
091: M_log.info("destroy()");
092:
093: super .destroy();
094: }
095:
096: /**
097: * Respond to navigation / access requests.
098: *
099: * @param req
100: * The servlet request.
101: * @param res
102: * The servlet response.
103: * @throws ServletException.
104: * @throws IOException.
105: */
106: @Override
107: protected void doGet(HttpServletRequest req, HttpServletResponse res)
108: throws ServletException, IOException {
109: try {
110: // get the Sakai session
111: Session session = SessionManager.getCurrentSession();
112:
113: // our path is /placement-id/tool-destination, but we want to
114: // include anchors and parameters in the destination...
115: String path = req.getPathInfo();
116: if ((path == null) || (path.length() <= 1))
117: throw new Exception("no path");
118:
119: // get the placement id, ignoring the first "/"
120: String[] parts = StringUtil.splitFirst(path.substring(1),
121: "/");
122: String placementId = parts[0];
123:
124: // get the toolPath if specified
125: String toolPath = null;
126: if (parts.length == 2)
127: toolPath = "/" + parts[1];
128:
129: boolean success = doTool(req, res, session, placementId,
130: req.getContextPath() + req.getServletPath() + "/"
131: + placementId, toolPath);
132:
133: } catch (Throwable t) {
134: doError(req, res, t);
135: }
136: }
137:
138: /**
139: * Process a tool request
140: *
141: * @param req
142: * @param res
143: * @param session
144: * @param placementId
145: * @param toolContextPath
146: * @param toolPathInfo
147: * @return true if the processing was successful, false if nt
148: * @throws ToolException
149: * @throws IOException
150: */
151: protected boolean doTool(HttpServletRequest req,
152: HttpServletResponse res, Session session,
153: String placementId, String toolContextPath,
154: String toolPathInfo) throws ToolException, IOException {
155: // find the tool from some site
156: // TODO: all placements are from sites? -ggolden
157: ToolConfiguration siteTool = SiteService.findTool(placementId);
158: if (siteTool == null)
159: return false;
160:
161: // find the tool registered for this
162: ActiveTool tool = ActiveToolManager.getActiveTool(siteTool
163: .getToolId());
164: if (tool == null)
165: return false;
166:
167: // permission check - visit the site (unless the tool is configured to
168: // bypass)
169: if (tool.getAccessSecurity() == Tool.AccessSecurity.PORTAL) {
170: Site site = null;
171: try {
172: site = SiteService.getSiteVisit(siteTool.getSiteId());
173: } catch (IdUnusedException e) {
174: return false;
175: } catch (PermissionException e) {
176: // TODO: login here?
177: return false;
178: }
179: }
180:
181: // if the path is not set, and we are expecting one, we need to compute
182: // the path and redirect
183: // we expect a path only if the tool has a registered home -ggolden
184: if ((toolPathInfo == null) && (tool.getHome() != null)) {
185: // what path? The one last visited, or home
186: ToolSession toolSession = SessionManager
187: .getCurrentSession().getToolSession(placementId);
188: String redirectPath = (String) toolSession
189: .getAttribute(ActiveTool.TOOL_ATTR_CURRENT_DESTINATION);
190: if (redirectPath == null) {
191: redirectPath = tool.getHome();
192: }
193:
194: // redirect with this tool path
195: String redirectUrl = ServerConfigurationService
196: .getServerUrl()
197: + toolContextPath + redirectPath;
198: res.sendRedirect(res.encodeRedirectURL(redirectUrl));
199: return true;
200: }
201:
202: // store the path as the current path, if we are doing this
203: if (tool.getHome() != null) {
204: ToolSession toolSession = SessionManager
205: .getCurrentSession().getToolSession(placementId);
206: toolSession.setAttribute(
207: ActiveTool.TOOL_ATTR_CURRENT_DESTINATION,
208: toolPathInfo);
209: }
210:
211: // prepare for the forward
212: setupForward(req, res, siteTool, siteTool.getSkin());
213: req.setAttribute(ToolURL.MANAGER, new ToolURLManagerImpl(res));
214:
215: // let the tool do the the work (forward)
216: tool.forward(req, res, siteTool, toolContextPath, toolPathInfo);
217:
218: return true;
219: }
220:
221: protected void doError(HttpServletRequest req,
222: HttpServletResponse res, Throwable t) {
223: ErrorReporter err = new ErrorReporter();
224: err.report(req, res, t);
225: }
226:
227: /**
228: * Respond to data posting requests.
229: *
230: * @param req
231: * The servlet request.
232: * @param res
233: * The servlet response.
234: * @throws ServletException.
235: * @throws IOException.
236: */
237: @Override
238: protected void doPost(HttpServletRequest req,
239: HttpServletResponse res) throws ServletException,
240: IOException {
241: doGet(req, res);
242: }
243:
244: /**
245: * Setup the request attributes with information used by the tools in their
246: * response.
247: *
248: * @param req
249: * @param res
250: * @param p
251: * @param skin
252: * @throws ToolException
253: */
254: protected void setupForward(HttpServletRequest req,
255: HttpServletResponse res, Placement p, String skin)
256: throws ToolException {
257: // setup html information that the tool might need (skin, body on load,
258: // js includes, etc).
259: if (skin == null || skin.length() == 0)
260: skin = ServerConfigurationService.getString("skin.default");
261: String skinRepo = ServerConfigurationService
262: .getString("skin.repo");
263: String headCssToolBase = "<link href=\""
264: + skinRepo
265: + "/tool_base.css\" type=\"text/css\" rel=\"stylesheet\" media=\"all\" />\n";
266: String headCssToolSkin = "<link href=\""
267: + skinRepo
268: + "/"
269: + skin
270: + "/tool.css\" type=\"text/css\" rel=\"stylesheet\" media=\"all\" />\n";
271: String headCss = headCssToolBase + headCssToolSkin;
272: String headJs = "<script type=\"text/javascript\" language=\"JavaScript\" src=\"/library/js/headscripts.js\"></script>\n";
273: String head = headCss + headJs;
274: StringBuilder bodyonload = new StringBuilder();
275: if (p != null) {
276: String element = Web.escapeJavascript("Main" + p.getId());
277: bodyonload.append("setMainFrameHeight('" + element + "');");
278: }
279: bodyonload.append("setFocus(focus_path);");
280:
281: req.setAttribute("sakai.html.head", head);
282: req.setAttribute("sakai.html.head.css", headCss);
283: req.setAttribute("sakai.html.head.css.base", headCssToolBase);
284: req.setAttribute("sakai.html.head.css.skin", headCssToolSkin);
285: req.setAttribute("sakai.html.head.js", headJs);
286: req.setAttribute("sakai.html.body.onload", bodyonload
287: .toString());
288: }
289: }
|