001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/control/ajax/WebEvent.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 // $Id: WebEvent.java 21672 2009-12-29 08:44:20Z apoth $ 046 package org.deegree.enterprise.control.ajax; 047 048 // JDK 1.3 049 import java.lang.reflect.Method; 050 import java.lang.reflect.Type; 051 import java.util.EventObject; 052 import java.util.Map; 053 054 import javax.servlet.ServletContext; 055 import javax.servlet.http.HttpServletRequest; 056 import javax.servlet.http.HttpSession; 057 058 import org.deegree.framework.util.KVP2Map; 059 060 /** 061 * 062 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 063 * 064 * @version $Revision: 21672 $ 065 */ 066 public class WebEvent extends EventObject { 067 /** 068 * 069 */ 070 private static final long serialVersionUID = 706936738051288390L; 071 072 private ServletContext servletContext; 073 074 protected String bean; 075 076 private Map<String,String> param; 077 078 /** 079 * Creates a new WebEvent object. 080 * 081 * @param servletContext 082 * @param request 083 * @param bean 084 */ 085 WebEvent( ServletContext servletContext, HttpServletRequest request, String bean ) { 086 super( request ); 087 this.bean = bean; 088 this.servletContext = servletContext; 089 param = KVP2Map.toMap( this._getRequest() ); 090 } 091 092 /** 093 * @return the parameters 094 */ 095 @SuppressWarnings("unchecked") 096 public Map getParameter() { 097 return param; 098 } 099 100 /** 101 * @return the document path 102 */ 103 public String getDocumentPath() { 104 return this._getRequest().getRequestURI(); 105 } 106 107 /** 108 * @return the request user 109 */ 110 public RequestUser getRequestUser() { 111 return this._getRequestUser( this._getRequest() ); 112 } 113 114 /** 115 * 116 * @return requests character encoding 117 */ 118 public String getCharacterEncoding() { 119 return _getRequest().getCharacterEncoding(); 120 } 121 122 /** 123 * 124 * @return session assigned to event 125 */ 126 public HttpSession getSession() { 127 return _getRequest().getSession(); 128 } 129 130 /** 131 * 132 * @param relativePath 133 * @return absolute path based on servlet context 134 */ 135 public String getAbsolutePath( String relativePath ) { 136 return servletContext.getRealPath( relativePath ); 137 } 138 139 /** 140 * 141 * @return request parameter mapped to a bean class 142 */ 143 public Object getAsBean() throws Exception { 144 Map<String,String> param = KVP2Map.toMap( this._getRequest() ); 145 Class<?> clzz = Class.forName( bean ); 146 Object bean = clzz.newInstance(); 147 Method[] methods = clzz.getMethods(); 148 for ( Method method : methods ) { 149 if ( method.getName().startsWith( "set" ) ) { 150 String var = method.getName().substring( 3, method.getName().length() ); 151 Object val = param.get( var.toUpperCase() ); 152 Type type = method.getGenericParameterTypes()[0]; 153 method.invoke( bean, ((Class<?>)type).cast( val ) ); 154 } 155 } 156 return bean; 157 } 158 159 160 @Override 161 public String toString() { 162 return this.getClass().getName().concat( " [ " ).concat( getDocumentPath() ).concat( " ]" ); 163 } 164 165 private RequestUser _getRequestUser( HttpServletRequest request ) { 166 return new RequestUser( request ); 167 } 168 169 private HttpServletRequest _getRequest() { 170 return (HttpServletRequest) getSource(); 171 } 172 173 }