001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/enterprise/servlet/WPVSHandler.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: 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:
044: package org.deegree.enterprise.servlet;
045:
046: import java.awt.Color;
047: import java.awt.Dimension;
048: import java.awt.Graphics;
049: import java.awt.Image;
050: import java.awt.image.BufferedImage;
051: import java.io.IOException;
052: import java.io.OutputStream;
053: import java.io.PrintWriter;
054:
055: import javax.servlet.http.HttpServletResponse;
056:
057: import org.deegree.enterprise.ServiceException;
058: import org.deegree.framework.log.ILogger;
059: import org.deegree.framework.log.LoggerFactory;
060: import org.deegree.framework.util.CharsetUtils;
061: import org.deegree.framework.util.ImageUtils;
062: import org.deegree.framework.util.MimeTypeMapper;
063: import org.deegree.framework.util.StringTools;
064: import org.deegree.framework.xml.DOMPrinter;
065: import org.deegree.framework.xml.Marshallable;
066: import org.deegree.ogcwebservices.OGCWebService;
067: import org.deegree.ogcwebservices.OGCWebServiceException;
068: import org.deegree.ogcwebservices.OGCWebServiceRequest;
069: import org.deegree.ogcwebservices.wpvs.WPVService;
070: import org.deegree.ogcwebservices.wpvs.WPVServiceFactory;
071: import org.deegree.ogcwebservices.wpvs.XMLFactory;
072: import org.deegree.ogcwebservices.wpvs.capabilities.WPVSCapabilities;
073: import org.deegree.ogcwebservices.wpvs.capabilities.WPVSCapabilitiesDocument;
074: import org.deegree.ogcwebservices.wpvs.configuration.WPVSConfiguration;
075: import org.deegree.ogcwebservices.wpvs.operation.Get3DFeatureInfoResponse;
076: import org.deegree.ogcwebservices.wpvs.operation.GetView;
077: import org.deegree.ogcwebservices.wpvs.operation.GetViewResponse;
078: import org.w3c.dom.Node;
079:
080: /**
081: * Handler for the Web Perspective View Service (WPVS).
082: *
083: * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
084: * @author last edited by: $Author: apoth $
085: *
086: * @version 2.0, $Revision: 9338 $, $Date: 2007-12-27 04:31:31 -0800 (Thu, 27 Dec 2007) $
087: *
088: * @since 2.0
089: */
090: public class WPVSHandler extends AbstractOWServiceHandler {
091:
092: private static ILogger LOG = LoggerFactory
093: .getLogger(WPVSHandler.class);
094:
095: /**
096: * Performs the passed OGCWebServiceRequest by accessing service from the
097: * pool and passing the request to it
098: *
099: * @param request the incoming web service request
100: * @param httpResponse the outgoing web serivce response
101: * @throws ServiceException
102: * @throws OGCWebServiceException
103: */
104: public void perform(OGCWebServiceRequest request,
105: HttpServletResponse httpResponse) throws ServiceException,
106: OGCWebServiceException {
107:
108: LOG.logDebug(StringTools.concat(200, "Performing request: ",
109: request.toString()));
110:
111: OGCWebService service = WPVServiceFactory.createInstance();
112:
113: try {
114: Object response = service.doService(request);
115: if (response instanceof WPVSCapabilities) {
116: sendGetCapabilitiesResponse(httpResponse,
117: (WPVSCapabilities) response);
118: } else if (response instanceof GetViewResponse) {
119: sendGetViewResponse(httpResponse,
120: (GetViewResponse) response);
121: } else if (response instanceof Get3DFeatureInfoResponse) {
122: sendGet3DFeatureInfoResponse(httpResponse,
123: (Get3DFeatureInfoResponse) response);
124: } else {
125: String s = (response == null ? "null response object"
126: : response.getClass().getName());
127: //this is not really nice...because excepts get cought later on below
128: throw new OGCWebServiceException(getClass().getName(),
129: StringTools.concat(200,
130: "Unknown response class: '", s, "'."));
131: }
132: } catch (OGCWebServiceException e) {
133:
134: LOG.logError("Error performing WPVFS request.", e);
135: if (request instanceof GetView
136: && ("INIMAGE".equalsIgnoreCase(((GetView) request)
137: .getExceptionFormat()))) {
138: sendExceptionImage(httpResponse, e, (GetView) request);
139:
140: } else {
141: sendException(httpResponse, e);
142: }
143: }
144:
145: }
146:
147: // TODO common to WMS
148: private void sendExceptionImage(HttpServletResponse httpResponse,
149: OGCWebServiceException e, GetView request) {
150:
151: Dimension d = request.getImageDimension();
152:
153: BufferedImage bi = new BufferedImage(d.width, d.height,
154: BufferedImage.TYPE_INT_RGB);
155: Graphics g = bi.getGraphics();
156: g.setColor(Color.WHITE);
157: g.fillRect(0, 0, d.width, d.height);
158: g.setColor(Color.BLUE);
159:
160: String s = e.getLocator();
161: String location = s != null ? s : "Unknown";
162: s = e.getMessage();
163: String message = s != null ? s : "Unknown reason!";
164:
165: g.drawString(location, 5, 20);
166: g.drawString(message, 15, 50);
167: String mime = MimeTypeMapper.toMimeType(request
168: .getOutputFormat());
169: g.dispose();
170: writeImage(bi, mime, httpResponse);
171: }
172:
173: //TODO common to WMS
174: private void writeImage(Object output, String mime,
175: HttpServletResponse resp) {
176: try {
177: OutputStream os = null;
178: resp.setContentType(mime);
179:
180: if (mime.equalsIgnoreCase("image/gif")) {
181: os = resp.getOutputStream();
182: ImageUtils.saveImage((BufferedImage) output, os, "gif",
183: 1);
184: } else if (mime.equalsIgnoreCase("image/jpg")
185: || mime.equalsIgnoreCase("image/jpeg")) {
186: os = resp.getOutputStream();
187: ImageUtils.saveImage((BufferedImage) output, os,
188: "jpeg", 1);
189: } else if (mime.equalsIgnoreCase("image/png")) {
190: os = resp.getOutputStream();
191: ImageUtils.saveImage((BufferedImage) output, os, "png",
192: 1);
193: } else if (mime.equalsIgnoreCase("image/tif")
194: || mime.equalsIgnoreCase("image/tiff")) {
195: os = resp.getOutputStream();
196: ImageUtils.saveImage((BufferedImage) output, os, "tif",
197: 1);
198: } else if (mime.equalsIgnoreCase("image/bmp")) {
199: os = resp.getOutputStream();
200: ImageUtils.saveImage((BufferedImage) output, os, "bmp",
201: 1);
202: } else if (mime.equalsIgnoreCase("image/svg+xml")) {
203: os = resp.getOutputStream();
204: PrintWriter pw = new PrintWriter(os);
205: DOMPrinter.printNode(pw, (Node) output);
206: pw.close();
207: } else {
208: resp.setContentType("text/xml; charset="
209: + CharsetUtils.getSystemCharset());
210: os = resp.getOutputStream();
211: OGCWebServiceException exce = new OGCWebServiceException(
212: "WMS:writeImage", "unsupported image format: "
213: + mime);
214: os
215: .write(((Marshallable) exce).exportAsXML()
216: .getBytes());
217: }
218:
219: os.close();
220: } catch (Exception e) {
221: LOG.logError(e.getMessage(), e);
222: }
223: }
224:
225: /**
226: * Sends the result of a someWPVService.doService( request ) bacn to the client
227: * @param httpResponse the response object used to pipe the result
228: * @param getViewResponse the actua result to be sent
229: */
230: private void sendGetViewResponse(HttpServletResponse httpResponse,
231: GetViewResponse getViewResponse) {
232:
233: String mime = MimeTypeMapper.toMimeType(getViewResponse
234: .getOutputFormat());
235: httpResponse.setContentType(mime);
236:
237: //GetView response is, for the time being, always an image
238: writeImage(getViewResponse.getOutput(), httpResponse, mime);
239:
240: System.gc();
241: }
242:
243: /**
244: * Sends the response to a GetCapabilities request to the client.
245: *
246: * @param httpResponse
247: * @param capabilities
248: * @throws OGCWebServiceException
249: * if an exception occurs which can be propagated to the client
250: */
251: private void sendGetCapabilitiesResponse(
252: HttpServletResponse httpResponse,
253: WPVSCapabilities capabilities)
254: throws OGCWebServiceException {
255: try {
256: httpResponse.setContentType("text/xml");
257: WPVSCapabilitiesDocument document = XMLFactory
258: .export(capabilities);
259: document.write(httpResponse.getOutputStream());
260: } catch (IOException e) {
261: LOG.logError("Error sending GetCapabilities response.", e);
262:
263: throw new OGCWebServiceException(getClass().getName(),
264: "Error exporting capabilities for GetCapabilities response.");
265: }
266: }
267:
268: /**
269: * Sends the response to a Get3DFeatureInfo request to the client.
270: *
271: * @param httpResponse
272: * @param response
273: * @throws OGCWebServiceException
274: * if an exception occurs which can be propagated to the client
275: */
276: private void sendGet3DFeatureInfoResponse(
277: HttpServletResponse httpResponse,
278: Get3DFeatureInfoResponse response)
279: throws OGCWebServiceException {
280: try {
281: httpResponse.setContentType("text/xml");
282:
283: PrintWriter pw = httpResponse.getWriter();
284: String s = response.get3DFeatureInfo();
285: pw.print(s);
286: } catch (IOException e) {
287: LOG.logError("Error sendingGet3DFeatureInfo response.", e);
288:
289: throw new OGCWebServiceException(getClass().getName(),
290: "Error exporting Info for Get3DFeatureInfo response.");
291: }
292: }
293:
294: /**
295: * Writes an output of a GetView request to the <code>httpResponse</code> using the
296: * <code>mime</code> type.
297: * @param output the image to be sent back
298: * @param httpResponse the response to pipe the image
299: * @param mime the type of image
300: */
301: private void writeImage(Image output,
302: HttpServletResponse httpResponse, String mime) {
303: try {
304:
305: OutputStream os = httpResponse.getOutputStream();
306: httpResponse.setContentType(mime);
307:
308: if (mime.equalsIgnoreCase("image/jpg")
309: || mime.equalsIgnoreCase("image/jpeg")) {
310:
311: OGCWebService service = WPVServiceFactory
312: .createInstance();
313: WPVSConfiguration config = (WPVSConfiguration) ((WPVService) service)
314: .getCapabilities();
315: float quality = config.getDeegreeParams()
316: .getViewQuality();
317: ImageUtils.saveImage((BufferedImage) output, os,
318: "jpeg", quality);
319: } else if (mime.equalsIgnoreCase("image/png")) {
320: ImageUtils.saveImage((BufferedImage) output, os, "png",
321: 1);
322: } else if (mime.equalsIgnoreCase("image/tif")
323: || mime.equalsIgnoreCase("image/tiff")) {
324: ImageUtils.saveImage((BufferedImage) output, os,
325: "tiff", 1);
326: } else if (mime.equalsIgnoreCase("image/bmp")) {
327: ImageUtils.saveImage((BufferedImage) output, os, "bmp",
328: 1);
329: } else {
330: httpResponse.setContentType("text/xml");
331: os = httpResponse.getOutputStream();
332: OGCWebServiceException exce = new OGCWebServiceException(
333: "WMS:writeImage", "unsupported image format: "
334: + mime);
335: os
336: .write(((Marshallable) exce).exportAsXML()
337: .getBytes());
338: }
339:
340: os.close();
341: } catch (Exception e) {
342: LOG.logError("-", e);
343: }
344: }
345: }
|