001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/standard/sos/control/SOSClientRequestDispatcher.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.sos.control;
037    
038    
039    import java.io.IOException;
040    import java.net.URL;
041    
042    import javax.servlet.ServletConfig;
043    import javax.servlet.ServletException;
044    import javax.servlet.http.HttpServletRequest;
045    import javax.servlet.http.HttpServletResponse;
046    
047    import org.deegree.enterprise.control.FormEvent;
048    import org.deegree.framework.log.ILogger;
049    import org.deegree.framework.log.LoggerFactory;
050    import org.deegree.framework.util.StringTools;
051    import org.deegree.framework.util.WebappResourceResolver;
052    import org.deegree.portal.standard.sos.configuration.SOSClientConfiguration;
053    
054    
055    /**
056     * Client request dispatcher based on CSW's one.
057     *
058     * @version $Revision: 18195 $
059     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
060     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
061     */
062    public class SOSClientRequestDispatcher extends org.deegree.enterprise.control.RequestDispatcher {
063    
064        /**
065         *
066         */
067        private static final long serialVersionUID = 1533730388575870568L;
068    
069    
070        private static final ILogger LOG = LoggerFactory.getLogger( SOSClientRequestDispatcher.class );
071    
072    
073        /**
074         * Comment for <code>ERROR_PAGE</code>
075         */
076        static String ERROR_PAGE = "error.jsp";
077    
078        /**
079         * This method initializes the servlet.
080         *
081         * @param   cfg  the servlet configuration
082         *
083         * @throws  ServletException  an exception
084         */
085        @Override
086        public void init( ServletConfig cfg ) throws ServletException {
087            super.init( cfg );
088    
089            try {
090                // initialize configuration of client and data servers
091                URL url = WebappResourceResolver.resolveFileLocation( getInitParameter( "Client.configFile" ),
092                    this.getServletContext(), LOG);
093                SOSClientConfiguration.getInstance( url );
094    
095            } catch ( Exception e ) {
096                e.printStackTrace();
097                throw new ServletException( StringTools.stackTraceToString( e.getStackTrace() ) );
098            }
099    
100            if ( getInitParameter( "Client.errorPage" ) != null ) {
101                ERROR_PAGE = getInitParameter( "Client.errorPage" );
102            }
103        }
104    
105         @Override
106        protected void service(HttpServletRequest request, HttpServletResponse response)
107                               throws ServletException, IOException {
108    
109            // create event out of request
110            FormEvent _event = createEvent( request );
111    
112            // deliver event to application handler
113            deliverEvent( _event );
114    
115            // get next page from request attribute
116            String nextPage = (String) request.getAttribute( "next" );
117    
118            // show error page if next page is null
119            if ( nextPage == null ) nextPage = ERROR_PAGE;
120            nextPage = "/" + nextPage;
121    
122            if ( request.getAttribute("javax.servlet.jsp.jspException") != null ) {
123                nextPage = "/" + ERROR_PAGE;
124            }
125    
126            try {
127                request.setAttribute( "CONFIGURATION",  SOSClientConfiguration.getInstance() );
128            } catch(Exception e) {
129                e.printStackTrace();
130                throw new ServletException( "could not create SOS client configuration" );
131            }
132            //FIXME throwing nasty exception when plaform doesn't exist -> handle this
133            // this exception is not showing up on error.jsp
134            // call request dispatcher
135            getServletConfig().getServletContext().getRequestDispatcher( nextPage ).forward( request, response );
136            _event = null;
137        }
138    }