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