001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/servlet/SOSHandler.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.enterprise.servlet;
037    
038    import java.io.IOException;
039    import java.io.OutputStream;
040    
041    import javax.servlet.http.HttpServletResponse;
042    
043    import org.deegree.enterprise.ServiceException;
044    import org.deegree.framework.log.ILogger;
045    import org.deegree.framework.log.LoggerFactory;
046    import org.deegree.framework.xml.XMLFragment;
047    import org.deegree.ogcwebservices.OGCWebServiceException;
048    import org.deegree.ogcwebservices.OGCWebServiceRequest;
049    import org.deegree.ogcwebservices.sos.SOService;
050    import org.deegree.ogcwebservices.sos.SOServiceFactory;
051    import org.deegree.ogcwebservices.sos.XMLFactory;
052    import org.deegree.ogcwebservices.sos.capabilities.SOSCapabilities;
053    import org.deegree.ogcwebservices.sos.describeplatform.DescribePlatformResult;
054    import org.deegree.ogcwebservices.sos.describesensor.DescribeSensorResult;
055    import org.deegree.ogcwebservices.sos.getobservation.GetObservationResult;
056    
057    /**
058     * Web servlet client for WFS.
059     *
060     * @author <a href="mailto:mkulbe@lat-lon.de">Matthias Kulbe</a>
061     * @author last edited by: $Author: mschneider $
062     *
063     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
064     */
065    class SOSHandler extends AbstractOWServiceHandler {
066    
067        private static ILogger LOG = LoggerFactory.getLogger( SOSHandler.class );
068    
069        /**
070         *
071         */
072        SOSHandler() {
073            LOG.logDebug( "New SOSHandler instance created: " + this.getClass().getName() );
074        }
075    
076        /**
077         * @param request
078         * @param response
079         * @throws ServiceException
080         * @throws OGCWebServiceException
081         * @see "org.deegree.enterprise.servlet.ServiceDispatcher#perform(org.deegree.services.AbstractOGCWebServiceRequest,javax.servlet.http.HttpServletResponse)"
082         */
083        public void perform( OGCWebServiceRequest request, HttpServletResponse response )
084                                throws ServiceException, OGCWebServiceException {
085    
086            Object serviceRes = this.getService().doService( request );
087    
088            if ( serviceRes instanceof OGCWebServiceException ) {
089                sendException( response, (OGCWebServiceException) serviceRes );
090            } else if ( response instanceof Exception ) {
091                sendException( response, (Exception) serviceRes );
092            } else if ( serviceRes instanceof SOSCapabilities ) {
093                XMLFragment doc = XMLFactory.export( (SOSCapabilities) serviceRes );
094                createResponse( response, doc );
095            } else if ( serviceRes instanceof DescribePlatformResult ) {
096                XMLFragment doc = XMLFactory.export( (DescribePlatformResult) serviceRes );
097                createResponse( response, doc );
098            } else if ( serviceRes instanceof DescribeSensorResult ) {
099                XMLFragment doc = XMLFactory.export( (DescribeSensorResult) serviceRes );
100                createResponse( response, doc );
101            } else if ( serviceRes instanceof GetObservationResult ) {
102                XMLFragment doc = XMLFactory.export( (GetObservationResult) serviceRes );
103                createResponse( response, doc );
104            } else {
105    
106                String mesgFragment = null;
107                if ( response == null ) {
108                    mesgFragment = "null response object";
109                } else {
110                    mesgFragment = response.getClass().getName();
111                }
112    
113                OGCWebServiceException e = new OGCWebServiceException( getClass().getName(), "Unknown response class: "
114                                                                                             + mesgFragment );
115                sendException( response, e );
116            }
117        }
118    
119        private void createResponse( HttpServletResponse response, XMLFragment doc )
120                                throws OGCWebServiceException {
121    
122            try {
123                OutputStream os = response.getOutputStream();
124                doc.write( os );
125                os.close();
126            } catch ( IOException e ) {
127                throw new OGCWebServiceException( "SCS Web Service failed." );
128            }
129    
130        }
131    
132        /**
133         * @return Service
134         * @throws ServiceException
135         */
136        /*
137         * (non-Javadoc)
138         *
139         * @see org.deegree.enterprise.servlet.ServiceDispatcher#setConfiguration(java.net.URL)
140         */
141        public SOService getService()
142                                throws ServiceException {
143            SOService service = null;
144            try {
145                service = SOServiceFactory.getService();
146            } catch ( Exception e ) {
147                LOG.logError( e.getMessage(), e );
148                throw new ServiceException( e );
149            }
150            return service;
151        }
152    }