    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/enterprise/control/ajax/JSONEvent.java $
    002    /*----------------    FILE HEADER  ------------------------------------------
    003     This file is part of deegree.
    004     Copyright (C) 2001-2008 by:
    005     Department of Geography, University of Bonn
    006     http://www.giub.uni-bonn.de/deegree/
    007     lat/lon GmbH
    008     http://www.lat-lon.de
    009
    010     This library is free software; you can redistribute it and/or
    011     modify it under the terms of the GNU Lesser General Public
    012     License as published by the Free Software Foundation; either
    013     version 2.1 of the License, or (at your option) any later version.
    014     This library is distributed in the hope that it will be useful,
    015     but WITHOUT ANY WARRANTY; without even the implied warranty of
    016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    017     Lesser General Public License for more details.
    018     You should have received a copy of the GNU Lesser General Public
    019     License along with this library; if not, write to the Free Software
    020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    021     Contact:
    022
    023     Andreas Poth
    024     lat/lon GmbH
    025     Aennchenstr. 19
    026     53177 Bonn
    027     Germany
    028     E-Mail: poth@lat-lon.de
    029
    030     Prof. Dr. Klaus Greve
    031     Department of Geography
    032     University of Bonn
    033     Meckenheimer Allee 166
    034     53115 Bonn
    035     Germany
    036     E-Mail: greve@giub.uni-bonn.de
    037     ---------------------------------------------------------------------------*/
    038
    039    package org.deegree.enterprise.control.ajax;
    040
    041    import java.io.IOException;
    042    import java.io.InputStreamReader;
    043    import java.lang.reflect.Method;
    044    import java.lang.reflect.Type;
    045    import java.util.Map;
    046
    047    import javax.servlet.ServletContext;
    048    import javax.servlet.ServletException;
    049    import javax.servlet.http.HttpServletRequest;
    050
    051    import org.deegree.framework.log.ILogger;
    052    import org.deegree.framework.log.LoggerFactory;
    053    import org.deegree.framework.util.FileUtils;
    054    import org.stringtree.json.JSONReader;
    055
    056    /**
    057     *
    058     *
    059     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    060     * @author last edited by: $Author: jmays $
    061     *
    062     * @version. $Revision: 23820 $, $Date: 2010-04-26 08:17:25 +0200 (Mo, 26. Apr 2010) $
    063     */
    064    public class JSONEvent extends WebEvent {
    065
    066        private static final long serialVersionUID = 6459849162427895987L;
    067
    068        private static final ILogger LOG = LoggerFactory.getLogger( JSONEvent.class );
    069
    070        private Map<String, ?> json;
    071
    072
    073
    074        /**
    075         *
    076         * @param servletContext
    077         * @param request
    078         * @throws ServletException
    079         */
    080        @SuppressWarnings("unchecked")
    081        JSONEvent( ServletContext servletContext, HttpServletRequest request ) throws ServletException {
    082            super( servletContext, request, null );
    083            JSONReader reader = new JSONReader();
    084            String string = null;
    085            try {
    086                LOG.logDebug( "request character encoding: " + request.getCharacterEncoding() );
    087
    088                InputStreamReader isr = null;
    089                if ( request.getCharacterEncoding() != null ) {
    090                    isr = new InputStreamReader( request.getInputStream(), request.getCharacterEncoding() );
    091                } else {
    092                    // always use UTF-8 because XMLHttpRequest normally uses this encoding
    093                    isr = new InputStreamReader( request.getInputStream(), "UTF-8" );
    094                }
    095                string = FileUtils.readTextFile( isr ).toString();
    096            } catch ( IOException e ) {
    097                LOG.logError( e.getMessage(), e );
    098                throw new ServletException( "can not parse json: " + json, e );
    099            }
    100            json = (Map<String, ?>) reader.read( string );
    101            LOG.logDebug( "request parameter: " +  json );
    102        }
    103
    104        @SuppressWarnings("unchecked")
    105        @Override
    106        public Map getParameter() {
    107            return json;
    108        }
    109
    110        /**
    111         *
    112         * @param bean
    113         */
    114        void setBean( String bean ) {
    115            this.bean = bean;
    116        }
    117
    118        @Override
    119        public Object getAsBean()
    120                                throws Exception {
    121            Class<?> clzz = Class.forName( bean );
    122            Object bean = clzz.newInstance();
    123            Method[] methods = clzz.getMethods();
    124            for ( Method method : methods ) {
    125                if ( method.getName().startsWith( "set" ) ) {
    126                    String var = method.getName().substring( 4, method.getName().length() );
    127                    var = method.getName().substring( 3, 4 ).toLowerCase() + var;
    128                    Object val = json.get( var );
    129                    Type type = method.getGenericParameterTypes()[0];
    130                    if ( val != null ) {
    131                        method.invoke( bean, ((Class<?>)type).cast( val ) );
    132                    }
    133                }
    134            }
    135            return bean;
    136        }
    137
    138    }