001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/search/tags/sakai_2-4-1/search-tool/tool/src/java/org/sakaiproject/search/tool/ControllerServlet.java $
003: * $Id: ControllerServlet.java 29315 2007-04-20 14:28:12Z ajpoland@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.search.tool;
021:
022: import java.io.IOException;
023:
024: import javax.servlet.RequestDispatcher;
025: import javax.servlet.ServletConfig;
026: import javax.servlet.ServletContext;
027: import javax.servlet.ServletException;
028: import javax.servlet.http.HttpServlet;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.sakaiproject.search.tool.api.SearchBeanFactory;
035: import org.sakaiproject.tool.api.SessionManager;
036: import org.sakaiproject.tool.api.Tool;
037: import org.sakaiproject.tool.api.ToolSession;
038: import org.springframework.web.context.WebApplicationContext;
039: import org.springframework.web.context.support.WebApplicationContextUtils;
040:
041: /**
042: * @author Ian Boston
043: */
044: public class ControllerServlet extends HttpServlet {
045: public static Log log = LogFactory.getLog(ControllerServlet.class);
046:
047: /**
048: * Required for serialization... also to stop eclipse from giving me a
049: * warning!
050: */
051: private static final long serialVersionUID = 676743152200357708L;
052:
053: public static final String SAVED_REQUEST_URL = "org.sakaiproject.search.api.last-request-url";
054:
055: private static final String PANEL = "panel";
056:
057: private static final Object TITLE_PANEL = "Title";
058:
059: private WebApplicationContext wac;
060:
061: private SearchBeanFactory searchBeanFactory = null;
062:
063: private SessionManager sessionManager;
064:
065: public void init(ServletConfig servletConfig)
066: throws ServletException {
067:
068: super .init(servletConfig);
069:
070: ServletContext sc = servletConfig.getServletContext();
071:
072: wac = WebApplicationContextUtils.getWebApplicationContext(sc);
073: try {
074: searchBeanFactory = (SearchBeanFactory) wac
075: .getBean("search-searchBeanFactory");
076: ServletContext context = getServletContext();
077: searchBeanFactory.setContext(context);
078: } catch (Exception ex) {
079: }
080: if (sessionManager == null) {
081: sessionManager = (SessionManager) wac
082: .getBean(SessionManager.class.getName());
083: }
084:
085: }
086:
087: protected void doGet(final HttpServletRequest request,
088: HttpServletResponse response) throws ServletException,
089: IOException {
090:
091: execute(request, response);
092: }
093:
094: protected void doPost(HttpServletRequest request,
095: HttpServletResponse response) throws ServletException,
096: IOException {
097: execute(request, response);
098: }
099:
100: public void execute(HttpServletRequest request,
101: HttpServletResponse response) throws ServletException,
102: IOException {
103: if (wac == null) {
104: wac = WebApplicationContextUtils
105: .getRequiredWebApplicationContext(this
106: .getServletContext());
107: if (wac == null) {
108: response.sendError(
109: HttpServletResponse.SC_SERVICE_UNAVAILABLE,
110: "Cannot get WebApplicationContext");
111: return;
112: }
113:
114: }
115: request.setAttribute(Tool.NATIVE_URL, Tool.NATIVE_URL);
116:
117: if (searchBeanFactory == null) {
118: searchBeanFactory = (SearchBeanFactory) wac
119: .getBean("search-searchBeanFactory");
120: ServletContext context = getServletContext();
121: searchBeanFactory.setContext(context);
122: }
123: if (sessionManager == null) {
124: sessionManager = (SessionManager) wac
125: .getBean(SessionManager.class.getName());
126: }
127:
128: addLocalHeaders(request);
129:
130: String targetURL = persistState(request);
131: if (targetURL != null && targetURL.trim().length() > 0) {
132: response.sendRedirect(targetURL);
133: return;
134: }
135: if (TITLE_PANEL.equals(request.getParameter(PANEL))) {
136:
137: String targetPage = "/WEB-INF/pages/title.jsp";
138: RequestDispatcher rd = request
139: .getRequestDispatcher(targetPage);
140: rd.forward(request, response);
141:
142: } else {
143: String path = request.getPathInfo();
144: if (path == null || path.length() == 0) {
145: path = "/index";
146: }
147: if (!path.startsWith("/")) {
148: path = "/" + path;
149: }
150:
151: String targetPage = "/WEB-INF/pages" + path + ".jsp";
152:
153: request.setAttribute(
154: SearchBeanFactory.SEARCH_BEAN_FACTORY_ATTR,
155: searchBeanFactory);
156: RequestDispatcher rd = request
157: .getRequestDispatcher(targetPage);
158: rd.forward(request, response);
159: }
160:
161: request.removeAttribute(Tool.NATIVE_URL);
162: }
163:
164: public void addLocalHeaders(HttpServletRequest request) {
165: String sakaiHeader = (String) request
166: .getAttribute("sakai.html.head");
167: String skin = "default/"; // this could be changed in the future to
168: // make search skin awaire
169: String localStylesheet = "<link href=\"/sakai-search-tool/styles/"
170: + skin
171: + "searchStyle.css\" type=\"text/css\" rel=\"stylesheet\" media=\"all\" />";
172: String localJavascript = "<script type=\"text/javascript\" src=\"/sakai-search-tool/scripts/search.js\"> </script>";
173: request.setAttribute("sakai.html.head", localStylesheet
174: + sakaiHeader + localJavascript);
175: }
176:
177: /**
178: * returns the request state for the tool. If the state is restored, we set
179: * the request attribute RWikiServlet.REQUEST_STATE_RESTORED to Boolean.TRUE
180: * and a Thread local named RWikiServlet.REQUEST_STATE_RESTORED to
181: * Boolean.TRUE. These MUST be checked by anything that modifies state, to
182: * ensure that a reinitialisation of Tool state does not result in a repost
183: * of data.
184: *
185: * @param request
186: * @return
187: */
188: private String persistState(HttpServletRequest request) {
189: ToolSession ts = sessionManager.getCurrentToolSession();
190: if (isPageToolDefault(request)) {
191: log.debug("Incomming URL is "
192: + request.getRequestURL().toString() + "?"
193: + request.getQueryString());
194: log.debug("Restore " + ts.getAttribute(SAVED_REQUEST_URL));
195: return (String) ts.getAttribute(SAVED_REQUEST_URL);
196: }
197: if (isPageRestorable(request)) {
198: ts.setAttribute(SAVED_REQUEST_URL, request.getRequestURL()
199: .toString()
200: + "?" + request.getQueryString());
201: log.debug("Saved " + ts.getAttribute(SAVED_REQUEST_URL));
202: }
203: return null;
204: }
205:
206: /**
207: * Check to see if the reques represents the Tool default page. This is not
208: * the same as the view Home. It is the same as first entry into a Tool or
209: * when the page is refreshed
210: *
211: * @param request
212: * @return true if the page is the Tool default page
213: */
214: private boolean isPageToolDefault(HttpServletRequest request) {
215: if (TITLE_PANEL.equals(request.getParameter(PANEL)))
216: return false;
217: String pathInfo = request.getPathInfo();
218: String queryString = request.getQueryString();
219: String method = request.getMethod();
220: return ("GET".equalsIgnoreCase(method)
221: && (pathInfo == null || request.getPathInfo().length() == 0) && (queryString == null || queryString
222: .length() == 0));
223: }
224:
225: /**
226: * Check to see if the request represents a page that can act as a restor
227: * point.
228: *
229: * @param request
230: * @return true if it is possible to restore to this point.
231: */
232: private boolean isPageRestorable(HttpServletRequest request) {
233:
234: if (TITLE_PANEL.equals(request.getParameter(PANEL)))
235: return false;
236: String pathInfo = request.getPathInfo();
237: if (pathInfo != null) {
238: if (request.getPathInfo().endsWith(".gif")) {
239: return false;
240: }
241: if (request.getPathInfo().endsWith(".src")) {
242: return false;
243: }
244: }
245:
246: if ("GET".equalsIgnoreCase(request.getMethod()))
247: return true;
248:
249: return false;
250: }
251:
252: }
|