001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/standard/context/control/ContextLoadListener.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.portal.standard.context.control;
038    
039    import java.io.IOException;
040    import java.util.List;
041    
042    import org.deegree.enterprise.control.FormEvent;
043    import org.deegree.enterprise.control.RPCMember;
044    import org.deegree.enterprise.control.RPCMethodCall;
045    import org.deegree.enterprise.control.RPCParameter;
046    import org.deegree.enterprise.control.RPCStruct;
047    import org.deegree.enterprise.control.RPCUtils;
048    import org.deegree.enterprise.control.RPCWebEvent;
049    import org.deegree.framework.log.ILogger;
050    import org.deegree.framework.log.LoggerFactory;
051    import org.deegree.i18n.Messages;
052    import org.deegree.portal.PortalException;
053    
054    /**
055     * This listener generates a list of availavle ViewContexts. The only parameter passed is the user name. Currently only
056     * .xml files are being accepted as contexts and it's also also assumed that those are available under
057     * WEB-INF/xml/users/some_user user directories
058     * 
059     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
060     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
061     * @author last edited by: $Author: jmays $
062     * 
063     * @version $Revision: 21440 $, $Date: 2009-12-14 16:46:38 +0100 (Mo, 14 Dez 2009) $
064     */
065    public class ContextLoadListener extends AbstractContextListener {
066    
067        private static final ILogger LOG = LoggerFactory.getLogger( ContextLoadListener.class );
068    
069        /*
070         * (non-Javadoc)
071         * 
072         * @see org.deegree.enterprise.control.WebListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
073         */
074        @Override
075        public void actionPerformed( FormEvent event ) {
076    
077            RPCWebEvent rpc = (RPCWebEvent) event;
078            try {
079                validate( rpc );
080            } catch ( PortalException e ) {
081                LOG.logError( e.getMessage(), e );
082                gotoErrorPage( Messages.getMessage( "IGEO_STD_CNTXT_INVALID_RPC", "ContextLoad", e.getMessage() ) );
083                return;
084            }
085    
086            String userName = extractUserName( rpc );
087            List<String> contextList = getContextList( userName );
088            String userStartContext;
089            try {
090                userStartContext = getUsersStartContext( userName );
091            } catch ( IOException e ) {
092                LOG.logError( e.getMessage(), e );
093                gotoErrorPage( Messages.getMessage( "IGEO_STD_SEC_ERROR_STARTCONTEXT" ) );
094                return;
095            }
096    
097            getRequest().setAttribute( "CONTEXT_LIST", contextList );
098            getRequest().setAttribute( "USER", userName );
099            getRequest().setAttribute( "STARTCONTEXT", userStartContext );
100        }
101    
102        /**
103         * reads the session ID from the passed RPC and gets the assigned user name from a authentification service
104         * 
105         * @param event
106         * @return the user name
107         */
108        private String extractUserName( RPCWebEvent event ) {
109            RPCMethodCall mc = event.getRPCMethodCall();
110            RPCParameter[] pars = mc.getParameters();
111            RPCStruct struct = (RPCStruct) pars[0].getValue();
112    
113            // get map context value
114            String name = "default";
115            try {
116                name = getUserName( RPCUtils.getRpcPropertyAsString( struct, "sessionID" ) );
117                if ( name == null ) {
118                    name = "default";
119                }
120            } catch ( Exception e ) {
121            }
122            return name;
123        }
124    
125        /**
126         * validates if the passed RPC call containes the required variables
127         * 
128         * @param rpc
129         * @throws PortalException
130         */
131        private void validate( RPCWebEvent rpc )
132                                throws PortalException {
133            RPCMethodCall mc = rpc.getRPCMethodCall();
134            RPCParameter param = mc.getParameters()[0];
135            RPCStruct struct = (RPCStruct) param.getValue();
136            RPCMember sessionID = struct.getMember( "sessionID" );
137            if ( sessionID == null ) {
138                throw new PortalException( Messages.getMessage( "IGEO_STD_CNTXT_MISSING_PARAM", "sessionID", "ContextLoad" ) );
139            }
140        }
141    
142    }