/* Copyright (c) 2001, 2003 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, availible at the root
* application directory.
*/
package org.vfny.geoserver.wms.responses.map.kml;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Logger;
import org.vfny.geoserver.ServiceException;
import org.vfny.geoserver.global.Service;
import org.vfny.geoserver.wms.GetMapProducer;
import org.vfny.geoserver.wms.WMSMapContext;
import org.vfny.geoserver.wms.WmsException;
import org.vfny.geoserver.wms.responses.map.svg.EncodeSVG;
/**
* Handles a GetMap request that spects a map in KML format.
*
* @author James Macgill
*/
class KMLMapProducer implements GetMapProducer {
/** standard logger */
private static final Logger LOGGER = Logger.getLogger(
"org.vfny.geoserver.responses.wms.kml");
/** encoder instance which does all the hard work */
private EncodeKML kmlEncoder;
/** lag between 'encode' call and 'writeTo' requires temporary storage' */
private File temp;
/**
* Request that encoding be halted if possible.
*
* @param gs The orriginating Service
*/
public void abort(Service gs) {
this.kmlEncoder.abort();
}
/**
* The type for the format generated by this producer.
*
* @return String containing official MIME type for this format.
*/
public String getContentType() {
return KMLMapProducerFactory.MIME_TYPE;
}
/**
* aborts the encoding.
*/
public void abort() {
LOGGER.fine("aborting KML map response");
if (this.kmlEncoder != null) {
LOGGER.info("aborting KML encoder");
this.kmlEncoder.abort();
}
}
/**
* Produce the actual map ready for outputing.
*
* @param map WMSMapContext describing what layers, styles, area of interest
* etc are to be used when producing the map.
*
* @throws WmsException thrown if anything goes wrong during the production.
*/
public void produceMap(WMSMapContext map)
throws WmsException {
try{
temp = File.createTempFile("kml",null);
this.kmlEncoder = new EncodeKML(map);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(temp));
kmlEncoder.encode(bos);
bos.flush();
bos.close();
} catch(IOException ioe){
WmsException we = new WmsException(ioe.getMessage());
we.initCause(ioe);
throw we;
}
}
/**
* Pumps the map to the provided output stream. Note by this point that
* produceMap should already have been called so little work should be done
* within this method.
*
* @param out OutputStream to stream the map to.
*
* @throws ServiceException
* @throws IOException
*
* @TODO replace stream copy with nio code.
*/
public void writeTo(OutputStream out) throws ServiceException, IOException {
FileInputStream fis = new FileInputStream(temp);
byte[] buf = new byte[1024];
int i = 0;
while((i=fis.read(buf))!=-1) {
out.write(buf, 0, i);
}
fis.close();
}
}