001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/servlet/RedirectServlet.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 037 package org.deegree.enterprise.servlet; 038 039 import static org.deegree.framework.log.LoggerFactory.getLogger; 040 041 import java.io.IOException; 042 import java.util.Enumeration; 043 import java.util.TreeMap; 044 045 import javax.servlet.ServletConfig; 046 import javax.servlet.ServletException; 047 import javax.servlet.ServletRequest; 048 import javax.servlet.ServletResponse; 049 import javax.servlet.http.HttpServlet; 050 import javax.servlet.http.HttpServletRequest; 051 import javax.servlet.http.HttpServletResponse; 052 053 import org.deegree.framework.log.ILogger; 054 055 /** 056 * To configure the <code>RedirectServlet</code>, have the tomcat match everything (/*) for this 057 * servlet. Add the following init parameters: 058 * 059 * <ul> 060 * <li>defaultLocation: set it to the location to be used if no rule matches</li> 061 * <li>rule<xxx>: set the value to a String to match, followed by ",", followed by 062 * the String to replace it with</li> 063 * </ul> 064 * 065 * Using a log4j logging level of debug will output all redirections that take place. 066 * 067 * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a> 068 * @author last edited by: $Author: mschneider $ 069 * 070 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $ 071 */ 072 public class RedirectServlet extends HttpServlet { 073 074 private static final long serialVersionUID = -8507813259962496365L; 075 076 private static final ILogger LOG = getLogger( RedirectServlet.class ); 077 078 private ServletConfig config; 079 080 private TreeMap<String, String> redirections; 081 082 private TreeMap<String, String> redirectionOrder; 083 084 private String defaultLocation; 085 086 @Override 087 public void destroy() { 088 config = null; 089 } 090 091 @Override 092 public ServletConfig getServletConfig() { 093 return config; 094 } 095 096 @Override 097 public String getServletInfo() { 098 return "Simple redirection servlet"; 099 } 100 101 @Override 102 public void init( ServletConfig config ) 103 throws ServletException { 104 this.config = config; 105 redirections = new TreeMap<String, String>(); 106 redirectionOrder = new TreeMap<String, String>(); 107 108 Enumeration<?> enumer = config.getInitParameterNames(); 109 while ( enumer.hasMoreElements() ) { 110 String name = (String) enumer.nextElement(); 111 if ( name.equalsIgnoreCase( "defaultlocation" ) ) { 112 defaultLocation = config.getInitParameter( name ); 113 } 114 115 if ( name.toLowerCase().startsWith( "rule" ) ) { 116 String[] r = config.getInitParameter( name ).split( "," ); 117 redirections.put( r[0], r[1] ); 118 redirectionOrder.put( name.toLowerCase(), r[0] ); 119 if ( LOG.isDebug() ) { 120 LOG.logDebug( "Adding rule " + r[0] + " -> " + r[1] ); 121 } 122 } 123 } 124 125 if ( defaultLocation == null ) { 126 LOG.logWarning( "No default location given. I hope your rules match all possible URLs." ); 127 } 128 } 129 130 @Override 131 public void service( ServletRequest request, ServletResponse response ) 132 throws ServletException, IOException { 133 HttpServletRequest r = (HttpServletRequest) request; 134 135 String req = r.getRequestURL().toString() + ( r.getQueryString() == null ? "" : ( "?" + r.getQueryString() ) ); 136 137 String location = defaultLocation; 138 139 // use the first match 140 for ( String rule : redirectionOrder.keySet() ) { 141 String p = redirectionOrder.get( rule ); 142 if ( req.indexOf( p ) != -1 ) { 143 LOG.logDebug( "Rule with key " + p + " matches." ); 144 location = req.replace( p, redirections.get( p ) ); 145 break; 146 } 147 } 148 149 if ( LOG.isDebug() ) { 150 LOG.logDebug( "Redirecting " + req + " to " + location ); 151 } 152 153 // do the redirect 154 HttpServletResponse httpResponse = ( (HttpServletResponse) response ); 155 httpResponse.setHeader( "Location", location ); 156 httpResponse.setStatus( 302 ); 157 } 158 159 }