001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.servlets;
017:
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpServletResponse;
020: import org.jamwiki.utils.WikiLogger;
021: import org.springframework.web.servlet.ModelAndView;
022: import org.springframework.web.servlet.mvc.AbstractController;
023:
024: /**
025: * Provides the infrastructure that is common to all JAMWiki servlets. Unless
026: * special handling is required all JAMWiki servlets should extend this
027: * servlet.
028: */
029: public abstract class JAMWikiServlet extends AbstractController {
030:
031: private static final WikiLogger logger = WikiLogger
032: .getLogger(JAMWikiServlet.class.getName());
033:
034: /** Flag to indicate whether or not the servlet should load the nav bar and other layout elements. */
035: protected boolean layout = true;
036: /** The prefix of the JSP file used to display the servlet output. */
037: protected String displayJSP = "wiki";
038: /** Any page that take longer than this value (specified in milliseconds) will print a warning to the log. */
039: protected static final int SLOW_PAGE_LIMIT = 1000;
040:
041: /**
042: * Abstract method that must be implemented by all sub-classes to handle
043: * servlet requests.
044: *
045: * @param request The servlet request object.
046: * @param response The servlet response object.
047: * @param next A ModelAndView object that has been initialized to the view
048: * specified by the <code>displayJSP</code> member variable.
049: * @param pageInfo A WikiPageInfo object that will hold output parameters
050: * to be passed to the output JSP.
051: * @return A ModelAndView object corresponding to the information to be
052: * rendered, or <code>null</code> if the method directly handles its own
053: * output, for example by writing directly to the output response.
054: */
055: protected abstract ModelAndView handleJAMWikiRequest(
056: HttpServletRequest request, HttpServletResponse response,
057: ModelAndView next, WikiPageInfo pageInfo) throws Exception;
058:
059: /**
060: * Implement the handleRequestInternal method specified by the
061: * Spring AbstractController class.
062: *
063: * @param request The servlet request object.
064: * @param response The servlet response object.
065: * @return A ModelAndView object corresponding to the information to be
066: * rendered, or <code>null</code> if the method directly handles its own
067: * output, for example by writing directly to the output response.
068: * @throws Exception Thrown if any error occurs during method execution.
069: */
070: public ModelAndView handleRequestInternal(
071: HttpServletRequest request, HttpServletResponse response) {
072: long start = System.currentTimeMillis();
073: initParams();
074: ModelAndView next = new ModelAndView(this .displayJSP);
075: WikiPageInfo pageInfo = new WikiPageInfo();
076: try {
077: next = this .handleJAMWikiRequest(request, response, next,
078: pageInfo);
079: if (this .layout) {
080: ServletUtil.loadDefaults(request, next, pageInfo);
081: }
082: } catch (Throwable t) {
083: return ServletUtil.viewError(request, t);
084: }
085: long execution = System.currentTimeMillis() - start;
086: if (execution > JAMWikiServlet.SLOW_PAGE_LIMIT) {
087: logger.warning("Slow page loading time: "
088: + request.getRequestURI() + " ("
089: + (execution / 1000.000) + " s.)");
090: }
091: logger.info("Loaded page " + request.getRequestURI() + " ("
092: + (execution / 1000.000) + " s.)");
093: return next;
094: }
095:
096: /**
097: * If any special servlet initialization needs to be performed it can be done
098: * by overriding this method. In particular, this method can be used to
099: * override the defaults for the <code>layout</code> member variable, which
100: * determines whether or not the output JSP should include the left navigation
101: * and other layout values, and the <code>displayJSP</code> member variable,
102: * which determine the JSP file used to render output.
103: */
104: protected void initParams() {
105: }
106: }
|