private void deliverProductFile(HttpServletRequest req,
HttpServletResponse res, int index, String productID)
throws CatalogException, IOException {
Product product = client.getProductById(productID);
List refs = client.getProductReferences(product);
Reference ref = (Reference) refs.get(index);
res.addHeader(CONTENT_LENGTH_HDR, String.valueOf(ref.getFileSize()));
String contentType = (ref.getMimeType() != null
&& ref.getMimeType().getName() != null && !ref.getMimeType().getName()
.equals("")) ? ref.getMimeType().getName() : DataUtils
.guessTypeFromName(ref.getDataStoreReference());
res.addHeader(CONTENT_TYPE_HDR, contentType);
try {
res.addHeader(CONTENT_DISPOSITION_HDR, "attachment; filename=\""
+ new File(new URI(ref.getDataStoreReference())).getName() + "\"");
} catch (URISyntaxException e) {
LOG.log(Level.WARNING,
"Unable to sense filename from data store URI: Message: "
+ e.getMessage());
}
URL url = new URL(ref.getDataStoreReference());
URLConnection c = url.openConnection();
InputStream in = c.getInputStream();
OutputStream out = res.getOutputStream();
byte[] buf = new byte[512];
int n;