001 //$HeadURL: $
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
037 package org.deegree.enterprise.servlet;
038
039 import java.io.IOException;
040 import java.util.List;
041
042 import javax.servlet.http.HttpServletResponse;
043 import javax.xml.transform.TransformerException;
044
045 import org.deegree.enterprise.ServiceException;
046 import org.deegree.framework.log.ILogger;
047 import org.deegree.framework.log.LoggerFactory;
048 import org.deegree.framework.xml.XMLFragment;
049 import org.deegree.i18n.Messages;
050 import org.deegree.ogcbase.ExceptionCode;
051 import org.deegree.ogcwebservices.OGCWebServiceException;
052 import org.deegree.ogcwebservices.OGCWebServiceRequest;
053 import org.deegree.ogcwebservices.wcts.WCTService;
054 import org.deegree.ogcwebservices.wcts.WCTServiceFactory;
055 import org.deegree.ogcwebservices.wcts.XMLFactory;
056 import org.deegree.ogcwebservices.wcts.operation.GetResourceByID;
057 import org.deegree.ogcwebservices.wcts.operation.GetTransformation;
058 import org.deegree.ogcwebservices.wcts.operation.IsTransformable;
059 import org.deegree.ogcwebservices.wcts.operation.Transform;
060 import org.deegree.ogcwebservices.wcts.operation.TransformResponse;
061 import org.deegree.ogcwebservices.wcts.operation.WCTSGetCapabilities;
062
063 /**
064 * The <code>WCTSHandler</code> is the interface between the {@link WCTService} and the {@link OGCServletController}.
065 * <p>
066 * Currently it is able to understand following operations:
067 * <ul>
068 * <li>WCTSGetCapabilities</li>
069 * <li>GetResourceByID --limited</li>
070 * <li>IsTransformable</li>
071 * <li>Transform</li>
072 * </ul>
073 * </p>
074 * <p>
075 * Not supported is the the {@link GetTransformation} - Operation which --in our humble opinion-- is just another
076 * GetResoureceByID.
077 * </p>
078 *
079 * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
080 *
081 * @author last edited by: $Author:$
082 *
083 * @version $Revision:$, $Date:$
084 *
085 */
086 public class WCTSHandler extends AbstractOWServiceHandler {
087
088 private static ILogger LOG = LoggerFactory.getLogger( WCTSHandler.class );
089
090 private String ip = "UnKnown IP";
091
092 /*
093 * (non-Javadoc)
094 *
095 * @see org.deegree.enterprise.servlet.ServiceDispatcher#perform(org.deegree.ogcwebservices.OGCWebServiceRequest,
096 * javax.servlet.http.HttpServletResponse)
097 */
098 public void perform( OGCWebServiceRequest request, HttpServletResponse response )
099 throws ServiceException, OGCWebServiceException {
100 LOG.logDebug( "Performing request: " + request.toString() );
101
102 try {
103 WCTService service = WCTServiceFactory.createServiceInstance();
104 if ( request instanceof WCTSGetCapabilities ) {
105 performGetCapabilities( service, (WCTSGetCapabilities) request, response );
106 } else if ( request instanceof GetResourceByID ) {
107 performGetResourceByID( service, (GetResourceByID) request, response );
108 } else if ( request instanceof IsTransformable ) {
109 performIsTransformable( service, (IsTransformable) request, response );
110 } else if ( request instanceof Transform ) {
111 performTransform( service, (Transform) request, response );
112 } else if ( request instanceof GetTransformation ) {
113 performGetTransformation( service, (GetTransformation) request, response );
114 } else {
115 LOG.logInfo( "Incoming request is not a WCTS request." );
116 sendException( response, new OGCWebServiceException( "Given request is not known to the WCTS",
117 ExceptionCode.OPERATIONNOTSUPPORTED ) );
118 }
119 } catch ( OGCWebServiceException e ) {
120 LOG.logInfo( "Following error occurred while performing WCTS request (from ip=" + ip + "): "
121 + e.getMessage(), e );
122 sendException( response, e );
123 } catch ( Exception e ) {
124 LOG.logError( "Following fatal error occurred while performing WCTS request: " + e.getMessage(), e );
125 sendException( response, new OGCWebServiceException( getClass().getName(), e.getMessage() ) );
126 }
127 }
128
129 /**
130 * @param requestIP
131 */
132 public void setIP( String requestIP ) {
133 if ( requestIP != null && !"".equals( requestIP ) ) {
134 ip = requestIP;
135 }
136 }
137
138 /**
139 * @param service
140 * to do the handling.
141 * @param request
142 * to handle.
143 * @param response
144 * to write to.
145 * @throws OGCWebServiceException
146 * if for some reason the request can not be dealt with.
147 */
148 private void performGetTransformation( WCTService service, GetTransformation request, HttpServletResponse response )
149 throws OGCWebServiceException {
150 XMLFragment serviceResponse = (XMLFragment) service.doService( request );
151 outputResponse( response, serviceResponse, null );
152 }
153
154 /**
155 * @param service
156 * to do the handling.
157 * @param request
158 * to handle.
159 * @param response
160 * to write to.
161 * @throws OGCWebServiceException
162 * if for some reason the request can not be dealt with.
163 */
164 private void performTransform( WCTService service, Transform request, HttpServletResponse response )
165 throws OGCWebServiceException {
166 request.setRequestIP( ip );
167 TransformResponse serviceResponse = (TransformResponse) service.doService( request );
168 LOG.logDebug( "Transform response: " + serviceResponse );
169 XMLFragment resultDocument = XMLFactory.createResponse( serviceResponse,
170 service.getDeegreeParams().useDeegreeTransformType() );
171 if ( LOG.isDebug() ) {
172 LOG.logDebugXMLFile( "transform_response", resultDocument );
173 }
174 outputResponse( response, resultDocument, request.getOutputFormat() );
175
176 }
177
178 /**
179 * @param service
180 * to do the handling.
181 * @param request
182 * to handle.
183 * @param response
184 * to write to.
185 * @throws OGCWebServiceException
186 * if for some reason the request can not be dealt with.
187 */
188 private void performIsTransformable( WCTService service, IsTransformable request, HttpServletResponse response )
189 throws OGCWebServiceException {
190 XMLFragment serviceResponse = (XMLFragment) service.doService( request );
191 outputResponse( response, serviceResponse, null );
192 }
193
194 /**
195 * @param service
196 * to do the handling.
197 * @param request
198 * to handle.
199 * @param response
200 * to write to.
201 * @throws OGCWebServiceException
202 * if for some reason the request can not be dealt with.
203 */
204 private void performGetResourceByID( WCTService service, GetResourceByID request, HttpServletResponse response )
205 throws OGCWebServiceException {
206 XMLFragment serviceResponse = (XMLFragment) service.doService( request );
207 outputResponse( response, serviceResponse, request.getOutputFormat() );
208 }
209
210 /**
211 * @param service
212 * to do the handling.
213 * @param request
214 * to handle.
215 * @param response
216 * to write to.
217 * @throws OGCWebServiceException
218 * if for some reason the request can not be dealt with.
219 */
220 private void performGetCapabilities( WCTService service, WCTSGetCapabilities request, HttpServletResponse response )
221 throws OGCWebServiceException {
222 XMLFragment serviceResponse = (XMLFragment) service.doService( request );
223 List<String> acceptedFormats = request.getAcceptedFormats();
224 String contentType = "text/xml";
225 for ( String format : acceptedFormats ) {
226 if ( format != null ) {
227 String tmp = format.trim().toLowerCase();
228 if ( tmp.contains( "xml" ) || tmp.contains( "gml" ) ) {
229 contentType = tmp;
230 break;
231 }
232 }
233 }
234 outputResponse( response, serviceResponse, contentType );
235
236 }
237
238 /**
239 * Outputs the given result to the servlet response object and sets the contentType to the given contentType.
240 *
241 * @param response
242 * to output to.
243 * @param result
244 * to output.
245 * @param contentType
246 * to set.
247 */
248 private void outputResponse( HttpServletResponse response, XMLFragment result, String contentType ) {
249 if ( result != null ) {
250 if ( contentType == null || "".equals( contentType.trim() ) ) {
251 contentType = "text/xml;";
252 }
253 response.setContentType( contentType );
254 try {
255 result.prettyPrint( response.getOutputStream() );
256 } catch ( TransformerException e ) {
257 LOG.logError( e.getMessage(), e );
258 } catch ( IOException e ) {
259 LOG.logError( e.getMessage(), e );
260 }
261 } else {
262 sendException( response, new OGCWebServiceException( Messages.getMessage( "WCTS_ILLEGAL_STATE" ),
263 ExceptionCode.NOAPPLICABLECODE ) );
264 }
265
266 }
267
268 }