001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/portlet/modules/actions/LoginUser.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    package org.deegree.portal.portlet.modules.actions;
037    
038    import java.io.File;
039    
040    import javax.servlet.ServletContext;
041    import javax.servlet.http.HttpSession;
042    
043    import org.deegree.portal.context.ViewContext;
044    import org.deegree.portal.context.WebMapContextFactory;
045    import org.deegree.security.drm.model.User;
046    
047    /**
048     * This class can be used within listener classes that will be called if a user logs in into a
049     * portal. It reads the context documents from the users context directory that are storing the
050     * state of the maps when the users has logged out the last time. <BR>
051     * At the moment a concrete listener is available for Jetspeed 1.6
052     *
053     * @see org.deegree.portal.portlet.modules.actions.IGeoJetspeed16LoginUser
054     *
055     *
056     * @version $Revision: 18195 $
057     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
058     * @author last edited by: $Author: mschneider $
059     *
060     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
061     */
062    class LoginUser {
063    
064        /**
065         * validates if a WMC directory for the passed user is already available and creates it if not.
066         * The user's WMC directory will be returned as in instance of <@link File>
067         *
068         * @param user
069         * @param sc
070         * @return user's WMC directory
071         */
072        File ensureDirectory( ServletContext sc, String user ) {
073    
074            File dir = new File( sc.getRealPath( "WEB-INF/wmc/" + user ) );
075            if ( !dir.exists() ) {
076                dir.mkdir();
077            }
078    
079            return dir;
080        }
081    
082        /**
083         *
084         * @param dir
085         * @param ses
086         * @param user
087         */
088        void readContextDocuments( File dir, HttpSession ses, User user ) {
089            File[] files = dir.listFiles();
090            // we have to look for all files stored in the user's WMC
091            // directory and read those which can be identified as stored
092            // when the has been logged out the last time. These files will
093            // read and stored in the users session where the file name
094            // ( without extension ) is the attributes key and will
095            // be used by the portlets to access the assigned WMC
096            if ( files != null ) {
097                for ( int i = 0; i < files.length; i++ ) {
098                    String name = files[i].getName();
099                    if ( name.endsWith( AbstractPortletPerform.CURRENT_WMC + ".xml" ) ) {
100    
101                        int pos = name.lastIndexOf( '.' );
102                        name = name.substring( 0, pos );
103                        ViewContext vc = null;
104                        try {
105                            vc = WebMapContextFactory.createViewContext( files[i].toURL(), user, null );
106                        } catch ( Exception e ) {
107                            e.printStackTrace();
108                        }
109                        ses.setAttribute( name, vc );
110    
111                    }
112                }
113            }
114        }
115    
116    }