001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/servlet/WASSHandler.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    
037    package org.deegree.enterprise.servlet;
038    
039    import static org.deegree.framework.log.LoggerFactory.getLogger;
040    import static org.deegree.framework.util.CharsetUtils.getSystemCharset;
041    
042    import java.io.BufferedInputStream;
043    import java.io.IOException;
044    import java.io.InputStream;
045    import java.io.OutputStream;
046    
047    import javax.servlet.http.HttpServletResponse;
048    
049    import org.apache.commons.httpclient.Header;
050    import org.deegree.enterprise.ServiceException;
051    import org.deegree.framework.log.ILogger;
052    import org.deegree.framework.util.CharsetUtils;
053    import org.deegree.ogcwebservices.OGCWebServiceException;
054    import org.deegree.ogcwebservices.OGCWebServiceRequest;
055    import org.deegree.ogcwebservices.wass.common.WASServiceFactory;
056    import org.deegree.ogcwebservices.wass.common.XMLFactory;
057    import org.deegree.ogcwebservices.wass.was.WAService;
058    import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilities;
059    import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilitiesDocument;
060    import org.deegree.ogcwebservices.wass.was.operation.DescribeUserResponse;
061    import org.deegree.ogcwebservices.wass.wss.WSService;
062    import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilities;
063    import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilitiesDocument;
064    import org.deegree.ogcwebservices.wass.wss.operation.DoServiceResponse;
065    
066    /**
067     * This is the servlet handler class for the WASS services, ie, the Web Authentication Service and the Web Security
068     * Service. Attention: since much of the WAS/WSS behaviour is specified to be the same, much of the code of this class
069     * is shared, see for example the handleResult method.
070     *
071     * @see #handleResult(Object)
072     *
073     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
074     * @author last edited by: $Author: mschneider $
075     *
076     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
077     */
078    public class WASSHandler extends AbstractOWServiceHandler {
079    
080        private static final ILogger LOG = getLogger( WASSHandler.class );
081    
082        private HttpServletResponse response = null;
083    
084        /**
085         * Method to handle the various output objects.
086         *
087         * @param result
088         * @throws IOException
089         */
090        private void handleResult( Object result )
091                                throws IOException {
092            // if result is null, was possibly a CloseSession request, so return
093            // nothing as specified
094            if ( result == null ) {
095                response.setContentType( "text/plain; charset=" + getSystemCharset() );
096                response.getWriter().println();
097            }
098    
099            if ( result instanceof OGCWebServiceException ) {
100                sendException( response, (OGCWebServiceException) result );
101            } else if ( result instanceof Exception ) {
102                sendException( response, (Exception) result );
103            } else if ( result instanceof String ) {
104                // just write the SessionID result from GetSession request
105                response.setContentType( "text/plain; charset=" + getSystemCharset() );
106                response.getWriter().print( result );
107            } else if ( result instanceof WASCapabilities ) {
108                sendCapabilities( (WASCapabilities) result );
109            } else if ( result instanceof WSSCapabilities ) {
110                sendCapabilities( (WSSCapabilities) result );
111            } else if ( result instanceof DescribeUserResponse ) {
112                sendDescribeUserResponse( (DescribeUserResponse) result );
113            } else if ( result instanceof DoServiceResponse ) {
114                sendOtherServiceResponse( (DoServiceResponse) result );
115            }
116        }
117    
118        /**
119         * Sends the XML document contained within the given parameter
120         *
121         * @param result
122         * @throws IOException
123         */
124        private void sendDescribeUserResponse( DescribeUserResponse result )
125                                throws IOException {
126            response.setContentType( "text/xml;  charset=" + CharsetUtils.getSystemCharset() );
127            result.write( response.getOutputStream() );
128        }
129    
130        /**
131         * Method to send the result of another service.
132         *
133         * @param result
134         * @throws IOException
135         */
136        private void sendOtherServiceResponse( DoServiceResponse result )
137                                throws IOException {
138            Header[] headers = result.getHeaders();
139            // footers will be ignored for now
140            // Header[] footers = result.getFooters();
141            InputStream in = result.getResponseBody();
142            for ( Header h : headers ) {
143                // maybe we have to filter some headers here TODO
144                LOG.logDebug( h.toExternalForm() );
145                // ignore content length header, as the content length actually changes for GetCapabilities responses
146                // (facade URLs and possibly encoding). Otherwise parts of the response may get cut off.
147                if ( !h.getName().equalsIgnoreCase( "content-length" ) ) {
148                    response.setHeader( h.getName(), h.getValue() );
149                }
150            }
151    
152            BufferedInputStream bin = new BufferedInputStream( in, 16384 );
153            OutputStream out = response.getOutputStream();
154            byte[] buf = new byte[16384];
155            int numberRead = 0;
156            while ( ( numberRead = bin.read( buf ) ) != -1 ) {
157                out.write( buf, 0, numberRead );
158            }
159            bin.close();
160            out.close();
161        }
162    
163        /**
164         * Sends the given capabilities.
165         *
166         * @param capabilities
167         */
168        private void sendCapabilities( WSSCapabilities capabilities ) {
169            try {
170                response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
171                WSSCapabilitiesDocument document = XMLFactory.export( capabilities );
172                document.write( response.getOutputStream() );
173            } catch ( IOException e ) {
174                LOG.logError( "Error sending GetCapabilities response.", e );
175            }
176        }
177    
178        /**
179         * Sends the given capabilities.
180         *
181         * @param capabilities
182         */
183        private void sendCapabilities( WASCapabilities capabilities ) {
184            try {
185                response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
186                WASCapabilitiesDocument document = XMLFactory.export( capabilities );
187                document.write( response.getOutputStream() );
188            } catch ( IOException e ) {
189                LOG.logError( "Error sending GetCapabilities response.", e );
190            }
191        }
192    
193        /*
194         * (non-Javadoc)
195         *
196         * @see org.deegree.enterprise.servlet.ServiceDispatcher#perform(org.deegree.ogcwebservices.OGCWebServiceRequest,
197         * javax.servlet.http.HttpServletResponse)
198         */
199        public void perform( OGCWebServiceRequest request, HttpServletResponse response )
200                                throws ServiceException, OGCWebServiceException {
201            Object result = null;
202            this.response = response;
203            if ( "WAS".equals( request.getServiceName() ) ) {
204                WAService service = WASServiceFactory.getUncachedWAService(); // get from factory
205                result = service.doService( request );
206            } else if ( "WSS".equals( request.getServiceName() ) ) {
207                WSService service = WASServiceFactory.getUncachedWSService(); // get from factory
208                result = service.doService( request );
209            }
210    
211            try {
212                handleResult( result );
213            } catch ( IOException e ) {
214                LOG.logError( e.getLocalizedMessage(), e );
215                throw new OGCWebServiceException( "Error while handling request: \n" + e.getLocalizedMessage() );
216            }
217        }
218    
219    }