final ResourceResponse resourceResponse = new ResourceResponse();
if (resourceResponse.dataNeedsToBeWritten(attributes))
{
// get resource stream
final IResourceStream resourceStream = getResourceStream();
// bail out if resource stream could not be found
if (resourceStream == null)
return sendResourceError(resourceResponse, HttpServletResponse.SC_NOT_FOUND,
"Unable to find resource");
// set Content-Type (may be null)
resourceResponse.setContentType(resourceStream.getContentType());
// add Last-Modified header (to support HEAD requests and If-Modified-Since)
final Time lastModified = resourceStream.lastModifiedTime();
if (lastModified != null)
resourceResponse.setLastModified(lastModified.toDate());
try
{
// read resource data
final byte[] bytes;
try
{
bytes = IOUtils.toByteArray(resourceStream.getInputStream());
}
finally
{
resourceStream.close();
}
// send Content-Length header
resourceResponse.setContentLength(bytes.length);