001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2005 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki;
021:
022: import javax.servlet.*;
023: import javax.servlet.http.*;
024: import java.io.*;
025:
026: import org.apache.log4j.Logger;
027:
028: import com.ecyrd.jspwiki.url.DefaultURLConstructor;
029:
030: /**
031: * This provides a master servlet for dealing with short urls. It mostly does
032: * redirects to the proper JSP pages. It also intercepts the servlet
033: * shutdown events and uses it to signal wiki shutdown.
034: *
035: * @author Janne Jalkanen
036: * @author Andrew Jaquith
037: * @since 2.2
038: */
039: public class WikiServlet extends HttpServlet {
040: private static final long serialVersionUID = 3258410651167633973L;
041: private WikiEngine m_engine;
042: static final Logger log = Logger.getLogger(WikiServlet.class
043: .getName());
044:
045: /**
046: * {@inheritDoc}
047: */
048: public void init(ServletConfig config) throws ServletException {
049: super .init(config);
050:
051: m_engine = WikiEngine.getInstance(config);
052:
053: log.info("WikiServlet initialized.");
054: }
055:
056: /**
057: * Destroys the WikiServlet; called by the servlet container
058: * when shutting down the webapp. This method calls the
059: * protected method {@link WikiEngine#shutdown()}, which
060: * sends {@link com.ecyrd.jspwiki.event.WikiEngineEvent#SHUTDOWN}
061: * events to registered listeners.
062: * @see javax.servlet.GenericServlet#destroy()
063: */
064: public void destroy() {
065: log.info("WikiServlet shutdown.");
066: m_engine.shutdown();
067: super .destroy();
068: }
069:
070: /**
071: * {@inheritDoc}
072: */
073: public void doPost(HttpServletRequest req, HttpServletResponse res)
074: throws IOException, ServletException {
075: doGet(req, res);
076: }
077:
078: /**
079: * {@inheritDoc}
080: */
081: public void doGet(HttpServletRequest req, HttpServletResponse res)
082: throws IOException, ServletException {
083: String pageName = DefaultURLConstructor.parsePageFromURL(req,
084: m_engine.getContentEncoding());
085:
086: log.info("Request for page: " + pageName);
087:
088: if (pageName == null)
089: pageName = m_engine.getFrontPage(); // FIXME: Add special pages as well
090:
091: String jspPage = m_engine.getURLConstructor().getForwardPage(
092: req);
093:
094: RequestDispatcher dispatcher = req.getRequestDispatcher("/"
095: + jspPage + "?page=" + m_engine.encodeName(pageName)
096: + "&" + req.getQueryString());
097:
098: dispatcher.forward(req, res);
099: }
100: }
|