001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/enterprise/control/RequestDispatcher.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2008 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/deegree/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 This library is free software; you can redistribute it and/or 012 modify it under the terms of the GNU Lesser General Public 013 License as published by the Free Software Foundation; either 014 version 2.1 of the License, or (at your option) any later version. 015 016 This library is distributed in the hope that it will be useful, 017 but WITHOUT ANY WARRANTY; without even the implied warranty of 018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 Lesser General Public License for more details. 020 021 You should have received a copy of the GNU Lesser General Public 022 License along with this library; if not, write to the Free Software 023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 025 Contact: 026 027 Andreas Poth 028 lat/lon GmbH 029 Aennchenstr. 19 030 53115 Bonn 031 Germany 032 E-Mail: poth@lat-lon.de 033 034 Prof. Dr. Klaus Greve 035 Department of Geography 036 University of Bonn 037 Meckenheimer Allee 166 038 53115 Bonn 039 Germany 040 E-Mail: greve@giub.uni-bonn.de 041 042 043 ---------------------------------------------------------------------------*/ 044 045 package org.deegree.enterprise.control; 046 047 import java.io.File; 048 import java.io.IOException; 049 050 import javax.servlet.ServletConfig; 051 import javax.servlet.ServletException; 052 import javax.servlet.http.HttpServlet; 053 import javax.servlet.http.HttpServletRequest; 054 import javax.servlet.http.HttpServletResponse; 055 056 import org.deegree.enterprise.servlet.ServletRequestWrapper; 057 058 /** 059 * This is a <code>RequestDispatcher</code> which creates an event out of a GET or POST request. 060 * 061 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a> 062 * @author last edited by: $Author: mays$ 063 * 064 * @version $Revision: 9338 $ $Date: 2007-12-27 13:31:31 +0100 (Do, 27 Dez 2007) $ 065 */ 066 public class RequestDispatcher extends HttpServlet { 067 068 private static final long serialVersionUID = 1L; 069 070 private static String CONFIGURATION = "Handler.configFile"; 071 072 protected transient ApplicationHandler appHandler = null; 073 074 /** 075 * This method initializes the servlet. 076 * 077 * @param cfg 078 * the servlet configuration 079 * @throws ServletException 080 * an exception 081 */ 082 @Override 083 public void init( ServletConfig cfg ) 084 throws ServletException { 085 super.init( cfg ); 086 087 try { 088 String url = null; 089 090 String s = getInitParameter( CONFIGURATION ); 091 if ( new File( s ).isAbsolute() ) { 092 url = s; 093 } else { 094 url = getServletContext().getRealPath( s ); 095 } 096 097 this.appHandler = new ApplicationHandler( url ); 098 } catch ( Exception e ) { 099 e.printStackTrace(); 100 } 101 } 102 103 /** 104 * 105 * @param request 106 * @param response 107 * @throws ServletException 108 * @throws IOException 109 */ 110 @Override 111 protected void service( HttpServletRequest request, HttpServletResponse response ) 112 throws ServletException, IOException { 113 // Map<String, String[]> requestParameters= request.getParameterMap(); 114 // FormEvent event = null; 115 // if( requestParameters.size() > 0 ){ 116 // Map<String, String> params = new HashMap<String, String>(); 117 // Set<String> keys = requestParameters.keySet(); 118 // for( String key : keys){ 119 // String[] values = requestParameters.get( key ); 120 // //maybe put in more keys with _iterator appended? 121 // if( values.length >= 1 ){ 122 // params.put( key, values[0] ); 123 // } 124 // } 125 // event = new WebEvent( params ); 126 // } else { 127 // event = new WebEvent( request.getInputStream() ); 128 // } 129 130 // create event out of request 131 FormEvent event = createEvent( request ); 132 133 // deliver event to application handler 134 deliverEvent( event ); 135 136 // get next page from request attribute 137 String nextPage = (String) request.getAttribute( "next" ); 138 139 // TODO: implement handling of attribute "alternativeNext" ! something like: 140 // if (next != null) { check whether the path points to an existing file. if not, use "alternativeNext"} 141 142 // show error page if next page is null or an error occured 143 nextPage = "/" + ( ( nextPage == null ) ? "error.jsp" : nextPage ); 144 145 if ( request.getAttribute( "javax.servlet.jsp.jspException" ) != null ) { 146 nextPage = "/error.jsp"; 147 } 148 149 // call request dispatcher 150 getServletConfig().getServletContext().getRequestDispatcher( nextPage ).forward( request, response ); 151 event = null; 152 } 153 154 /** 155 * 156 * @param original 157 * request from the service 158 * @return a new WebEvent which wraps the request into a {@link ServletRequestWrapper}. 159 */ 160 protected FormEvent createEvent( HttpServletRequest request ) { 161 return new WebEvent( new ServletRequestWrapper( request ) ); 162 } 163 164 /** 165 * 166 * @param event 167 */ 168 protected void deliverEvent( FormEvent event ) { 169 if ( appHandler == null ) { 170 try { 171 String url = null; 172 173 String s = getInitParameter( CONFIGURATION ); 174 if ( new File( s ).isAbsolute() ) { 175 url = s; 176 } else { 177 url = getServletContext().getRealPath( s ); 178 } 179 180 this.appHandler = new ApplicationHandler( url ); 181 } catch ( Exception e ) { 182 e.printStackTrace(); 183 } 184 } 185 this.appHandler.actionPerformed( event ); 186 } 187 }