001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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 04:31:31 -0800 (Thu, 27 Dec 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) throws ServletException {
084: super .init(cfg);
085:
086: try {
087: String url = null;
088:
089: String s = getInitParameter(CONFIGURATION);
090: if (new File(s).isAbsolute()) {
091: url = s;
092: } else {
093: url = getServletContext().getRealPath(s);
094: }
095:
096: this .appHandler = new ApplicationHandler(url);
097: } catch (Exception e) {
098: e.printStackTrace();
099: }
100: }
101:
102: /**
103: *
104: * @param request
105: * @param response
106: * @throws ServletException
107: * @throws IOException
108: */
109: @Override
110: protected void service(HttpServletRequest request,
111: HttpServletResponse response) throws ServletException,
112: 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(
151: nextPage).forward(request, response);
152: event = null;
153: }
154:
155: /**
156: *
157: * @param original
158: * request from the service
159: * @return a new WebEvent which wraps the request into a {@link ServletRequestWrapper}.
160: */
161: protected FormEvent createEvent(HttpServletRequest request) {
162: return new WebEvent(new ServletRequestWrapper(request));
163: }
164:
165: /**
166: *
167: * @param event
168: */
169: protected void deliverEvent(FormEvent event) {
170: if (appHandler == null) {
171: try {
172: String url = null;
173:
174: String s = getInitParameter(CONFIGURATION);
175: if (new File(s).isAbsolute()) {
176: url = s;
177: } else {
178: url = getServletContext().getRealPath(s);
179: }
180:
181: this .appHandler = new ApplicationHandler(url);
182: } catch (Exception e) {
183: e.printStackTrace();
184: }
185: }
186: this.appHandler.actionPerformed(event);
187: }
188: }
|