public static void fileRequested(IoBufferAllocatorEx<?> allocator, HttpRequestMessage httpRequest,
HttpResponseMessage httpResponse, File requestFile) throws IOException {
if (requestFile.isFile() && requestFile.exists()) {
String etag = getETagHeaderValue(requestFile);
String ifNoneMatch = httpRequest.getHeader("If-None-Match");
String ifModifiedSince = httpRequest.getHeader("If-Modified-Since");
if (!hasBeenModified(requestFile, etag, ifNoneMatch, ifModifiedSince)) {
httpResponse.setHeader("ETag", etag);
httpResponse.setStatus(HttpStatus.REDIRECT_NOT_MODIFIED);
} else {
FileInputStream in = new FileInputStream(requestFile);
byte[] buf = new byte[8192];
IoBufferEx out = allocator.wrap(allocator.allocate(in.available())).setAutoExpander(allocator);
int length;
while ((length = in.read(buf)) > 0) {
out.put(buf, 0, length);
}
out.flip();
in.close();
httpResponse.setHeader("ETag", etag);
httpResponse.setHeader("Last-Modified", RFC822_FORMAT_PATTERN.format(requestFile.lastModified()));
httpResponse.setHeader("Expires", RFC822_FORMAT_PATTERN.format(System.currentTimeMillis()));
httpResponse.setContent(new HttpContentMessage(out, true));
}
} else {
httpResponse.setStatus(HttpStatus.CLIENT_NOT_FOUND);
}
}