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.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. It also intercepts the 038 * 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 = Logger.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 CacheManager.getInstance().shutdown(); 069 m_engine.shutdown(); 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}