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.arcgrid.ArcGridWriter;
009: import org.opengis.coverage.grid.GridCoverageWriter;
010: import org.opengis.parameter.ParameterValueGroup;
011: import org.vfny.geoserver.ServiceException;
012: import org.vfny.geoserver.global.GeoServer;
013: import org.vfny.geoserver.wcs.WcsException;
014: import org.vfny.geoserver.wcs.responses.CoverageResponseDelegate;
015: import java.io.IOException;
016: import java.io.OutputStream;
017: import java.util.zip.GZIPOutputStream;
018:
019: /**
020: * DOCUMENT ME!
021: *
022: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last
023: * modification)
024: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last
025: * modification)
026: */
027: public class AscCoverageResponseDelegate implements
028: CoverageResponseDelegate {
029: /**
030: *
031: * @uml.property name="sourceCoverage"
032: * @uml.associationEnd multiplicity="(0 1)"
033: */
034: private GridCoverage2D sourceCoverage;
035: private boolean compressOutput = false;
036:
037: public AscCoverageResponseDelegate() {
038: }
039:
040: public boolean canProduce(String outputFormat) {
041: return "ArcGrid".equalsIgnoreCase(outputFormat)
042: || "ArcGrid-GZIP".equalsIgnoreCase(outputFormat);
043: }
044:
045: public void prepare(String outputFormat, GridCoverage2D coverage)
046: throws IOException {
047: this .compressOutput = "ArcGrid-GZIP"
048: .equalsIgnoreCase(outputFormat);
049: this .sourceCoverage = coverage;
050: }
051:
052: public String getContentType(GeoServer gs) {
053: // return gs.getMimeType();
054: return compressOutput ? "application/x-gzip" : "text/plain";
055: }
056:
057: /**
058: * DOCUMENT ME!
059: *
060: * @return DOCUMENT ME!
061: */
062: public String getContentEncoding() {
063: // return compressOutput ? "gzip" : null;
064: return null;
065: }
066:
067: /**
068: * DOCUMENT ME!
069: *
070: * @return DOCUMENT ME!
071: */
072: public String getContentDisposition() {
073: return compressOutput ? ("attachment;filename="
074: + this .sourceCoverage.getName() + ".asc.gz") : null;
075: }
076:
077: public void encode(OutputStream output) throws ServiceException,
078: IOException {
079: if (sourceCoverage == null) {
080: throw new IllegalStateException(new StringBuffer(
081: "It seems prepare() has not been called").append(
082: " or has not succeed").toString());
083: }
084:
085: GZIPOutputStream gzipOut = null;
086:
087: if (compressOutput) {
088: gzipOut = new GZIPOutputStream(output);
089: output = gzipOut;
090: }
091:
092: try {
093: final GridCoverageWriter writer = new ArcGridWriter(output);
094: final ParameterValueGroup params = writer.getFormat()
095: .getWriteParameters();
096: //params.parameter("Compressed").setValue(compressOutput);
097: writer.write(sourceCoverage, null);
098:
099: if (gzipOut != null) {
100: gzipOut.finish();
101: gzipOut.flush();
102: }
103:
104: // freeing everything
105: writer.dispose();
106: this .sourceCoverage.dispose();
107: this .sourceCoverage = null;
108: } catch (Exception e) {
109: throw new WcsException(new StringBuffer(
110: "Problems Rendering Image").append(e.getMessage())
111: .toString(), e);
112: }
113: }
114: }
|