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 net.sf.ehcache.CacheManager; 022import org.apache.logging.log4j.LogManager; 023import org.apache.logging.log4j.Logger; 024import org.apache.wiki.api.core.Engine; 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. It also intercepts the 039 * 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#shutdown()}, which sends {@link org.apache.wiki.event.WikiEngineEvent#SHUTDOWN} 062 * events to registered listeners. 063 * 064 * @see javax.servlet.GenericServlet#destroy() 065 */ 066 @Override 067 public void destroy() { 068 log.info( "WikiServlet shutdown." ); 069 CacheManager.getInstance().shutdown(); 070 m_engine.shutdown(); 071 super.destroy(); 072 } 073 074 /** 075 * {@inheritDoc} 076 */ 077 @Override 078 public void doPost( final HttpServletRequest req, final HttpServletResponse res ) throws IOException, ServletException { 079 doGet( req, res ); 080 } 081 082 /** 083 * {@inheritDoc} 084 */ 085 @Override 086 public void doGet( final HttpServletRequest req, final HttpServletResponse res ) throws IOException, ServletException { 087 String pageName = URLConstructor.parsePageFromURL( req, m_engine.getContentEncoding() ); 088 089 log.info( "Request for page: " + pageName ); 090 if( pageName == null ) { 091 pageName = m_engine.getFrontPage(); // FIXME: Add special pages as well 092 } 093 094 final String jspPage = m_engine.getManager( URLConstructor.class ).getForwardPage( req ); 095 final RequestDispatcher dispatcher = req.getRequestDispatcher( "/" + jspPage + "?page=" + 096 m_engine.encodeName( pageName ) + "&" + req.getQueryString() ); 097 098 dispatcher.forward( req, res ); 099 } 100 101}