public void write(Object value, OutputStream output, Operation operation) throws IOException,
ServiceException {
GridCoverage[] coverages = (GridCoverage[]) value;
// grab the delegate for coverage encoding
GetCoverageType request = (GetCoverageType) operation.getParameters()[0];
String outputFormat = request.getOutput().getFormat();
CoverageResponseDelegate delegate = CoverageResponseDelegateFactory
.encoderFor(outputFormat);
if (delegate == null)
throw new WcsException("Could not find encoder for output format " + outputFormat);
// grab the coverage info for Coverages document encoding
final GridCoverage2D coverage = (GridCoverage2D) coverages[0];
CoverageInfo coverageInfo = catalog.getCoverageByName(request.getIdentifier().getValue());
// write the coverage to temporary storage in the data dir
File wcsStore = null;
try {
File temp = GeoserverDataDirectory.findCreateConfigDir("temp");
wcsStore = new File(temp, "wcs");
if(!wcsStore.exists())
wcsStore.mkdir();
} catch(Exception e) {
throw new WcsException("Could not create the temporary storage directory for WCS");
}
// Make sure we create a file name that's not already there (even if splitting the same nanosecond
// with two requests should not ever happen...)
File coverageFile = null;
while(true) {
// TODO: find a way to get good extensions
coverageFile = new File(wcsStore, coverageInfo.getName().replace(':', '_') + "_" + System.nanoTime() + "." + delegate.getFileExtension());
if(!coverageFile.exists())
break;
}
// store the coverage
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(coverageFile));
delegate.prepare(outputFormat, coverage);
delegate.encode(os);
os.flush();
} finally {
if(os != null) os.close();
}
System.out.println(coverageFile);
// build the path where the clients will be able to retrieve the coverage files
final String coverageLocation = buildURL(request.getBaseUrl(),
appendPath("temp/wcs", coverageFile.getName()), null, URLType.RESOURCE);
// build the response
CoveragesTransformer tx = new CoveragesTransformer(wcs, request, coverageLocation);
try {