001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/msgcntr/trunk/messageforums-app/src/java/org/sakaiproject/tool/messageforums/MessageForumsFilePickerServlet.java $
003: * $Id: MessageForumsFilePickerServlet.java 9227 2006-05-15 15:02:42Z cwen@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 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.tool.messageforums;
021:
022: import java.io.IOException;
023: import java.util.Enumeration;
024:
025: import javax.servlet.RequestDispatcher;
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.sakaiproject.tool.api.ToolSession;
031: import org.sakaiproject.tool.cover.SessionManager;
032: import org.sakaiproject.tool.api.ActiveTool;
033: import org.sakaiproject.tool.api.Tool;
034: import org.sakaiproject.tool.cover.ActiveToolManager;
035: import org.sakaiproject.jsf.util.JsfTool;
036: import org.sakaiproject.util.Web;
037: import org.sakaiproject.tool.api.ToolException;
038:
039: /**
040: * @author Chen Wen
041: * @version $Id$
042: *
043: */
044: public class MessageForumsFilePickerServlet extends JsfTool {
045: private static final String HELPER_EXT = ".helper";
046:
047: private static final String HELPER_SESSION_PREFIX = "session.";
048:
049: protected void dispatch(HttpServletRequest req,
050: HttpServletResponse res) throws ServletException,
051: IOException {
052: // NOTE: this is a simple path dispatching, taking the path as the view
053: // id = jsp file name for the view,
054: // with default used if no path and a path prefix as configured.
055: // TODO: need to allow other sorts of dispatching, such as pulling out
056: // drill-down ids and making them
057: // available to the JSF
058:
059: // build up the target that will be dispatched to
060: String target = req.getPathInfo();
061:
062: // see if we have a helper request
063: if (sendToHelper(req, res, target)) {
064: return;
065: }
066:
067: // see if we have a resource request - i.e. a path with an extension,
068: // and one that is not the JSF_EXT
069: if (isResourceRequest(target)) {
070: // get a dispatcher to the path
071: RequestDispatcher resourceDispatcher = getServletContext()
072: .getRequestDispatcher(target);
073: if (resourceDispatcher != null) {
074: resourceDispatcher.forward(req, res);
075: return;
076: }
077: }
078:
079: if ("Title".equals(req.getParameter("panel"))) {
080: // This allows only one Title JSF for each tool
081: target = "/title.jsf";
082: }
083:
084: else {
085: ToolSession session = SessionManager
086: .getCurrentToolSession();
087:
088: if (target == null || "/".equals(target)) {
089: target = computeDefaultTarget(true);
090:
091: // make sure it's a valid path
092: if (!target.startsWith("/")) {
093: target = "/" + target;
094: }
095:
096: // now that we've messed with the URL, send a redirect to make
097: // it official
098: res.sendRedirect(Web.returnUrl(req, target));
099: return;
100: }
101:
102: // see if we want to change the specifically requested view
103: String newTarget = redirectRequestedTarget(target);
104:
105: // make sure it's a valid path
106: if (!newTarget.startsWith("/")) {
107: newTarget = "/" + newTarget;
108: }
109:
110: if (!newTarget.equals(target)) {
111: // now that we've messed with the URL, send a redirect to make
112: // it official
113: res.sendRedirect(Web.returnUrl(req, newTarget));
114: return;
115: }
116: target = newTarget;
117:
118: // store this
119: if (m_defaultToLastView) {
120: session.setAttribute(LAST_VIEW_VISITED, target);
121: }
122: }
123:
124: // add the configured folder root and extension (if missing)
125: target = m_path + target;
126:
127: // add the default JSF extension (if we have no extension)
128: int lastSlash = target.lastIndexOf("/");
129: int lastDot = target.lastIndexOf(".");
130: if (lastDot < 0 || lastDot < lastSlash) {
131: target += JSF_EXT;
132: }
133:
134: // set the information that can be removed from return URLs
135: req.setAttribute(URL_PATH, m_path);
136: req.setAttribute(URL_EXT, ".jsp");
137:
138: // set the sakai request object wrappers to provide the native, not
139: // Sakai set up, URL information
140: // - this assures that the FacesServlet can dispatch to the proper view
141: // based on the path info
142: req.setAttribute(Tool.NATIVE_URL, Tool.NATIVE_URL);
143:
144: // TODO: Should setting the HTTP headers be moved up to the portal level
145: // as well?
146: res.setContentType("text/html; charset=UTF-8");
147: res.addDateHeader("Expires", System.currentTimeMillis()
148: - (1000L * 60L * 60L * 24L * 365L));
149: res.addDateHeader("Last-Modified", System.currentTimeMillis());
150: res
151: .addHeader("Cache-Control",
152: "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
153: res.addHeader("Pragma", "no-cache");
154:
155: // dispatch to the target
156: RequestDispatcher dispatcher = getServletContext()
157: .getRequestDispatcher(target);
158: dispatcher.forward(req, res);
159:
160: // restore the request object
161: req.removeAttribute(Tool.NATIVE_URL);
162: req.removeAttribute(URL_PATH);
163: req.removeAttribute(URL_EXT);
164: }
165:
166: protected boolean sendToHelper(HttpServletRequest req,
167: HttpServletResponse res, String target)
168: throws ToolException {
169: String path = req.getPathInfo();
170: if (path == null)
171: path = "/";
172:
173: // if synoptic helper is obscuring add attachment helper, remove it
174: final int helperIndex = path.indexOf("sakai.filepicker");
175:
176: // filepicker helper should be first part of path
177: if (helperIndex > 1) {
178: path = path.substring(helperIndex - 1);
179: target = path;
180: }
181:
182: //
183: // 0 parts means the path was just "/", otherwise parts[0] = "",
184: // parts[1] = item id, parts[2] if present is "edit"...
185: String[] parts = path.split("/");
186:
187: if (parts.length < 2) {
188: return false;
189: }
190: /* if (parts.length < 3) {
191: // return false;
192: }*/
193:
194: if (!parts[1].endsWith(HELPER_EXT)) {
195: return false;
196: }
197: /*
198: if (!parts[2].endsWith(HELPER_EXT)) {
199: return false;
200: }
201: */
202: ToolSession toolSession = SessionManager
203: .getCurrentToolSession();
204:
205: Enumeration params = req.getParameterNames();
206: while (params.hasMoreElements()) {
207: String paramName = (String) params.nextElement();
208: if (paramName.startsWith(HELPER_SESSION_PREFIX)) {
209: String attributeName = paramName
210: .substring(HELPER_SESSION_PREFIX.length());
211: toolSession.setAttribute(attributeName, req
212: .getParameter(paramName));
213: }
214: }
215:
216: // calc helper id
217: int posEnd = parts[1].lastIndexOf(".");
218: ////int posEnd = parts[2].lastIndexOf(".");
219:
220: String helperId = target.substring(1, posEnd + 1);
221: ////String helperId = parts[2].substring(0, posEnd);
222: ActiveTool helperTool = ActiveToolManager
223: .getActiveTool(helperId);
224:
225: if (toolSession.getAttribute(helperTool.getId()
226: + Tool.HELPER_DONE_URL) == null) {
227: toolSession
228: .setAttribute(helperTool.getId()
229: + Tool.HELPER_DONE_URL, req
230: .getContextPath()
231: + req.getServletPath()
232: + computeDefaultTarget(true));
233: }
234:
235: String context = req.getContextPath() + req.getServletPath()
236: + Web.makePath(parts, 1, 2);
237: ////String context = req.getContextPath() + req.getServletPath() + Web.makePath(parts, 2, 3);
238: String toolPath = Web.makePath(parts, 2, parts.length);
239: ////String toolPath = Web.makePath(parts, 3, parts.length);
240: helperTool.help(req, res, context, toolPath);
241:
242: return true; // was handled as helper call
243: }
244:
245: protected String computeDefaultTarget(boolean lastVisited) {
246: // setup for the default view as configured
247: String target = "/" + m_default;
248:
249: // if we are doing lastVisit and there's a last-visited view, for this
250: // tool placement / user, use that
251: if (lastVisited) {
252: ToolSession session = SessionManager
253: .getCurrentToolSession();
254: String last = (String) session
255: .getAttribute(LAST_VIEW_VISITED);
256: if (last != null) {
257: target = last;
258: }
259: }
260:
261: return target;
262: }
263: }
|