final InputStream data = stream.getStream();
if (data != null) {
- // Sets content type
- response.setContentType(stream.getContentType());
-
- // Sets cache headers
- final long cacheTime = stream.getCacheTime();
- if (cacheTime <= 0) {
- response.setProperty("Cache-Control", "no-cache");
- response.setProperty("Pragma", "no-cache");
- response.setProperty("Expires", "0");
- } else {
- response.setProperty("Cache-Control", "max-age=" + cacheTime
- / 1000);
- response.setProperty("Expires", "" + System.currentTimeMillis()
- + cacheTime);
- // Required to apply caching in some Tomcats
- response.setProperty("Pragma", "cache");
- }
+ OutputStream out = null;
+ try {
+
+ // Sets content type
+ response.setContentType(stream.getContentType());
- // Copy download stream parameters directly
- // to HTTP headers.
- final Iterator<String> i = stream.getParameterNames();
- if (i != null) {
- while (i.hasNext()) {
- final String param = i.next();
- response.setProperty(param, stream.getParameter(param));
+ // Sets cache headers
+ final long cacheTime = stream.getCacheTime();
+ if (cacheTime <= 0) {
+ response.setProperty("Cache-Control", "no-cache");
+ response.setProperty("Pragma", "no-cache");
+ response.setProperty("Expires", "0");
+ } else {
+ response.setProperty("Cache-Control", "max-age="
+ + cacheTime / 1000);
+ response.setProperty("Expires",
+ "" + System.currentTimeMillis() + cacheTime);
+ // Required to apply caching in some Tomcats
+ response.setProperty("Pragma", "cache");
}
- }
- // suggest local filename from DownloadStream if Content-Disposition
- // not explicitly set
- String contentDispositionValue = stream
- .getParameter("Content-Disposition");
- if (contentDispositionValue == null) {
- contentDispositionValue = "filename=\"" + stream.getFileName()
- + "\"";
- response.setProperty("Content-Disposition",
- contentDispositionValue);
- }
+ // Copy download stream parameters directly
+ // to HTTP headers.
+ final Iterator<String> i = stream.getParameterNames();
+ if (i != null) {
+ while (i.hasNext()) {
+ final String param = i.next();
+ response.setProperty(param, stream.getParameter(param));
+ }
+ }
- int bufferSize = stream.getBufferSize();
- if (bufferSize <= 0 || bufferSize > MAX_BUFFER_SIZE) {
- bufferSize = DEFAULT_BUFFER_SIZE;
- }
- final byte[] buffer = new byte[bufferSize];
- int bytesRead = 0;
+ // suggest local filename from DownloadStream if
+ // Content-Disposition
+ // not explicitly set
+ String contentDispositionValue = stream
+ .getParameter("Content-Disposition");
+ if (contentDispositionValue == null) {
+ contentDispositionValue = "filename=\""
+ + stream.getFileName() + "\"";
+ response.setProperty("Content-Disposition",
+ contentDispositionValue);
+ }
- final OutputStream out = response.getPortletOutputStream();
+ int bufferSize = stream.getBufferSize();
+ if (bufferSize <= 0 || bufferSize > MAX_BUFFER_SIZE) {
+ bufferSize = DEFAULT_BUFFER_SIZE;
+ }
+ final byte[] buffer = new byte[bufferSize];
+ int bytesRead = 0;
- while ((bytesRead = data.read(buffer)) > 0) {
- out.write(buffer, 0, bytesRead);
- out.flush();
+ out = response.getPortletOutputStream();
+
+ while ((bytesRead = data.read(buffer)) > 0) {
+ out.write(buffer, 0, bytesRead);
+ out.flush();
+ }
+ out.close();
+ } finally {
+ AbstractCommunicationManager.tryToCloseStream(data);
+ AbstractCommunicationManager.tryToCloseStream(out);
}
- out.close();
}
}
final InputStream data = stream.getStream();
if (data != null) {
- // Sets content type
- response.setContentType(stream.getContentType());
-
- // Sets cache headers
- final long cacheTime = stream.getCacheTime();
- if (cacheTime <= 0) {
- response.setHeader("Cache-Control", "no-cache");
- response.setHeader("Pragma", "no-cache");
- response.setDateHeader("Expires", 0);
- } else {
- response.setHeader("Cache-Control", "max-age=" + cacheTime
- / 1000);
- response.setDateHeader("Expires", System.currentTimeMillis()
- + cacheTime);
- response.setHeader("Pragma", "cache"); // Required to apply
- // caching in some
- // Tomcats
- }
+ OutputStream out = null;
+ try {
+ // Sets content type
+ response.setContentType(stream.getContentType());
+
+ // Sets cache headers
+ final long cacheTime = stream.getCacheTime();
+ if (cacheTime <= 0) {
+ response.setHeader("Cache-Control", "no-cache");
+ response.setHeader("Pragma", "no-cache");
+ response.setDateHeader("Expires", 0);
+ } else {
+ response.setHeader("Cache-Control", "max-age=" + cacheTime
+ / 1000);
+ response.setDateHeader("Expires",
+ System.currentTimeMillis() + cacheTime);
+ response.setHeader("Pragma", "cache"); // Required to apply
+ // caching in some
+ // Tomcats
+ }
- // Copy download stream parameters directly
- // to HTTP headers.
- final Iterator<String> i = stream.getParameterNames();
- if (i != null) {
- while (i.hasNext()) {
- final String param = i.next();
- response.setHeader(param, stream.getParameter(param));
+ // Copy download stream parameters directly
+ // to HTTP headers.
+ final Iterator<String> i = stream.getParameterNames();
+ if (i != null) {
+ while (i.hasNext()) {
+ final String param = i.next();
+ response.setHeader(param, stream.getParameter(param));
+ }
}
- }
- // suggest local filename from DownloadStream if Content-Disposition
- // not explicitly set
- String contentDispositionValue = stream
- .getParameter("Content-Disposition");
- if (contentDispositionValue == null) {
- contentDispositionValue = "filename=\"" + stream.getFileName()
- + "\"";
- response.setHeader("Content-Disposition",
- contentDispositionValue);
- }
+ // suggest local filename from DownloadStream if
+ // Content-Disposition
+ // not explicitly set
+ String contentDispositionValue = stream
+ .getParameter("Content-Disposition");
+ if (contentDispositionValue == null) {
+ contentDispositionValue = "filename=\""
+ + stream.getFileName() + "\"";
+ response.setHeader("Content-Disposition",
+ contentDispositionValue);
+ }
- int bufferSize = stream.getBufferSize();
- if (bufferSize <= 0 || bufferSize > MAX_BUFFER_SIZE) {
- bufferSize = DEFAULT_BUFFER_SIZE;
- }
- final byte[] buffer = new byte[bufferSize];
- int bytesRead = 0;
+ int bufferSize = stream.getBufferSize();
+ if (bufferSize <= 0 || bufferSize > MAX_BUFFER_SIZE) {
+ bufferSize = DEFAULT_BUFFER_SIZE;
+ }
+ final byte[] buffer = new byte[bufferSize];
+ int bytesRead = 0;
- final OutputStream out = response.getOutputStream();
+ out = response.getOutputStream();
- while ((bytesRead = data.read(buffer)) > 0) {
- out.write(buffer, 0, bytesRead);
- out.flush();
+ while ((bytesRead = data.read(buffer)) > 0) {
+ out.write(buffer, 0, bytesRead);
+ out.flush();
+ }
+ out.close();
+ data.close();
+ } finally {
+ AbstractCommunicationManager.tryToCloseStream(out);
+ AbstractCommunicationManager.tryToCloseStream(data);
}
- out.close();
- data.close();
}
}
return startedEvent.isDisposed();
}
- private void tryToCloseStream(OutputStream out) {
+ static void tryToCloseStream(OutputStream out) {
try {
// try to close output stream (e.g. file handle)
if (out != null) {
}
}
+ static void tryToCloseStream(InputStream in) {
+ try {
+ // try to close output stream (e.g. file handle)
+ if (in != null) {
+ in.close();
+ }
+ } catch (IOException e1) {
+ // NOP
+ }
+ }
+
/**
* Removes any possible path information from the filename and returns the
* filename. Separators / and \\ are used.