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