001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-impl/impl/src/java/org/sakaiproject/portal/charon/handlers/ToolHandler.java $
003: * $Id: ToolHandler.java 29143 2007-04-19 01:10:38Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006, 2007 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.charon.handlers;
021:
022: import java.io.IOException;
023:
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.sakaiproject.exception.IdUnusedException;
028: import org.sakaiproject.exception.PermissionException;
029: import org.sakaiproject.portal.api.Portal;
030: import org.sakaiproject.portal.api.PortalHandlerException;
031: import org.sakaiproject.site.api.Site;
032: import org.sakaiproject.site.api.ToolConfiguration;
033: import org.sakaiproject.site.cover.SiteService;
034: import org.sakaiproject.tool.api.ActiveTool;
035: import org.sakaiproject.tool.api.Session;
036: import org.sakaiproject.tool.api.Tool;
037: import org.sakaiproject.tool.api.ToolException;
038: import org.sakaiproject.tool.api.ToolSession;
039: import org.sakaiproject.tool.cover.ActiveToolManager;
040: import org.sakaiproject.tool.cover.SessionManager;
041: import org.sakaiproject.util.Web;
042:
043: /**
044: *
045: * @author ieb
046: * @since Sakai 2.4
047: * @version $Rev: 29143 $
048: *
049: */
050: public class ToolHandler extends BasePortalHandler {
051: public ToolHandler() {
052: urlFragment = "tool";
053: }
054:
055: @Override
056: public int doPost(String[] parts, HttpServletRequest req,
057: HttpServletResponse res, Session session)
058: throws PortalHandlerException {
059: return doGet(parts, req, res, session);
060: }
061:
062: @Override
063: public int doGet(String[] parts, HttpServletRequest req,
064: HttpServletResponse res, Session session)
065: throws PortalHandlerException {
066: // recognize and dispatch the 'tool' option: [1] = "tool", [2] =
067: // placement id (of a site's tool placement), rest for the tool
068: if ((parts.length > 2) && (parts[1].equals("tool"))) {
069: try {
070: // Resolve the placements of the form
071: // /portal/tool/sakai.resources?sakai.site=~csev
072: String toolPlacement = portal.getPlacement(req, res,
073: session, parts[2], false);
074: if (toolPlacement == null) {
075: return ABORT;
076: }
077: parts[2] = toolPlacement;
078:
079: doTool(req, res, session, parts[2], req
080: .getContextPath()
081: + req.getServletPath()
082: + Web.makePath(parts, 1, 3), Web.makePath(
083: parts, 3, parts.length));
084: return END;
085: } catch (Exception ex) {
086: throw new PortalHandlerException(ex);
087: }
088: } else {
089: return NEXT;
090: }
091: }
092:
093: public void doTool(HttpServletRequest req, HttpServletResponse res,
094: Session session, String placementId,
095: String toolContextPath, String toolPathInfo)
096: throws ToolException, IOException {
097:
098: if (portal.redirectIfLoggedOut(res))
099: return;
100:
101: // find the tool from some site
102: ToolConfiguration siteTool = SiteService.findTool(placementId);
103: if (siteTool == null) {
104: portal.doError(req, res, session, Portal.ERROR_WORKSITE);
105: return;
106: }
107:
108: // Reset the tool state if requested
109: if ("true".equals(req.getParameter(portalService
110: .getResetStateParam()))
111: || "true".equals(portalService.getResetState())) {
112: Session s = SessionManager.getCurrentSession();
113: ToolSession ts = s.getToolSession(placementId);
114: ts.clearAttributes();
115: }
116:
117: // find the tool registered for this
118: ActiveTool tool = ActiveToolManager.getActiveTool(siteTool
119: .getToolId());
120: if (tool == null) {
121: portal.doError(req, res, session, Portal.ERROR_WORKSITE);
122: return;
123: }
124:
125: // permission check - visit the site (unless the tool is configured to
126: // bypass)
127: if (tool.getAccessSecurity() == Tool.AccessSecurity.PORTAL) {
128: Site site = null;
129: try {
130: site = SiteService.getSiteVisit(siteTool.getSiteId());
131: } catch (IdUnusedException e) {
132: portal
133: .doError(req, res, session,
134: Portal.ERROR_WORKSITE);
135: return;
136: } catch (PermissionException e) {
137: // if not logged in, give them a chance
138: if (session.getUserId() == null) {
139: portal.doLogin(req, res, session,
140: req.getPathInfo(), false);
141: } else {
142: portal.doError(req, res, session,
143: Portal.ERROR_WORKSITE);
144: }
145: return;
146: }
147: }
148:
149: portal.forwardTool(tool, req, res, siteTool,
150: siteTool.getSkin(), toolContextPath, toolPathInfo);
151: }
152:
153: }
|