001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-impl/impl/src/java/org/sakaiproject/portal/charon/handlers/PageHandler.java $
003: * $Id: PageHandler.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: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.sakaiproject.component.cover.ServerConfigurationService;
034: import org.sakaiproject.exception.IdUnusedException;
035: import org.sakaiproject.exception.PermissionException;
036: import org.sakaiproject.portal.api.Portal;
037: import org.sakaiproject.portal.api.PortalHandlerException;
038: import org.sakaiproject.portal.api.PortalRenderContext;
039: import org.sakaiproject.portal.api.StoredState;
040: import org.sakaiproject.portal.util.PortalSiteHelper;
041: import org.sakaiproject.site.api.Site;
042: import org.sakaiproject.site.api.SitePage;
043: import org.sakaiproject.site.api.ToolConfiguration;
044: import org.sakaiproject.site.cover.SiteService;
045: import org.sakaiproject.tool.api.Session;
046: import org.sakaiproject.tool.api.ToolException;
047:
048: /**
049: *
050: * @author ieb
051: * @since Sakai 2.4
052: * @version $Rev: 29143 $
053: *
054: */
055: public class PageHandler extends BasePortalHandler {
056:
057: private static final String INCLUDE_PAGE = "include-page";
058:
059: private static final Log log = LogFactory.getLog(PageHandler.class);
060:
061: protected PortalSiteHelper siteHelper = new PortalSiteHelper();
062:
063: public PageHandler() {
064: urlFragment = "page";
065: }
066:
067: @Override
068: public int doPost(String[] parts, HttpServletRequest req,
069: HttpServletResponse res, Session session)
070: throws PortalHandlerException {
071: return doGet(parts, req, res, session);
072: }
073:
074: @Override
075: public int doGet(String[] parts, HttpServletRequest req,
076: HttpServletResponse res, Session session)
077: throws PortalHandlerException {
078: if ((parts.length == 3) && (parts[1].equals("page"))) {
079: try {
080: // Resolve the placements of the form
081: // /portal/page/sakai.resources?sakai.site=~csev
082: String pagePlacement = portal.getPlacement(req, res,
083: session, parts[2], true);
084: if (pagePlacement == null) {
085: return ABORT;
086: }
087: parts[2] = pagePlacement;
088:
089: doPage(req, res, session, parts[2], req
090: .getContextPath()
091: + req.getServletPath());
092: return END;
093: } catch (Exception ex) {
094: throw new PortalHandlerException(ex);
095: }
096: } else {
097: return NEXT;
098: }
099: }
100:
101: public void doPage(HttpServletRequest req, HttpServletResponse res,
102: Session session, String pageId, String toolContextPath)
103: throws ToolException, IOException {
104: // find the page from some site
105: SitePage page = SiteService.findPage(pageId);
106: if (page == null) {
107: portal.doError(req, res, session, Portal.ERROR_WORKSITE);
108: return;
109: }
110:
111: // permission check - visit the site
112: Site site = null;
113: try {
114: site = SiteService.getSiteVisit(page.getSiteId());
115: } catch (IdUnusedException e) {
116: portal.doError(req, res, session, Portal.ERROR_WORKSITE);
117: return;
118: } catch (PermissionException e) {
119: // if not logged in, give them a chance
120: if (session.getUserId() == null) {
121:
122: StoredState ss = portalService.newStoredState("", "");
123: ss.setRequest(req);
124: ss.setToolContextPath(toolContextPath);
125: portalService.setStoredState(ss);
126: portal.doLogin(req, res, session, req.getPathInfo(),
127: false);
128: } else {
129: portal
130: .doError(req, res, session,
131: Portal.ERROR_WORKSITE);
132: }
133: return;
134: }
135:
136: // form a context sensitive title
137: String title = ServerConfigurationService
138: .getString("ui.service")
139: + " : " + site.getTitle() + " : " + page.getTitle();
140:
141: String siteType = portal.calcSiteType(site.getId());
142: // start the response
143: PortalRenderContext rcontext = portal.startPageContext(
144: siteType, title, page.getSkin(), req);
145:
146: includePage(rcontext, res, req, page, toolContextPath,
147: "contentFull");
148:
149: portal.sendResponse(rcontext, res, "page", null);
150: StoredState ss = portalService.getStoredState();
151: if (ss != null
152: && toolContextPath.equals(ss.getToolContextPath())) {
153: // This request is the destination of the request
154: portalService.setStoredState(null);
155: }
156:
157: }
158:
159: public void includePage(PortalRenderContext rcontext,
160: HttpServletResponse res, HttpServletRequest req,
161: SitePage page, String toolContextPath, String wrapperClass)
162: throws IOException {
163: if (rcontext.uses(INCLUDE_PAGE)) {
164:
165: // divs to wrap the tools
166: rcontext.put("pageWrapperClass", wrapperClass);
167: rcontext
168: .put(
169: "pageColumnLayout",
170: (page.getLayout() == SitePage.LAYOUT_DOUBLE_COL) ? "col1of2"
171: : "col1");
172: Site site = null;
173: try {
174: site = SiteService.getSite(page.getSiteId());
175: } catch (Exception ignoreMe) {
176: // Non fatal - just assume null
177: if (log.isTraceEnabled())
178: log
179: .trace("includePage unable to find site for page "
180: + page.getId());
181: }
182: {
183: List<Map> toolList = new ArrayList<Map>();
184: List tools = page.getTools(0);
185: for (Iterator i = tools.iterator(); i.hasNext();) {
186: ToolConfiguration placement = (ToolConfiguration) i
187: .next();
188:
189: if (site != null) {
190: boolean this Tool = siteHelper.allowTool(site,
191: placement);
192: // System.out.println(" Allow Tool Display -" +
193: // placement.getTitle() + " retval = " + thisTool);
194: if (!this Tool)
195: continue; // Skip this tool if not
196: // allowed
197: }
198:
199: Map m = portal.includeTool(res, req, placement);
200: if (m != null) {
201: toolList.add(m);
202: }
203: }
204: rcontext.put("pageColumn0Tools", toolList);
205: }
206:
207: rcontext.put("pageTwoColumn", Boolean.valueOf(page
208: .getLayout() == SitePage.LAYOUT_DOUBLE_COL));
209:
210: // do the second column if needed
211: if (page.getLayout() == SitePage.LAYOUT_DOUBLE_COL) {
212: List<Map> toolList = new ArrayList<Map>();
213: List tools = page.getTools(1);
214: for (Iterator i = tools.iterator(); i.hasNext();) {
215: ToolConfiguration placement = (ToolConfiguration) i
216: .next();
217: Map m = portal.includeTool(res, req, placement);
218: if (m != null) {
219: toolList.add(m);
220: }
221: }
222: rcontext.put("pageColumn1Tools", toolList);
223: }
224: }
225: }
226:
227: }
|