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.wcs.responses.coverage;
006:
007: import org.geotools.coverage.grid.GridCoverage2D;
008: import org.geotools.gce.gtopo30.GTopo30Writer;
009: import org.opengis.coverage.grid.GridCoverageWriter;
010: import org.vfny.geoserver.ServiceException;
011: import org.vfny.geoserver.global.GeoServer;
012: import org.vfny.geoserver.wcs.responses.CoverageResponseDelegate;
013: import java.io.IOException;
014: import java.io.OutputStream;
015: import java.util.zip.ZipOutputStream;
016:
017: /**
018: * @author simone giannecchini
019: * @author alesio fabiani
020: *
021: * TODO To change the template for this generated type comment go to Window -
022: * Preferences - Java - Code Style - Code Templates
023: */
024: public class GTopo30CoverageResponseDelegate implements
025: CoverageResponseDelegate {
026: /**
027: * the grid coverage to be used in this repsonse
028: *
029: * @uml.property name="sourceCoverage"
030: * @uml.associationEnd multiplicity="(0 1)"
031: */
032: private GridCoverage2D sourceCoverage;
033:
034: /*
035: * (non-Javadoc)
036: *
037: * @see org.vfny.geoserver.wcs.responses.CoverageResponseDelegate#canProduce(java.lang.String)
038: */
039: public boolean canProduce(String outputFormat) {
040: if (outputFormat.compareToIgnoreCase("GTopo30") == 0) {
041: return true;
042: }
043:
044: return false;
045: }
046:
047: /*
048: * (non-Javadoc)
049: *
050: * @see org.vfny.geoserver.wcs.responses.CoverageResponseDelegate#prepare(java.lang.String,
051: * org.geotools.coverage.grid.GridCoverage2D)
052: */
053: public void prepare(String outputFormat, GridCoverage2D coverage)
054: throws IOException {
055: this .sourceCoverage = coverage;
056: }
057:
058: /*
059: * (non-Javadoc)
060: *
061: * @see org.vfny.geoserver.wcs.responses.CoverageResponseDelegate#getContentType(org.vfny.geoserver.global.GeoServer)
062: */
063: public String getContentType(GeoServer gs) {
064: return "application/x-zip";
065: }
066:
067: /*
068: * (non-Javadoc)
069: *
070: * @see org.vfny.geoserver.wcs.responses.CoverageResponseDelegate#getContentEncoding()
071: */
072: public String getContentEncoding() {
073: // return "zip";
074: return null;
075: }
076:
077: /**
078: * DOCUMENT ME!
079: *
080: * @return DOCUMENT ME!
081: */
082: public String getContentDisposition() {
083: return new StringBuffer("attachment;filename=").append(
084: this .sourceCoverage.getName()).append(".zip")
085: .toString();
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see org.vfny.geoserver.wcs.responses.CoverageResponseDelegate#encode(java.io.OutputStream)
092: */
093: public void encode(OutputStream output) throws ServiceException,
094: IOException {
095: // creating a zip outputstream
096: final ZipOutputStream outZ = new ZipOutputStream(output);
097: output = outZ;
098:
099: // creating a writer
100: final GridCoverageWriter writer = new GTopo30Writer(outZ);
101:
102: // writing
103: if (writer != null) {
104: writer.write(sourceCoverage, null);
105: } else {
106: throw new ServiceException(
107: "Could not create a writer for the format Gtopo30!");
108: }
109:
110: // freeing everything
111: writer.dispose();
112: this.sourceCoverage.dispose();
113: this.sourceCoverage = null;
114: }
115: }
|