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