001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.     
018 */
019package org.apache.wiki;
020
021import javax.servlet.*;
022import javax.servlet.http.*;
023import java.io.*;
024
025import net.sf.ehcache.CacheManager;
026import org.apache.log4j.Logger;
027
028import org.apache.wiki.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 * @since 2.2
036 */
037public class WikiServlet extends HttpServlet {
038    private static final long serialVersionUID = 3258410651167633973L;
039    private WikiEngine m_engine;
040    static final Logger log = Logger.getLogger(WikiServlet.class.getName());
041
042    /**
043     * {@inheritDoc}
044     */
045    public void init(ServletConfig config)
046            throws ServletException {
047        super.init(config);
048
049        m_engine = WikiEngine.getInstance(config);
050
051        log.info("WikiServlet initialized.");
052    }
053
054    /**
055     * Destroys the WikiServlet; called by the servlet container
056     * when shutting down the webapp. This method calls the
057     * protected method {@link WikiEngine#shutdown()}, which
058     * sends {@link org.apache.wiki.event.WikiEngineEvent#SHUTDOWN}
059     * events to registered listeners.
060     *
061     * @see javax.servlet.GenericServlet#destroy()
062     */
063    public void destroy() {
064        log.info("WikiServlet shutdown.");
065        CacheManager.getInstance().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
092        String jspPage = m_engine.getURLConstructor().getForwardPage(req);
093
094        RequestDispatcher dispatcher = req.getRequestDispatcher("/" + jspPage + "?page="
095                + m_engine.encodeName(pageName) + "&" + req.getQueryString());
096
097        dispatcher.forward(req, res);
098    }
099}