001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/enterprise/servlet/WPSHandler.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    
040    import javax.servlet.http.HttpServletResponse;
041    
042    import org.deegree.enterprise.ServiceException;
043    import org.deegree.framework.log.ILogger;
044    import org.deegree.framework.log.LoggerFactory;
045    import org.deegree.framework.util.CharsetUtils;
046    import org.deegree.model.feature.FeatureCollection;
047    import org.deegree.model.feature.GMLFeatureAdapter;
048    import org.deegree.ogcbase.ExceptionCode;
049    import org.deegree.ogcwebservices.OGCWebServiceException;
050    import org.deegree.ogcwebservices.OGCWebServiceRequest;
051    import org.deegree.ogcwebservices.wps.WPService;
052    import org.deegree.ogcwebservices.wps.WPServiceFactory;
053    import org.deegree.ogcwebservices.wps.XMLFactory;
054    import org.deegree.ogcwebservices.wps.capabilities.WPSCapabilities;
055    import org.deegree.ogcwebservices.wps.capabilities.WPSCapabilitiesDocument;
056    import org.deegree.ogcwebservices.wps.configuration.WPSConfiguration;
057    import org.deegree.ogcwebservices.wps.describeprocess.ProcessDescriptions;
058    import org.deegree.ogcwebservices.wps.describeprocess.ProcessDescriptionsDocument;
059    import org.deegree.ogcwebservices.wps.execute.ComplexValue;
060    import org.deegree.ogcwebservices.wps.execute.ExecuteResponse;
061    import org.deegree.ogcwebservices.wps.execute.ExecuteResponseDocument;
062    
063    /**
064     * WPSHandler.java
065     *
066     * Created on 08.03.2006. 17:01:31h
067     *
068     * @author <a href="mailto:kiehle@giub.uni-bonn.de">Christian Kiehle</a>
069     * @author <a href="mailto:che@wupperverband.de">Christian Heier</a>
070     *
071     * @version 1.0.
072     *
073     * @since 2.0
074     */
075    public class WPSHandler extends AbstractOWServiceHandler implements ServiceDispatcher {
076    
077        private static final ILogger LOG = LoggerFactory.getLogger( WPSHandler.class );
078    
079        /**
080         *
081         */
082        public void perform( OGCWebServiceRequest request, HttpServletResponse httpServletResponse )
083                                throws ServiceException, OGCWebServiceException {
084    
085            WPService service = WPServiceFactory.getInstance();
086            @SuppressWarnings("unused")
087            WPSConfiguration config = (WPSConfiguration) service.getCapabilities();
088            Object response = service.doService( request );
089            if ( response instanceof WPSCapabilities ) {
090                sendGetCapabilitiesResponse( httpServletResponse, (WPSCapabilities) response );
091            } else if ( response instanceof ProcessDescriptions ) {
092                sendDescribeProcessResponse( httpServletResponse, (ProcessDescriptions) response );
093            } else if ( response instanceof ExecuteResponse ) {
094                sendExecuteResponse( httpServletResponse, (ExecuteResponse) response );
095            }
096    
097        }
098    
099        /**
100         * Sends the response to a GetCapabilities request to the client.
101         *
102         * @param httpResponse
103         * @param capabilities
104         */
105        private void sendGetCapabilitiesResponse( HttpServletResponse httpResponse, WPSCapabilities capabilities ) {
106            try {
107                httpResponse.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
108                WPSCapabilitiesDocument document = XMLFactory.export( capabilities );
109                document.write( httpResponse.getOutputStream() );
110            } catch ( IOException e ) {
111                LOG.logError( "Error sending GetCapabilities response.", e );
112            }
113        }
114    
115        /**
116         * Sends the response to a DescribeProcess request to the client.
117         *
118         * @param httpResponse
119         * @param processDescriptions
120         */
121        private void sendDescribeProcessResponse( HttpServletResponse httpResponse, ProcessDescriptions processDescriptions ) {
122            try {
123                httpResponse.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
124                ProcessDescriptionsDocument document = XMLFactory.export( processDescriptions );
125                document.write( httpResponse.getOutputStream() );
126            } catch ( IOException e ) {
127                LOG.logError( "Error sending DescribeProcess response.", e );
128            }
129        }
130    
131        /**
132         * Sends the response to an Execute request to the client.
133         *
134         * @param httpResponse
135         * @param executeResponse
136         * @throws OGCWebServiceException
137         *             if an exception occurs which can be propagated to the client
138         */
139        private void sendExecuteResponse( HttpServletResponse httpResponse, ExecuteResponse executeResponse )
140                                throws OGCWebServiceException {
141    
142            /*
143             * @see OGC 05-007r4 Subclauses 10.3.1 and 10.3.2
144             * @see OGC 05-007r4 Tables 43, 44
145             * @see OGC 05-007r4 Table 27: If the storeparameter is false, process execution was
146             *      successful, there is only one output, and that output has a ComplexValue, then this
147             *      ComplexValue shall be returned to the client outside of any ExecuteResponse
148             *      document.
149             */
150            String processSucceeded = executeResponse.getStatus().getProcessSucceeded();
151    
152            if ( null != processSucceeded && executeResponse.isDirectResponse() ) {
153    
154                ComplexValue complexValue = executeResponse.getProcessOutputs().getOutputs().get( 0 ).getComplexValue();
155    
156                if ( null != complexValue ) {
157                    sendDirectResponse( httpResponse, complexValue );
158                }
159    
160            } else {
161                try {
162                    httpResponse.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
163                    ExecuteResponseDocument document = XMLFactory.export( executeResponse );
164                    document.write( httpResponse.getOutputStream() );
165                } catch ( IOException e ) {
166                    LOG.logError( "error sending execute response.", e );
167                }
168            }
169        }
170    
171        /**
172         * Writes the passed <code>ComplexValue</code> to the <code>HTTPServletResponse</code>
173         *
174         * @param httpResponse
175         * @param complexValue
176         */
177        private static void sendDirectResponse( HttpServletResponse httpResponse, ComplexValue complexValue )
178                                throws OGCWebServiceException {
179    
180            Object content = complexValue.getContent();
181    
182            if ( content instanceof FeatureCollection ) {
183    
184                LOG.logInfo( "content is instance of featurecollection" );
185    
186                FeatureCollection fc = (FeatureCollection) content;
187    
188                GMLFeatureAdapter gmlFeatureAdapter = new GMLFeatureAdapter();
189    
190                try {
191                    gmlFeatureAdapter.export( fc, httpResponse.getOutputStream() );
192                } catch ( Exception e ) {
193                    String msg = "Error sending direct execute response.";
194                    LOG.logError( msg, e );
195                    throw new OGCWebServiceException( "", msg, ExceptionCode.NOAPPLICABLECODE );
196                }
197            } else {
198                // TODO implement direct output methods for complexvalue types other
199                // than
200                // featurecollection
201            }
202        }
203    }