001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wms.responses.map.kml;
006:
007: import org.geoserver.wms.WebMapService;
008: import org.vfny.geoserver.wms.requests.GetMapRequest;
009: import org.vfny.geoserver.wms.responses.GetMapResponse;
010: import java.util.Map;
011:
012: import javax.servlet.http.HttpServletResponse;
013:
014: /**
015: * KML reflecting service.
016: * <p>
017: * This
018: * </p>
019: *
020: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
021: *
022: */
023: public class KMLReflector {
024: /**
025: * kml/kmz mime types
026: */
027: public static final String KML_MIME_TYPE = "application/vnd.google-earth.kml+xml";
028: public static final String KMZ_MIME_TYPE = "application/vnd.google-earth.kmz+xml";
029:
030: /** default 'kmscore' value */
031: public static final Integer KMSCORE = new Integer(50);
032:
033: /** default 'kmattr' value */
034: public static final Boolean KMATTR = Boolean.TRUE;
035:
036: /** default 'format' value */
037: public static final String FORMAT = KML_MIME_TYPE;
038:
039: /**
040: * web map service
041: */
042: WebMapService wms;
043:
044: public KMLReflector(WebMapService wms) {
045: this .wms = wms;
046: }
047:
048: public void wms(GetMapRequest request, HttpServletResponse response)
049: throws Exception {
050:
051: //first set up some of the normal wms defaults
052: if (request.getWidth() < 1) {
053: request.setWidth(1024);
054: }
055: if (request.getHeight() < 1) {
056: request.setHeight(1024);
057: }
058:
059: //set rest of the wms defaults
060: wms.reflect(request);
061:
062: //set some kml specific defaults
063: Map fo = request.getFormatOptions();
064: if (fo.get("kmattr") == null) {
065: fo.put("kmattr", KMATTR);
066: }
067: if (fo.get("kmscore") == null) {
068: fo.put("kmscore", KMSCORE);
069: }
070:
071: //set the format
072: //TODO: create a subclass of GetMapRequest to store these values
073: if (request.getSuperOverlay()) {
074: request.setFormat(KML_MIME_TYPE);
075: } else {
076: request.setFormat(KMZ_MIME_TYPE);
077: }
078:
079: response.setContentType(KML_MIME_TYPE);
080:
081: //set the content disposition
082: StringBuffer filename = new StringBuffer();
083: for (int i = 0; i < request.getLayers().length; i++) {
084: String name = request.getLayers()[i].getName();
085:
086: //strip off prefix
087: int j = name.indexOf(':');
088: if (j > -1) {
089: name = name.substring(j + 1);
090: }
091:
092: filename.append(name + "_");
093: }
094: filename.setLength(filename.length() - 1);
095: response.setHeader("Content-Disposition",
096: "attachment; filename=" + filename.toString() + ".kml");
097:
098: KMLNetworkLinkTransformer transformer = new KMLNetworkLinkTransformer();
099: transformer.setIndentation(3);
100: transformer.setEncodeAsRegion(request.getSuperOverlay());
101: transformer.transform(request, response.getOutputStream());
102:
103: }
104: }
|