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