001    //$HeadURL$
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.portal.portlet.enterprise;
040    
041    import java.io.File;
042    import java.io.FileNotFoundException;
043    import java.io.FileOutputStream;
044    import java.io.IOException;
045    import java.util.Map;
046    
047    import javax.servlet.ServletException;
048    import javax.servlet.http.HttpServlet;
049    import javax.servlet.http.HttpServletRequest;
050    import javax.servlet.http.HttpServletResponse;
051    import javax.xml.parsers.ParserConfigurationException;
052    
053    import org.deegree.framework.log.ILogger;
054    import org.deegree.framework.log.LoggerFactory;
055    import org.deegree.framework.util.KVP2Map;
056    import org.deegree.framework.util.StringTools;
057    import org.deegree.framework.xml.XMLFragment;
058    import org.deegree.portal.context.ViewContext;
059    import org.deegree.portal.context.XMLFactory;
060    import org.deegree.portal.portlet.modules.actions.IGeoPortalPortletPerform;
061    
062    /**
063     * 
064     * 
065     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
066     * @author last edited by: $Author: poth $
067     * 
068     * @version. $Revision: 1.6 $, $Date: 2007-09-04 16:09:05 $
069     */
070    public class ContextSaveServlet extends HttpServlet {
071    
072        private static final ILogger LOG = LoggerFactory.getLogger( ContextSaveServlet.class );
073    
074        @Override
075        protected void doGet( HttpServletRequest request, HttpServletResponse response )
076                                throws ServletException, IOException {
077            this.doPost( request, response );
078        }
079    
080        @Override
081        protected void doPost( HttpServletRequest request, HttpServletResponse response )
082                                throws ServletException, IOException {
083    
084            Map<String, String> parameter = KVP2Map.toMap( request );
085    
086            String mm = parameter.get( "MAPPORTLET" );
087            LOG.logDebug( "parameter: " + parameter );
088            IGeoPortalPortletPerform igeo = new IGeoPortalPortletPerform( request, null, getServletContext() );
089            ViewContext vc = igeo.getCurrentViewContext( mm );
090    
091            String filename = parameter.get( "FILENAME" );
092            String user = parameter.get( "USER" );
093            File dir = new File( getServletContext().getRealPath( Messages.getString( "WMCBasePath" ) + user ) );
094    
095            if ( !dir.exists() ) {
096                // create user director if not exits
097                dir.mkdir();
098            }
099    
100            filename = StringTools.concat( 150, Messages.getString( "WMCBasePath" ), user, '/', filename );
101            filename = getServletContext().getRealPath( filename );
102            try {
103                saveDocument( vc, filename );
104            } catch ( ParserConfigurationException e ) {
105                LOG.logError( e.getMessage(), e );
106                request.setAttribute( "MESSAGE", Messages.getString( "ContextSaveServlet.2" ) );
107                request.getRequestDispatcher( "/igeoportal/error.jsp" ).forward( request, response );
108                return;
109            } catch ( FileNotFoundException e ) {
110                LOG.logError( e.getMessage(), e );
111                request.setAttribute( "MESSAGE", Messages.getString( "ContextSaveServlet.3" ) );
112                request.getRequestDispatcher( "/igeoportal/error.jsp" ).forward( request, response );
113                return;
114            } catch ( IOException e ) {
115                LOG.logError( e.getMessage(), e );
116                request.setAttribute( "MESSAGE", Messages.getString( "ContextSaveServlet.4" ) );
117                request.getRequestDispatcher( "/igeoportal/error.jsp" ).forward( request, response );
118                return;
119            }
120            request.setAttribute( "MESSAGE", Messages.getString( "ContextSaveServlet.5" ) );
121            request.getRequestDispatcher( "/igeoportal/message.jsp" ).forward( request, response );
122        }
123    
124        /**
125         * 
126         * @param vc
127         * @param filename
128         * @throws ParserConfigurationException
129         * @throws IOException
130         * @throws ServletException
131         */
132        private void saveDocument( ViewContext vc, String filename )
133                                throws ParserConfigurationException, IOException {
134    
135            /*
136            // removes all layers that are just for highlighting selected features
137            Layer[] layers = vc.getLayerList().getLayers();        
138            for ( int i = 0; i < layers.length; i++ ) {
139                if ( layers[i].getTitle().startsWith( SelectFeaturesPerform.SELECTEDLAYERNAME ) ) {
140                    vc.getLayerList().removeLayer( layers[i].getName(), null );
141                }
142            }
143            */
144    
145            XMLFragment xml = XMLFactory.export( vc );
146            FileOutputStream fos = new FileOutputStream( filename );
147            xml.write( fos );
148            fos.close();
149    
150        }
151    
152    }