// grab the delegate for coverage encoding
GetCoverageType request = (GetCoverageType) operation.getParameters()[0];
String outputFormat = request.getOutput().getFormat();
CoverageResponseDelegate delegate = responseFactory.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());
// use javamail classes to actually encode the document
try {
// coverages xml structure (always set the headers after the data
// handlers, setting
// the data handlers kills some of them)
BodyPart coveragesPart = new MimeBodyPart();
final CoveragesData coveragesData = new CoveragesData(coverageInfo, request);
coveragesPart.setDataHandler(new DataHandler(coveragesData, "geoserver/coverages11"));
coveragesPart.setHeader("Content-ID", "<urn:ogc:wcs:1.1:coverages>");
coveragesPart.setHeader("Content-Type", "text/xml");
multipart.addBodyPart(coveragesPart);
// the actual coverage
BodyPart coveragePart = new MimeBodyPart();
CoverageEncoder encoder = new CoverageEncoder(delegate, coverage, outputFormat, new HashMap<String, String>());
coveragePart.setDataHandler(new DataHandler(encoder, "geoserver/coverageDelegate"));
coveragePart.setHeader("Content-ID", "<theCoverage>");
coveragePart.setHeader("Content-Type", delegate.getMimeType(outputFormat));
coveragePart.setHeader("Content-Transfer-Encoding", "base64");
multipart.addBodyPart(coveragePart);
// write out the multipart (we need to use mime message trying to
// encode directly with multipart or BodyPart does not set properly
// the encodings and binary files gets ruined
MimeMessage message = new GeoServerMimeMessage();
message.setContent(multipart);
message.writeTo(output);
output.flush();
} catch (MessagingException e) {
throw new WcsException("Error occurred while encoding the mime multipart response", e);
}
}