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