001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/standard/context/control/ContextDeleteListener.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.standard.context.control;
037    
038    import java.io.File;
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.RPCWebEvent;
048    import org.deegree.framework.log.ILogger;
049    import org.deegree.framework.log.LoggerFactory;
050    import org.deegree.i18n.Messages;
051    import org.deegree.portal.PortalException;
052    
053    /**
054     * Listener class that enables deleting a named conetxt
055     * 
056     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
057     * @author last edited by: $Author: jmays $
058     * 
059     * @version $Revision: 21440 $, $Date: 2009-12-14 16:46:38 +0100 (Mo, 14 Dez 2009) $
060     */
061    public class ContextDeleteListener extends AbstractContextListener {
062    
063        private static ILogger LOG = LoggerFactory.getLogger( ContextDeleteListener.class );
064    
065        /**
066         * WEB-INF/conf/igeoportal/
067         */
068        private static String confDir = "WEB-INF/conf/igeoportal/";
069    
070        public void actionPerformed( FormEvent event ) {
071    
072            RPCWebEvent rpc = (RPCWebEvent) event;
073            try {
074                validate( rpc );
075            } catch ( PortalException e ) {
076                gotoErrorPage( Messages.getMessage( "IGEO_STD_CNTXT_NO_VALID_RPC", e.getMessage() ) );
077                return;
078            }
079    
080            String userName = null;
081            try {
082                userName = extractUserName( rpc );
083            } catch ( Exception e ) {
084                gotoErrorPage( Messages.getMessage( "IGEO_STD_CNTXT_INVALID_USER_IDENTIFICATION", e.getMessage() ) );
085                return;
086            }
087    
088            String contextName = readContextName( rpc );
089            try {
090                deleteContext( contextName, userName );
091            } catch ( Exception e ) {
092                gotoErrorPage( Messages.getMessage( "IGEO_STD_CNTXT_DELETE_FAILED", e.getMessage() ) );
093                e.printStackTrace();
094                return;
095            }
096    
097            int pos = contextName.lastIndexOf( "/" );
098            contextName = contextName.substring( pos + 1, contextName.length() );
099            List<String> contextList = null;
100            try {
101                contextList = getContextList( userName );
102            } catch ( Exception e ) {
103                gotoErrorPage( "List of available context documents\n" + e.getMessage() );
104                return;
105            }
106    
107            getRequest().setAttribute( "CONTEXT", contextName );
108            getRequest().setAttribute( "CONTEXT_LIST", contextList );
109            getRequest().setAttribute( "USER", userName );
110        }
111    
112        /**
113         * validates if the passed RPC call containes the required variables
114         * 
115         * @param rpc
116         * @throws ClientException
117         */
118        private void validate( RPCWebEvent rpc )
119                                throws PortalException {
120            RPCMethodCall mc = rpc.getRPCMethodCall();
121            RPCParameter param = mc.getParameters()[0];
122            RPCStruct struct = (RPCStruct) param.getValue();
123    
124            RPCMember selectedContext = struct.getMember( "mapContext" );
125            if ( selectedContext == null ) {
126                throw new PortalException( Messages.getMessage( "IGEO_STD_CNTXT_MISSING_PARAM", "mapContext",
127                                                                "ContextDelete" ) );
128            }
129            RPCMember sessionID = struct.getMember( "sessionID" );
130            if ( sessionID == null ) {
131                throw new PortalException( Messages.getMessage( "IGEO_STD_CNTXT_MISSING_PARAM", "sessionID",
132                                                                "ContextDelete" ) );
133            }
134            LOG.logDebug( "*** RPC VALIDATION ***\n mapContext: ", selectedContext.getValue(), "\n sessionID: ",
135                          sessionID.getValue() );
136        }
137    
138        /**
139         * reads the session ID from the passed RPC and gets the assigned user name from a authentification service.
140         * 
141         * @param event
142         * @return
143         * @throws Exception
144         */
145        private String extractUserName( RPCWebEvent event )
146                                throws Exception {
147            RPCMethodCall mc = event.getRPCMethodCall();
148            RPCParameter[] pars = mc.getParameters();
149            RPCStruct struct = (RPCStruct) pars[0].getValue();
150    
151            // get map context value
152            String name = "default";
153            try {
154                name = getUserName( (String) struct.getMember( "sessionID" ).getValue() );
155                if ( name == null ) {
156                    name = "default";
157                }
158            } catch ( Exception e ) {
159            }
160            return name;
161        }
162    
163        private String readContextName( RPCWebEvent rpc ) {
164            RPCMethodCall mc = rpc.getRPCMethodCall();
165            RPCParameter param = mc.getParameters()[0];
166            RPCStruct struct = (RPCStruct) param.getValue();
167            return (String) struct.getMember( "mapContext" ).getValue();
168        }
169    
170        /**
171         * 
172         * @param contextName
173         * @param userName
174         * @throws ClientException
175         * @throws IOException
176         */
177        private void deleteContext( String contextName, String userName )
178                                throws PortalException, IOException {
179    
180            String fName = getHomePath() + confDir + contextName;
181            // ensure that no configuration files are deleted
182            if ( !fName.endsWith( ".properties" ) && !fName.endsWith( ".properies~" ) ) {
183    
184                String startCnxt = getUsersStartContext( userName );
185                if ( new File( startCnxt ).getName().equals( new File( fName ).getName() ) ) {
186                    throw new PortalException( Messages.getMessage( "IGEO_STD_CNTXT_DELETE_STARTCONTEXT_NOT_ALLOWED",
187                                                                    contextName ) );
188                }
189    
190                File file = new File( fName );
191                if ( !file.exists() ) {
192                    throw new PortalException( Messages.getMessage( "IGEO_STD_CNTXT_DOES_NOT_EXIST", contextName ) );
193                }
194                if ( !file.delete() ) {
195                    throw new PortalException( Messages.getMessage( "IGEO_STD_CNTXT_NOT_DELETED", contextName ) );
196                }
197            }
198        }
199    
200    }