001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/enterprise/control/RequestDispatcher.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.control;
038
039 import java.io.File;
040 import java.io.IOException;
041
042 import javax.servlet.ServletConfig;
043 import javax.servlet.ServletException;
044 import javax.servlet.http.HttpServlet;
045 import javax.servlet.http.HttpServletRequest;
046 import javax.servlet.http.HttpServletResponse;
047
048 import org.deegree.enterprise.servlet.ServletRequestWrapper;
049
050 /**
051 * This is a <code>RequestDispatcher</code> which creates an event out of a GET or POST request.
052 *
053 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
054 * @author last edited by: $Author: mays$
055 *
056 * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
057 */
058 public class RequestDispatcher extends HttpServlet {
059
060 private static final long serialVersionUID = 1L;
061
062 private static String CONFIGURATION = "Handler.configFile";
063
064 /** */
065 protected transient ApplicationHandler appHandler = null;
066
067 /**
068 * This method initializes the servlet.
069 *
070 * @param cfg
071 * the servlet configuration
072 * @throws ServletException
073 * an exception
074 */
075 @Override
076 public void init( ServletConfig cfg )
077 throws ServletException {
078 super.init( cfg );
079
080 try {
081 String url = null;
082
083 String s = getInitParameter( CONFIGURATION );
084 if ( new File( s ).isAbsolute() ) {
085 url = s;
086 } else {
087 url = getServletContext().getRealPath( s );
088 }
089
090 this.appHandler = new ApplicationHandler( url );
091 } catch ( Exception e ) {
092 e.printStackTrace();
093 }
094 }
095
096 /**
097 *
098 * @param request
099 * @param response
100 * @throws ServletException
101 * @throws IOException
102 */
103 @Override
104 protected void service( HttpServletRequest request, HttpServletResponse response )
105 throws ServletException, IOException {
106 // Map<String, String[]> requestParameters= request.getParameterMap();
107 // FormEvent event = null;
108 // if( requestParameters.size() > 0 ){
109 // Map<String, String> params = new HashMap<String, String>();
110 // Set<String> keys = requestParameters.keySet();
111 // for( String key : keys){
112 // String[] values = requestParameters.get( key );
113 // //maybe put in more keys with _iterator appended?
114 // if( values.length >= 1 ){
115 // params.put( key, values[0] );
116 // }
117 // }
118 // event = new WebEvent( params );
119 // } else {
120 // event = new WebEvent( request.getInputStream() );
121 // }
122
123 // create event out of request
124 FormEvent event = createEvent( request );
125
126 // deliver event to application handler
127 deliverEvent( event );
128
129 // get next page from request attribute
130 String nextPage = (String) request.getAttribute( "next" );
131
132 // TODO: implement handling of attribute "alternativeNext" ! something like:
133 // if (next != null) { check whether the path points to an existing file. if not, use
134 // "alternativeNext"}
135
136 // show error page if next page is null or an error occured
137 nextPage = "/" + ( ( nextPage == null ) ? "error.jsp" : nextPage );
138
139 if ( request.getAttribute( "javax.servlet.jsp.jspException" ) != null ) {
140 nextPage = "/error.jsp";
141 }
142
143 // call request dispatcher
144 getServletConfig().getServletContext().getRequestDispatcher( nextPage ).forward( request, response );
145 event = null;
146 }
147
148 /**
149 *
150 * @param request
151 * request from the service
152 * @return a new WebEvent which wraps the request into a {@link ServletRequestWrapper}.
153 */
154 protected FormEvent createEvent( HttpServletRequest request ) {
155 return new WebEvent( new ServletRequestWrapper( request ) );
156 }
157
158 /**
159 *
160 * @param event
161 */
162 protected void deliverEvent( FormEvent event ) {
163 if ( appHandler == null ) {
164 try {
165 String url = null;
166
167 String s = getInitParameter( CONFIGURATION );
168 if ( new File( s ).isAbsolute() ) {
169 url = s;
170 } else {
171 url = getServletContext().getRealPath( s );
172 }
173
174 this.appHandler = new ApplicationHandler( url );
175 } catch ( Exception e ) {
176 e.printStackTrace();
177 }
178 }
179 this.appHandler.actionPerformed( event );
180 }
181 }