001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/enterprise/servlet/WASSHandler.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     
043     ---------------------------------------------------------------------------*/
044    
045    package org.deegree.enterprise.servlet;
046    
047    import java.io.BufferedInputStream;
048    import java.io.IOException;
049    import java.io.InputStream;
050    import java.io.OutputStream;
051    
052    import javax.servlet.http.HttpServletResponse;
053    
054    import org.apache.commons.httpclient.Header;
055    import org.deegree.enterprise.ServiceException;
056    import org.deegree.framework.log.ILogger;
057    import org.deegree.framework.log.LoggerFactory;
058    import org.deegree.framework.util.CharsetUtils;
059    import org.deegree.ogcwebservices.OGCWebServiceException;
060    import org.deegree.ogcwebservices.OGCWebServiceRequest;
061    import org.deegree.ogcwebservices.wass.common.WASServiceFactory;
062    import org.deegree.ogcwebservices.wass.common.XMLFactory;
063    import org.deegree.ogcwebservices.wass.was.WAService;
064    import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilities;
065    import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilitiesDocument;
066    import org.deegree.ogcwebservices.wass.was.operation.DescribeUserResponse;
067    import org.deegree.ogcwebservices.wass.wss.WSService;
068    import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilities;
069    import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilitiesDocument;
070    import org.deegree.ogcwebservices.wass.wss.operation.DoServiceResponse;
071    
072    /**
073     * This is the servlet handler class for the WASS services, ie, the Web
074     * Authentication Service and the Web Security Service. Attention: since much of
075     * the WAS/WSS behaviour is specified to be the same, much of the code of this
076     * class is shared, see for example the handleResult method.
077     * 
078     * @see #handleResult(Object)
079     * 
080     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
081     * @author last edited by: $Author: apoth $
082     * 
083     * @version 2.0, $Revision: 9338 $, $Date: 2007-12-27 13:31:31 +0100 (Do, 27 Dez 2007) $
084     * 
085     * @since 2.0
086     */
087    
088    public class WASSHandler extends AbstractOWServiceHandler implements ServiceDispatcher {
089    
090        private static final ILogger LOG = LoggerFactory.getLogger( WASSHandler.class );
091    
092        private HttpServletResponse response = null;
093    
094        /**
095         * Method to handle the various output objects.
096         * 
097         * @param result
098         * @throws IOException
099         */
100        private void handleResult( Object result )
101                                throws IOException {
102            // if result is null, was possibly a CloseSession request, so return
103            // nothing as specified
104            if ( result == null ){
105                response.setContentType( "text/plain; charset=" + CharsetUtils.getSystemCharset() );            
106                response.getWriter().println();
107            }
108    
109            if ( result instanceof OGCWebServiceException ) {
110                sendException( response, (OGCWebServiceException) result );
111            } else if ( result instanceof Exception ) {
112                sendException( response, (Exception) result );
113            } else if ( result instanceof String ) {
114                // just write the SessionID result from GetSession request
115                response.setContentType( "text/plain; charset=" + CharsetUtils.getSystemCharset() );
116                response.getWriter().print( result );
117            } else if ( result instanceof WASCapabilities ) {
118                sendCapabilities( (WASCapabilities) result );
119            } else if ( result instanceof WSSCapabilities ) {
120                sendCapabilities( (WSSCapabilities) result );
121            } else if ( result instanceof DescribeUserResponse ) {
122                sendDescribeUserResponse( (DescribeUserResponse) result );
123            } else if ( result instanceof DoServiceResponse ) {
124                sendOtherServiceResponse( (DoServiceResponse) result );
125            }
126        }
127    
128        /**
129         * Sends the XML document contained within the given parameter
130         * 
131         * @param result
132         * @throws IOException 
133         */
134        private void sendDescribeUserResponse( DescribeUserResponse result ) throws IOException {
135            response.setContentType( "text/xml;  charset=" + CharsetUtils.getSystemCharset() );
136            result.write( response.getOutputStream() );
137        }
138    
139        /**
140         * Method to send the result of another service.
141         * 
142         * @param result
143         * @throws IOException 
144         */
145        private void sendOtherServiceResponse( DoServiceResponse result )
146                                throws IOException {
147            Header[] headers = result.getHeaders();
148            // footers will be ignored for now
149            //        Header[] footers = result.getFooters();
150            InputStream in = result.getResponseBody();
151            for ( Header h : headers ) {
152                // maybe we have to filter some headers here TODO
153                LOG.logDebug( h.toExternalForm() );
154                response.setHeader( h.getName(), h.getValue() );
155            }
156    //        BufferedReader bin = new BufferedReader( new InputStreamReader( in ) );
157    //        PrintWriter out = response.getWriter();
158            BufferedInputStream bin = new BufferedInputStream( in );
159            OutputStream out = response.getOutputStream();
160            byte[] buf = new byte[4096];
161            int numberRead = 0;
162            while ( (numberRead = bin.read( buf )) != -1 ){
163                out.write( buf, 0, numberRead );
164            }
165            bin.close();
166            out.close();
167        }
168    
169        /**
170         * Sends the given capabilities.
171         * 
172         * @param capabilities
173         */
174        private void sendCapabilities( WSSCapabilities capabilities ) {
175            try {
176                response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );            
177                WSSCapabilitiesDocument document = XMLFactory.export( capabilities );
178                document.write( response.getOutputStream() );
179            } catch ( IOException e ) {
180                LOG.logError( "Error sending GetCapabilities response.", e );
181            }
182        }
183    
184        /**
185         * Sends the given capabilities.
186         * 
187         * @param capabilities
188         */
189        private void sendCapabilities( WASCapabilities capabilities ) {
190            try {
191                response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
192                WASCapabilitiesDocument document = XMLFactory.export( capabilities );
193                document.write( response.getOutputStream() );
194            } catch ( IOException e ) {
195                LOG.logError( "Error sending GetCapabilities response.", e );
196            }
197        }
198    
199        /*
200         * (non-Javadoc)
201         * 
202         * @see org.deegree.enterprise.servlet.ServiceDispatcher#perform(org.deegree.ogcwebservices.OGCWebServiceRequest,
203         *      javax.servlet.http.HttpServletResponse)
204         */
205        public void perform( OGCWebServiceRequest request, HttpServletResponse response )
206                                throws ServiceException, OGCWebServiceException {
207            Object result = null;
208            this.response = response;
209            if ( "WAS".equals( request.getServiceName() ) ) {
210                WAService service = WASServiceFactory.getUncachedWAService(); // get from factory
211                result = service.doService( request );
212            } else if ( "WSS".equals( request.getServiceName() ) ) {
213                WSService service = WASServiceFactory.getUncachedWSService(); // get from factory
214                result = service.doService( request );
215            }
216    
217            try {
218                handleResult( result );
219            } catch ( IOException e ) {
220                LOG.logError( e.getLocalizedMessage(), e );
221                throw new OGCWebServiceException( "Error while handling request: \n"
222                                                  + e.getLocalizedMessage() );
223            }
224        }
225    
226    }