    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/enterprise/servlet/AbstractOWServiceHandler.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.OutputStream;
    039
    040    import javax.servlet.http.HttpServletResponse;
    041
    042    import org.deegree.framework.log.ILogger;
    043    import org.deegree.framework.log.LoggerFactory;
    044    import org.deegree.framework.xml.XMLFragment;
    045    import org.deegree.ogcwebservices.ExceptionReport;
    046    import org.deegree.ogcwebservices.OGCWebServiceException;
    047    import org.deegree.owscommon.XMLFactory;
    048
    049    /**
    050     * This class provides methods that are common to all services that comply to the OWS Common
    051     * Implementation Specification 0.3.0.
    052     * <p>
    053     * At the moment, the only implemented functionality allows the sending of exception reports to the
    054     * client, but in the future this may be extended by providing a method that sends responses to
    055     * GetCapabilities requests.
    056     * </p>
    057     *
    058     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
    059     *
    060     * @author last edited by: $Author: mschneider $
    061     *
    062     * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    063     *
    064     * @since 2.0
    065     */
    066    abstract class AbstractOWServiceHandler implements ServiceDispatcher {
    067
    068        // private static final String RESPONSE_TYPE = "application/vnd.ogc.se_xml";
    069        private static final String RESPONSE_TYPE = "text/xml";
    070
    071        private static ILogger LOG = LoggerFactory.getLogger( AbstractOWServiceHandler.class );
    072
    073        /**
    074         * Sends an exception report to the client. The exception report complies to the OWS Common
    075         * Implementation Specification 0.3.0.
    076         *
    077         * @param httpResponse
    078         * @param serviceException
    079         */
    080        public void sendException( HttpServletResponse httpResponse, OGCWebServiceException serviceException ) {
    081
    082            ExceptionReport report = new ExceptionReport( new OGCWebServiceException[] { serviceException } );
    083            try {
    084                httpResponse.setContentType( RESPONSE_TYPE );
    085                XMLFragment reportDocument = XMLFactory.export( report );
    086                OutputStream os = httpResponse.getOutputStream();
    087                reportDocument.write( os );
    088                os.close();
    089            } catch ( Exception e ) {
    090                LOG.logError( "Error sending exception report: ", e );
    091            }
    092
    093        }
    094
    095        /**
    096         * Sends an exception report to the client. The exception report complies to the OWS Common
    097         * Implementation Specification 0.3.0.
    098         *
    099         * @param httpResponse
    100         * @param serviceException
    101         */
    102        public void sendException( HttpServletResponse httpResponse, Exception serviceException ) {
    103
    104            OGCWebServiceException ogc = new OGCWebServiceException( serviceException.getMessage() );
    105
    106            ExceptionReport report = new ExceptionReport( new OGCWebServiceException[] { ogc } );
    107            try {
    108                httpResponse.setContentType( RESPONSE_TYPE );
    109                XMLFragment reportDocument = XMLFactory.export( report );
    110                OutputStream os = httpResponse.getOutputStream();
    111                reportDocument.write( os );
    112                os.close();
    113            } catch ( Exception e ) {
    114                LOG.logError( "Error sending exception report: ", e );
    115            }
    116
    117        }
    118
    119        /**
    120         * @param response
    121         * @param exception
    122         */
    123        public void sendExceptionReport( HttpServletResponse response, Exception exception ) {
    124            OGCWebServiceException ogc;
    125            if ( exception instanceof OGCWebServiceException ) {
    126                ogc = (OGCWebServiceException) exception;
    127            } else {
    128                ogc = new OGCWebServiceException( exception.getMessage() );
    129            }
    130
    131            ExceptionReport report = new ExceptionReport( new OGCWebServiceException[] { ogc } );
    132            try {
    133                response.setContentType( RESPONSE_TYPE );
    134                XMLFragment reportDocument = XMLFactory.exportExceptionReport( report );
    135                OutputStream os = response.getOutputStream();
    136                reportDocument.prettyPrint( os );
    137                os.close();
    138            } catch ( Exception e ) {
    139                LOG.logError( "Error sending exception report: ", e );
    140            }
    141        }
    142
    143    }