@Override
public void respond(IRequestCycle requestCycle) {
boolean save = (this.save != null) ? this.save : Strings.isTrue(RequestCycle.get().getRequest().getRequestParameters()
.getParameterValue(SAVE_PARAMETER).toString());
BrixFileNode node = (BrixFileNode) this.node.getObject();
if (!SitePlugin.get().canViewNode(node, Action.Context.PRESENTATION)) {
throw Brix.get().getForbiddenException();
}
WebResponse response = (WebResponse) RequestCycle.get().getResponse();
response.setContentType(node.getMimeType());
Date lastModified = node.getLastModified();
response.setLastModifiedTime(Time.valueOf(lastModified));
try {
final HttpServletRequest r = (HttpServletRequest) requestCycle.getRequest().getContainerRequest();
String since = r.getHeader("If-Modified-Since");
if (!save && since != null) {
Date d = new Date(r.getDateHeader("If-Modified-Since"));
// the weird toString comparison is to prevent comparing
// milliseconds
if (d.after(lastModified) || d.toString().equals(lastModified.toString())) {
response.setContentLength(node.getContentLength());
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
}
String fileName = node.getName();
long length = node.getContentLength();
HttpServletResponse httpServletResponse = (HttpServletResponse) response.getContainerResponse();
httpServletResponse.setContentType(node.getMimeType());
InputStream stream = node.getDataAsStream();
new Streamer(length, stream, fileName, save, r, httpServletResponse).stream();
} catch (Exception e) {
log.error("Error writing resource data to content", e);
}