001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/control/ajax/ApplicationHandler.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 53177 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 package org.deegree.enterprise.control.ajax; 045 046 import java.io.File; 047 import java.io.IOException; 048 import java.net.MalformedURLException; 049 import java.util.ArrayList; 050 import java.util.HashMap; 051 import java.util.List; 052 053 import javax.servlet.ServletContext; 054 import javax.servlet.ServletException; 055 import javax.servlet.http.HttpServletRequest; 056 057 import org.deegree.datatypes.parameter.GeneralOperationParameterIm; 058 import org.deegree.datatypes.parameter.ParameterValueIm; 059 import org.deegree.enterprise.servlet.ServletRequestWrapper; 060 import org.deegree.framework.log.ILogger; 061 import org.deegree.framework.log.LoggerFactory; 062 import org.deegree.framework.xml.NamespaceContext; 063 import org.deegree.framework.xml.XMLFragment; 064 import org.deegree.framework.xml.XMLParsingException; 065 import org.deegree.framework.xml.XMLTools; 066 import org.deegree.framework.xml.XSLTDocument; 067 import org.deegree.ogcbase.CommonNamespaces; 068 import org.w3c.dom.Element; 069 import org.w3c.dom.Node; 070 import org.xml.sax.SAXException; 071 072 /** 073 * Handler for all web events. 074 * 075 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 076 * 077 * @version $Revision: 24033 $, $Date: 2010-04-30 10:21:00 +0200 (Fr, 30 Apr 2010) $ 078 */ 079 public class ApplicationHandler { 080 081 private static final ILogger LOG = LoggerFactory.getLogger( ApplicationHandler.class ); 082 083 private static final HashMap<String, Class<?>> handler = new HashMap<String, Class<?>>(); 084 085 private static final HashMap<String, XSLTDocument> xsl = new HashMap<String, XSLTDocument>(); 086 087 private static final HashMap<String, List<ParameterValueIm>> handlerParam = new HashMap<String, List<ParameterValueIm>>(); 088 089 private static final HashMap<String, String> nextPages = new HashMap<String, String>(); 090 091 private static final HashMap<String, String> beans = new HashMap<String, String>(); 092 093 private static final String EVENT = "event"; 094 095 private static final String NAME = "name"; 096 097 private static final String CLASS = "class"; 098 099 private static final String XSLT = "xslt"; 100 101 private static final String NEXT = "next"; 102 103 private static final String BEAN = "bean"; 104 105 /** 106 * Creates a new ApplicationHandler object. 107 * 108 * @param configFile 109 * @throws Exception 110 */ 111 public ApplicationHandler( String configFile ) throws Exception { 112 ApplicationHandler.initHandler( configFile ); 113 } 114 115 /** 116 * Handles all web action events. Calls the specified listener using the mapping defined in control.xml file. 117 * 118 * @param servletContext 119 * @param request 120 * @param responseHandler 121 * @throws ServletException 122 */ 123 public void actionPerformed( ServletContext servletContext, HttpServletRequest request, 124 ResponseHandler responseHandler ) 125 throws ServletException { 126 127 WebEvent event = null; 128 String actionName = request.getParameter( "action" ); 129 if ( actionName != null && actionName.trim().length() > 0 ) { 130 event = new WebEvent( servletContext, new ServletRequestWrapper( request ), beans.get( actionName ) ); 131 // handle HTTP GET which is assumed to be KVP encoded 132 LOG.logDebug( "KVP encoded Actionname: " + actionName ); 133 } else { 134 // handle HTTP POST which is assumed to be JSON encoded 135 event = new JSONEvent( servletContext, request ); 136 actionName = (String) event.getParameter().get( "className" ); 137 if ( actionName == null ) { 138 actionName = (String) event.getParameter().get( "action" ); 139 } 140 ( (JSONEvent) event ).setBean( beans.get( actionName ) ); 141 LOG.logDebug( "HTTP POST/JSON action/class name: " + actionName ); 142 } 143 try { 144 this.delegateToHelper( actionName, event, responseHandler ); 145 } catch ( Exception e ) { 146 LOG.logError( e.getMessage(), e ); 147 throw new ServletException( "no handler available for action: " + actionName, e ); 148 } 149 150 } 151 152 /** 153 * 154 * @param action 155 * @param event 156 * @param responseHandler 157 * @throws Exception 158 */ 159 protected void delegateToHelper( String action, WebEvent event, ResponseHandler responseHandler ) 160 throws Exception { 161 action = action.trim(); 162 Class<?> cls = ApplicationHandler.handler.get( action ); 163 AbstractListener helper = (AbstractListener) cls.newInstance(); 164 helper.setInitParameterList( handlerParam.get( action ) ); 165 helper.setNextPage( nextPages.get( action ) ); 166 helper.handle( event, responseHandler ); 167 } 168 169 /** 170 * 171 * 172 * @param configFile 173 * 174 * @throws IOException 175 * @throws MalformedURLException 176 * @throws SAXException 177 */ 178 private static void initHandler( String configFile ) 179 throws ServletException { 180 LOG.logInfo( "Reading event handler configuration file:" + configFile ); 181 // Read resource into Document... 182 XMLFragment xml; 183 try { 184 xml = new XMLFragment( new File( configFile ).toURI().toURL() ); 185 } catch ( Exception e ) { 186 LOG.logError( e.getMessage(), e ); 187 throw new ServletException( e ); 188 } 189 190 List<Node> nodes; 191 try { 192 nodes = XMLTools.getNodes( xml.getRootElement(), EVENT, CommonNamespaces.getNamespaceContext() ); 193 } catch ( XMLParsingException e ) { 194 LOG.logError( e.getMessage(), e ); 195 throw new ServletException( e ); 196 } 197 for ( Node node : nodes ) { 198 199 String name = XMLTools.getAttrValue( node, null, NAME, null ); 200 String cls = XMLTools.getAttrValue( node, null, CLASS, null ); 201 String nextPage = XMLTools.getAttrValue( node, null, NEXT, null ); 202 String bean = XMLTools.getAttrValue( node, null, BEAN, null ); 203 nextPages.put( name.trim(), nextPage ); 204 beans.put( name.trim(), bean ); 205 String xslt = XMLTools.getAttrValue( node, null, XSLT, null ); 206 if ( xslt != null ) { 207 XSLTDocument xsltDoc; 208 try { 209 xsltDoc = new XSLTDocument( xml.resolve( xslt ) ); 210 } catch ( Exception e ) { 211 LOG.logError( e.getMessage(), e ); 212 throw new ServletException( e ); 213 } 214 xsl.put( name.trim(), xsltDoc ); 215 } 216 217 Class<?> clscls = null; 218 try { 219 clscls = Class.forName( cls ); 220 handler.put( name.trim(), clscls ); 221 List<ParameterValueIm> pvList = parseParameters( node ); 222 handlerParam.put( name.trim(), pvList ); 223 LOG.logInfo( "Handler '" + clscls + "' bound to event '" + name + "'" ); 224 } catch ( Exception ex ) { 225 LOG.logError( "No handler '" + cls + "' specified for event '" + name + "'", ex ); 226 throw new ServletException( "No handler class specified for event:" + name + " " + cls, ex ); 227 } 228 } 229 } 230 231 /** 232 * several parameters can be passed to each Listener by adding 233 * 234 * <pre> 235 * <parameter> 236 * <name>aName</name> 237 * <value>aValue</value> 238 * </parameter> 239 * </pre> 240 * 241 * sections to the corresponding <event> element. 242 * 243 * @param node 244 * @return a List of ParameterValueIm 245 * @throws XMLParsingException 246 */ 247 private static List<ParameterValueIm> parseParameters( Node node ) 248 throws XMLParsingException { 249 250 NamespaceContext nsc = CommonNamespaces.getNamespaceContext(); 251 List<Node> nodes = XMLTools.getNodes( node, "parameter", nsc ); 252 List<ParameterValueIm> pvs = new ArrayList<ParameterValueIm>(); 253 for ( int i = 0; i < nodes.size(); i++ ) { 254 Element element = (Element) nodes.get( i ); 255 String name = XMLTools.getRequiredNodeAsString( element, "name", nsc ); 256 String value = XMLTools.getRequiredNodeAsString( element, "value", nsc ); 257 GeneralOperationParameterIm descriptor = new GeneralOperationParameterIm( name, null, 1, 1 ); 258 pvs.add( new ParameterValueIm( descriptor, value ) ); 259 } 260 261 return pvs; 262 } 263 264 }