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