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 java.io.IOException;
008: import java.io.OutputStream;
009: import java.util.logging.Logger;
010:
011: import javax.xml.transform.TransformerException;
012:
013: import org.geotools.map.MapLayer;
014: import org.vfny.geoserver.ServiceException;
015: import org.vfny.geoserver.global.Service;
016: import org.vfny.geoserver.wms.GetMapProducer;
017: import org.vfny.geoserver.wms.WmsException;
018: import org.vfny.geoserver.wms.responses.AbstractGetMapProducer;
019:
020: /**
021: * Handles a GetMap request that spects a map in KML format.
022: *
023: * @author James Macgill
024: */
025: class KMLMapProducer extends AbstractGetMapProducer implements
026: GetMapProducer {
027: /** standard logger */
028: protected static final Logger LOGGER = org.geotools.util.logging.Logging
029: .getLogger("org.vfny.geoserver.responses.wms.kml");
030:
031: /**
032: * encoder instance which does all the hard work
033: *
034: * @uml.property name="kmlEncoder"
035: * @uml.associationEnd multiplicity="(0 1)"
036: */
037:
038: // private EncodeKML kmlEncoder;
039: /** kml transformer which turns the map contedxt into kml */
040: protected KMLTransformer transformer;
041:
042: public KMLMapProducer(String mapFormat, String mime_type) {
043: super (mapFormat, mime_type);
044: }
045:
046: /**
047: * Request that encoding be halted if possible.
048: *
049: * @param gs
050: * The orriginating Service
051: */
052: public void abort(Service gs) {
053: if (transformer != null) {
054: // transformer.abort();
055: }
056: }
057:
058: /**
059: * aborts the encoding.
060: */
061: public void abort() {
062: LOGGER.fine("aborting KML map response");
063:
064: // if (this.kmlEncoder != null) {
065: // LOGGER.info("aborting KML encoder");
066: // this.kmlEncoder.abort();
067: // }
068: if (transformer != null) {
069: LOGGER.info("aborting KML encoder");
070:
071: // transformer.abort();
072: }
073: }
074:
075: /**
076: * Produce the actual map ready for outputing.
077: *
078: * @param map
079: * WMSMapContext describing what layers, styles, area of interest
080: * etc are to be used when producing the map.
081: *
082: * @throws WmsException
083: * thrown if anything goes wrong during the production.
084: */
085: public void produceMap() throws WmsException {
086: transformer = new KMLTransformer();
087:
088: // TODO: use GeoServer.isVerbose() to determine if we should indent?
089: transformer.setIndentation(3);
090: }
091:
092: /**
093: * Pumps the map to the provided output stream. Note by this point that
094: * produceMap should already have been called so little work should be done
095: * within this method.
096: *
097: * @param out
098: * OutputStream to stream the map to.
099: *
100: * @throws ServiceException
101: * @throws IOException
102: *
103: * @TODO replace stream copy with nio code.
104: */
105: public void writeTo(OutputStream out) throws ServiceException,
106: IOException {
107: try {
108: transformer.transform(mapContext, out);
109: } catch (TransformerException e) {
110: throw (IOException) new IOException().initCause(e);
111: }
112: }
113:
114: public String getContentDisposition() {
115: StringBuffer sb = new StringBuffer();
116: for (int i = 0; i < mapContext.getLayerCount(); i++) {
117: MapLayer layer = mapContext.getLayer(i);
118: String title = layer.getFeatureSource().getSchema()
119: .getTypeName();
120: if (title != null && !title.equals("")) {
121: sb.append(title).append("_");
122: }
123: }
124: if (sb.length() > 0) {
125: sb.setLength(sb.length() - 1);
126: return "attachment; filename=" + sb.toString() + ".kml";
127: }
128: return "attachment; filename=geoserver.kml";
129: }
130: }
|