001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/enterprise/control/RequestDispatcher.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2006 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 a event out of a GET 060 * or POST requests. 061 * <P> 062 * 063 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a> 064 * 065 * @version $Revision: 7335 $ $Date: 2007-05-29 11:43:53 +0200 (Di, 29 Mai 2007) $ 066 */ 067 public class RequestDispatcher extends HttpServlet { 068 069 private static final long serialVersionUID = 1L; 070 071 private static String CONFIGURATION = "Handler.configFile"; 072 073 protected transient ApplicationHandler appHandler = null; 074 075 /** 076 * This method initializes the servlet. 077 * 078 * @param cfg 079 * the servlet configuration 080 * 081 * @throws ServletException 082 * an exception 083 */ 084 @Override 085 public void init( ServletConfig cfg ) 086 throws ServletException { 087 super.init( cfg ); 088 089 try { 090 String url = null; 091 092 String s = getInitParameter( CONFIGURATION ); 093 if ( new File( s ).isAbsolute() ) { 094 url = s; 095 } else { 096 url = getServletContext().getRealPath( s ); 097 } 098 099 this.appHandler = new ApplicationHandler( url ); 100 } catch ( Exception e ) { 101 e.printStackTrace(); 102 } 103 } 104 105 /** 106 * 107 * 108 * @param request 109 * @param response 110 * 111 * @throws ServletException 112 * @throws IOException 113 */ 114 @Override 115 protected void service( HttpServletRequest request, HttpServletResponse response ) 116 throws ServletException, IOException { 117 // Map<String, String[]> requestParameters= request.getParameterMap(); 118 // FormEvent event = null; 119 // if( requestParameters.size() > 0 ){ 120 // Map<String, String> params = new HashMap<String, String>(); 121 // Set<String> keys = requestParameters.keySet(); 122 // for( String key : keys){ 123 // String[] values = requestParameters.get( key ); 124 // //maybe put in more keys with _iterator appended? 125 // if( values.length >= 1 ){ 126 // params.put( key, values[0] ); 127 // } 128 // } 129 // event = new WebEvent( params ); 130 // } else { 131 // event = new WebEvent( request.getInputStream() ); 132 // } 133 134 // create event out of request 135 FormEvent event = createEvent( request ); 136 137 // deliver event to application handler 138 deliverEvent( event ); 139 140 // get next page from request attribute 141 String nextPage = (String) request.getAttribute( "next" ); 142 143 // show error page if next page is null or an error occured 144 nextPage = "/" + ( ( nextPage == null ) ? "error.jsp" : nextPage ); 145 146 if ( request.getAttribute( "javax.servlet.jsp.jspException" ) != null ) { 147 nextPage = "/error.jsp"; 148 } 149 150 // call request dispatcher 151 getServletConfig().getServletContext().getRequestDispatcher( nextPage ).forward( request, 152 response ); 153 event = null; 154 } 155 156 /** 157 * 158 * 159 * @param original request from the service 160 * 161 * @return a new WebEvent which wraps the request into a {@link ServletRequestWrapper}. 162 */ 163 protected FormEvent createEvent( HttpServletRequest request ) { 164 return new WebEvent( new ServletRequestWrapper( request ) ); 165 } 166 167 /** 168 * 169 * 170 * @param event 171 */ 172 protected void deliverEvent( FormEvent event ) { 173 if ( appHandler == null ) { 174 try { 175 String url = null; 176 177 String s = getInitParameter( CONFIGURATION ); 178 if ( new File( s ).isAbsolute() ) { 179 url = s; 180 } else { 181 url = getServletContext().getRealPath( s ); 182 } 183 184 this.appHandler = new ApplicationHandler( url ); 185 } catch ( Exception e ) { 186 e.printStackTrace(); 187 } 188 } 189 this.appHandler.actionPerformed( event ); 190 } 191 }