001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-impl/impl/src/java/org/sakaiproject/portal/charon/handlers/DirectToolHandler.java $
003: * $Id: DirectToolHandler.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.portal.api.StoredState;
032: import org.sakaiproject.site.api.Site;
033: import org.sakaiproject.site.api.ToolConfiguration;
034: import org.sakaiproject.site.cover.SiteService;
035: import org.sakaiproject.tool.api.ActiveTool;
036: import org.sakaiproject.tool.api.Session;
037: import org.sakaiproject.tool.api.Tool;
038: import org.sakaiproject.tool.api.ToolException;
039: import org.sakaiproject.tool.api.ToolSession;
040: import org.sakaiproject.tool.cover.ActiveToolManager;
041: import org.sakaiproject.tool.cover.SessionManager;
042: import org.sakaiproject.util.Web;
043:
044: /**
045: * Handler to process directtool urls including storing destination state
046: *
047: * @author ieb
048: * @since Sakai 2.4
049: * @version $Rev: 29143 $
050: *
051: */
052: public class DirectToolHandler extends BasePortalHandler {
053: public DirectToolHandler() {
054: urlFragment = "directtool";
055: }
056:
057: @Override
058: public int doPost(String[] parts, HttpServletRequest req,
059: HttpServletResponse res, Session session)
060: throws PortalHandlerException {
061: return doGet(parts, req, res, session);
062: }
063:
064: @Override
065: public int doGet(String[] parts, HttpServletRequest req,
066: HttpServletResponse res, Session session)
067: throws PortalHandlerException {
068: if (portalService.isEnableDirect() && (parts.length > 2)
069: && (parts[1].equals("directtool"))) {
070: try {
071: // Resolve the placements of the form
072: // /portal/tool/sakai.resources?sakai.site=~csev
073: String toolPlacement = portal.getPlacement(req, res,
074: session, parts[2], false);
075: if (toolPlacement == null) {
076: return ABORT;
077: }
078: parts[2] = toolPlacement;
079:
080: return doDirectTool(req, res, session, parts[2], req
081: .getContextPath()
082: + req.getServletPath()
083: + Web.makePath(parts, 1, 3), Web.makePath(
084: parts, 3, parts.length));
085: } catch (Exception ex) {
086: throw new PortalHandlerException(ex);
087: }
088: } else {
089: return NEXT;
090: }
091: }
092:
093: /**
094: * Do direct tool, takes the url, stored the destination and the target
095: * iframe in the session constructs and outer url and when a request comes
096: * in that matches the stored iframe it ejects the destination address
097: *
098: * @param req
099: * @param res
100: * @param session
101: * @param placementId
102: * @param toolContextPath
103: * @param toolPathInfo
104: * @param placementId
105: * @throws ToolException
106: * @throws IOException
107: */
108: public int doDirectTool(HttpServletRequest req,
109: HttpServletResponse res, Session session,
110: String placementId, String toolContextPath,
111: String toolPathInfo) throws ToolException, IOException {
112: if (portal.redirectIfLoggedOut(res))
113: return ABORT;
114:
115: // find the tool from some site
116: ToolConfiguration siteTool = SiteService.findTool(placementId);
117: if (siteTool == null) {
118: portal.doError(req, res, session, Portal.ERROR_WORKSITE);
119: return END;
120: }
121:
122: // Reset the tool state if requested
123: if ("true".equals(req.getParameter(portalService
124: .getResetStateParam()))
125: || "true".equals(portalService.getResetState())) {
126: Session s = SessionManager.getCurrentSession();
127: ToolSession ts = s.getToolSession(placementId);
128: ts.clearAttributes();
129: }
130:
131: // find the tool registered for this
132: ActiveTool tool = ActiveToolManager.getActiveTool(siteTool
133: .getToolId());
134: if (tool == null) {
135: portal.doError(req, res, session, Portal.ERROR_WORKSITE);
136: return END;
137: }
138:
139: // permission check - visit the site (unless the tool is configured to
140: // bypass)
141: if (tool.getAccessSecurity() == Tool.AccessSecurity.PORTAL) {
142: Site site = null;
143: try {
144: site = SiteService.getSiteVisit(siteTool.getSiteId());
145: } catch (IdUnusedException e) {
146: portal
147: .doError(req, res, session,
148: Portal.ERROR_WORKSITE);
149: return END;
150: } catch (PermissionException e) {
151: // if not logged in, give them a chance
152: if (session.getUserId() == null) {
153: // let the tool do the the work (forward)
154: StoredState ss = portalService.newStoredState(
155: "directtool", "tool");
156: ss.setRequest(req);
157: ss.setPlacement(siteTool);
158: ss.setToolContextPath(toolContextPath);
159: ss.setToolPathInfo(toolPathInfo);
160: ss.setSkin(siteTool.getSkin());
161: portalService.setStoredState(ss);
162:
163: portal.doLogin(req, res, session, portal
164: .getPortalPageUrl(siteTool), false);
165: } else {
166: portal.doError(req, res, session,
167: Portal.ERROR_WORKSITE);
168: }
169: return END;
170: }
171: }
172: // let the tool do the the work (forward)
173: StoredState ss = portalService.newStoredState("directtool",
174: "tool");
175: ss.setRequest(req);
176: ss.setPlacement(siteTool);
177: ss.setToolContextPath(toolContextPath);
178: ss.setToolPathInfo(toolPathInfo);
179: ss.setSkin(siteTool.getSkin());
180: portalService.setStoredState(ss);
181:
182: portal.forwardPortal(tool, req, res, siteTool, siteTool
183: .getSkin(), toolContextPath, toolPathInfo);
184: return END;
185:
186: }
187:
188: }
|