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 org.apache.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.api.core.Engine;
024import org.apache.wiki.api.spi.Wiki;
025import org.apache.wiki.url.URLConstructor;
026
027import javax.servlet.RequestDispatcher;
028import javax.servlet.ServletConfig;
029import javax.servlet.ServletException;
030import javax.servlet.http.HttpServlet;
031import javax.servlet.http.HttpServletRequest;
032import javax.servlet.http.HttpServletResponse;
033import java.io.IOException;
034
035
036/**
037 * This provides a master servlet for dealing with short urls. It mostly does redirects to the proper JSP pages.
038 * It also intercepts the servlet shutdown events and uses it to signal wiki shutdown.
039 *
040 * @since 2.2
041 */
042public class WikiServlet extends HttpServlet {
043
044    private static final long serialVersionUID = 3258410651167633973L;
045    private Engine m_engine;
046    private static final Logger log = LogManager.getLogger( WikiServlet.class.getName() );
047
048    /**
049     * {@inheritDoc}
050     */
051    @Override
052    public void init( final ServletConfig config ) throws ServletException {
053        super.init( config );
054        m_engine = Wiki.engine().find( config );
055        log.info( "WikiServlet initialized." );
056    }
057
058    /**
059     * Destroys the WikiServlet; called by the servlet container when shutting down the webapp. This method calls the
060     * protected method {@link WikiEngine#shutdown()}, which sends {@link org.apache.wiki.event.WikiEngineEvent#SHUTDOWN}
061     * events to registered listeners.
062     *
063     * @see javax.servlet.GenericServlet#destroy()
064     */
065    @Override
066    public void destroy() {
067        log.info( "WikiServlet shutdown." );
068        m_engine.shutdown();
069        super.destroy();
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public void doPost( final HttpServletRequest req, final HttpServletResponse res ) throws IOException, ServletException {
077        doGet( req, res );
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public void doGet( final HttpServletRequest req, final HttpServletResponse res ) throws IOException, ServletException {
085        String pageName = URLConstructor.parsePageFromURL( req, m_engine.getContentEncoding() );
086
087        log.info( "Request for page: {}", pageName );
088        if( pageName == null ) {
089            pageName = m_engine.getFrontPage(); // FIXME: Add special pages as well
090        }
091
092        final String jspPage = m_engine.getManager( URLConstructor.class ).getForwardPage( req );
093        final RequestDispatcher dispatcher = req.getRequestDispatcher( "/" + jspPage + "?page=" +
094                                                                       m_engine.encodeName( pageName ) + "&" + req.getQueryString() );
095
096        dispatcher.forward( req, res );
097    }
098
099}