001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/enterprise/servlet/WMPSHandler.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
037 package org.deegree.enterprise.servlet;
038
039 import java.io.PrintWriter;
040
041 import javax.servlet.http.HttpServletResponse;
042
043 import org.deegree.framework.log.ILogger;
044 import org.deegree.framework.log.LoggerFactory;
045 import org.deegree.framework.util.NetWorker;
046 import org.deegree.framework.util.StringTools;
047 import org.deegree.framework.xml.DOMPrinter;
048 import org.deegree.framework.xml.XMLFragment;
049 import org.deegree.ogcwebservices.OGCWebService;
050 import org.deegree.ogcwebservices.OGCWebServiceException;
051 import org.deegree.ogcwebservices.OGCWebServiceRequest;
052 import org.deegree.ogcwebservices.OGCWebServiceResponse;
053 import org.deegree.ogcwebservices.wmps.WMPService;
054 import org.deegree.ogcwebservices.wmps.WMPServiceFactory;
055 import org.deegree.ogcwebservices.wmps.XMLFactory;
056 import org.deegree.ogcwebservices.wmps.capabilities.WMPSCapabilities;
057 import org.deegree.ogcwebservices.wmps.capabilities.WMPSCapabilitiesDocument;
058 import org.deegree.ogcwebservices.wmps.configuration.WMPSConfiguration;
059 import org.deegree.ogcwebservices.wmps.operation.DescribeTemplateResponseDocument;
060 import org.deegree.ogcwebservices.wmps.operation.GetAvailableTemplatesResponseDocument;
061 import org.deegree.ogcwebservices.wmps.operation.PrintMapResponseDocument;
062 import org.deegree.ogcwebservices.wmps.operation.WMPSGetCapabilitiesResult;
063 import org.deegree.ogcwebservices.wms.InvalidFormatException;
064
065 /**
066 *
067 * Web servlet client for WMPS.
068 *
069 * @author <a href="mailto:deshmukh@lat-lon.de">Anup Deshmukh</a>
070 *
071 * @author last edited by: $Author: apoth $
072 *
073 * @version 2.0, $Revision: 21101 $, $Date: 2009-11-27 13:51:01 +0100 (Fr, 27. Nov 2009) $
074 *
075 * @since 2.0
076 */
077
078 public class WMPSHandler extends AbstractOWServiceHandler {
079
080 private static ILogger LOG = LoggerFactory.getLogger( WMPSHandler.class );
081
082 private HttpServletResponse resp = null;
083
084 private WMPSConfiguration configuration = null;
085
086 /**
087 * Create an empty WMPSHandler
088 */
089 WMPSHandler() {
090 LOG.logDebug( "New WMPSHandler instance created: " + this.getClass().getName() );
091 }
092
093 /**
094 * performs the passed OGCWebServiceRequest by accessing service from the pool and passing the request to it
095 */
096 public void perform( OGCWebServiceRequest request, HttpServletResponse response )
097 throws OGCWebServiceException {
098
099 resp = response;
100 OGCWebService service;
101 try {
102 service = WMPServiceFactory.getService();
103 } catch ( Exception e ) {
104 throw new OGCWebServiceException( "Error performing the WMPService(s)." );
105 }
106 configuration = (WMPSConfiguration) ( (WMPService) service ).getCapabilities();
107 // if ( service == null ) {
108 // OGCWebServiceException exce = new OGCWebServiceException( "WMPS:WMPS",
109 // "could not access a WMPService instance" );
110 // sendException( response, exce );
111 // return;
112 // }
113 Object o = service.doService( request );
114 handleResponse( o );
115 }
116
117 /**
118 *
119 *
120 * @param result
121 */
122 private void handleResponse( Object result ) {
123
124 try {
125 OGCWebServiceResponse response = (OGCWebServiceResponse) result;
126 if ( response.getException() != null ) {
127 // handle the case that an exception occured during the
128 // request performance
129 OGCWebServiceException exce = response.getException();
130 sendException( resp, exce );
131 } else {
132 if ( response instanceof WMPSGetCapabilitiesResult ) {
133 handleGetCapabilitiesResponse( (WMPSGetCapabilitiesResult) response );
134 } else if ( response instanceof PrintMapResponseDocument ) {
135 handlePrintMapResponse( (PrintMapResponseDocument) response );
136 } else if ( response instanceof GetAvailableTemplatesResponseDocument ) {
137 handleGetAvailableTemplatesResponse( (GetAvailableTemplatesResponseDocument) response );
138 } else if ( response instanceof DescribeTemplateResponseDocument ) {
139 handleDescribeTemplateResponse( (DescribeTemplateResponseDocument) response );
140 }
141 }
142 } catch ( InvalidFormatException ife ) {
143 LOG.logError( ife.getMessage(), ife );
144 OGCWebServiceException exce = new OGCWebServiceException( "InvalidFormat", ife.getMessage() );
145 sendException( resp, exce );
146 } catch ( Exception e ) {
147 LOG.logError( e.getMessage(), e );
148 OGCWebServiceException exce = new OGCWebServiceException( "WMPS:write", e.getMessage() );
149 sendException( resp, exce );
150 }
151
152 }
153
154 /**
155 * @param response
156 */
157 private void handleDescribeTemplateResponse( DescribeTemplateResponseDocument response ) {
158 resp.setContentType( "application/vnd.ogc.wms_xml" );
159
160 XMLFragment xml = new XMLFragment( response.getRootElement() );
161 try {
162 PrintWriter pw = resp.getWriter();
163 xml.prettyPrint( pw );
164 pw.close();
165 } catch ( Exception e ) {
166 LOG.logError( "-", e );
167 }
168 }
169
170 /**
171 * @param response
172 */
173 private void handleGetAvailableTemplatesResponse( GetAvailableTemplatesResponseDocument response ) {
174 resp.setContentType( "application/vnd.ogc.wms_xml" );
175
176 XMLFragment xml = new XMLFragment( response.getRootElement() );
177 try {
178 PrintWriter pw = resp.getWriter();
179 xml.prettyPrint( pw );
180 pw.close();
181 } catch ( Exception e ) {
182 LOG.logError( "-", e );
183 }
184 }
185
186 /**
187 * handles the response to a get capabilities request
188 *
189 * @param response
190 */
191 private void handleGetCapabilitiesResponse( WMPSGetCapabilitiesResult response )
192 throws Exception {
193
194 resp.setContentType( "application/vnd.ogc.wms_xml" );
195 WMPSCapabilities capa = response.getCapabilities();
196 WMPSCapabilitiesDocument doc = XMLFactory.export( capa );
197
198 // XMLFragment frag = doc.transform( url.openStream() , XMLFragment.DEFAULT_URL );
199 String xml = DOMPrinter.nodeToString( doc.getRootElement(), "" );
200
201 String dtd = NetWorker.url2String( configuration.getDeegreeParams().getDTDLocation() );
202 StringBuffer sb = new StringBuffer();
203 sb.append( "<!DOCTYPE WMT_PS_Capabilities SYSTEM " );
204 sb.append( "'" + dtd + "' \n" );
205 sb.append( "[\n<!ELEMENT VendorSpecificCapabilities EMPTY>\n]>" );
206
207 xml = StringTools.replace( xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
208 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n" + sb.toString(), false );
209 xml = StringTools.replace( xml, "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "", false );
210
211 try {
212 PrintWriter pw = resp.getWriter();
213 pw.print( xml );
214 pw.close();
215 } catch ( Exception e ) {
216 LOG.logError( "-", e );
217 }
218
219 }
220
221 /**
222 * handles the response to a print map request
223 *
224 * @param response
225 */
226 private void handlePrintMapResponse( PrintMapResponseDocument response ) {
227
228 resp.setContentType( "application/vnd.ogc.wms_xml" );
229
230 XMLFragment xml = new XMLFragment( response.getRootElement() );
231 try {
232 PrintWriter pw = resp.getWriter();
233 xml.prettyPrint( pw );
234 pw.close();
235 } catch ( Exception e ) {
236 LOG.logError( "-", e );
237 }
238
239 }
240
241 }