001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_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.File;
040    import java.util.ArrayList;
041    import java.util.Arrays;
042    import java.util.List;
043    
044    import org.deegree.enterprise.control.FormEvent;
045    import org.deegree.enterprise.control.RPCMember;
046    import org.deegree.enterprise.control.RPCMethodCall;
047    import org.deegree.enterprise.control.RPCParameter;
048    import org.deegree.enterprise.control.RPCStruct;
049    import org.deegree.enterprise.control.RPCUtils;
050    import org.deegree.enterprise.control.RPCWebEvent;
051    import org.deegree.framework.log.ILogger;
052    import org.deegree.framework.log.LoggerFactory;
053    import org.deegree.i18n.Messages;
054    import org.deegree.portal.PortalException;
055    
056    /**
057     * This listener generates a list of availavle ViewContexts.
058     * The only parameter passed is the user name.Currently only
059     * .xml files are being accepted as contexts and it's also
060     * also assumed that those are available under
061     * WEB-INF/xml/users/some_user user directories
062     *
063     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
064     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
065     * @author last edited by: $Author: mschneider $
066     *
067     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
068     */
069    public class ContextLoadListener extends AbstractContextListener {
070    
071        private static String userDir = "WEB-INF/conf/igeoportal/users/";
072        private static final ILogger LOG = LoggerFactory.getLogger( ContextLoadListener.class );
073    
074        /* (non-Javadoc)
075         * @see org.deegree.enterprise.control.WebListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
076         */
077        @Override
078        public void actionPerformed( FormEvent event ) {
079    
080            RPCWebEvent rpc = (RPCWebEvent) event;
081            try {
082                validate( rpc );
083            } catch ( PortalException e ) {
084                LOG.logError( e.getMessage(), e );
085                gotoErrorPage( Messages.getMessage( "IGEO_STD_CNTXT_INVALID_RPC", "ContextLoad", e.getMessage() ) );
086                return;
087            }
088    
089            String userName = extractUserName( rpc );
090            List contextList =  getContextList( userName );
091    
092            getRequest().setAttribute( "CONTEXT_LIST", contextList );
093            getRequest().setAttribute( "USER", userName );
094    
095        }
096    
097        /**
098         * reads the session ID from the passed RPC and gets the assigned
099         * user name from a authentification service
100         * @param event
101         * @return the user name
102         */
103        private String extractUserName( RPCWebEvent event ) {
104            RPCMethodCall mc = event.getRPCMethodCall();
105            RPCParameter[] pars = mc.getParameters();
106            RPCStruct struct = (RPCStruct) pars[0].getValue();
107    
108            String name = "default";
109            try {
110                name = getUserName( RPCUtils.getRpcPropertyAsString( struct, "sessionID" ) );
111                if ( name == null ) {
112                    name = "default";
113                }
114            } catch ( Exception e ) {
115            }
116            // get map context value
117            return name;
118        }
119    
120        /**
121         * returns a list of all available context documents assigned to the
122         * passed user
123         *
124         * @param userName
125         * @return the list of available context documents
126         */
127        private List<String> getContextList( String userName ) {
128    
129            String path2Dir = getHomePath() + userDir + userName;
130    
131            File dir = new File( path2Dir );
132            File[] files = dir.listFiles();
133            List<String> contextList = new ArrayList<String>();
134            if ( files != null ) {
135                for ( int i = 0; i < files.length; i++ ) {
136                    String s = files[i].getName();
137                    if ( files[i].isFile() && s.endsWith( ".xml" ) ) {
138                        contextList.add( files[i].getName() );
139                    }
140                }
141            }
142            String[] list = contextList.toArray(new String[contextList.size()] );
143            Arrays.sort( list );
144            return new ArrayList<String>( Arrays.asList( list ) );
145        }
146    
147        /**
148         * validates if the passed RPC call containes the required variables
149         *
150         * @param rpc
151         * @throws PortalException
152         */
153        private void validate( RPCWebEvent rpc )
154                                throws PortalException {
155            RPCMethodCall mc = rpc.getRPCMethodCall();
156            RPCParameter param = mc.getParameters()[0];
157            RPCStruct struct = (RPCStruct) param.getValue();
158            RPCMember sessionID = struct.getMember( "sessionID" );
159            if ( sessionID == null ) {
160                throw new PortalException( Messages.getMessage( "IGEO_STD_CNTXT_MISSING_PARAM", "sessionID", "ContextLoad") );
161            }
162        }
163    
164    }