}
Resource resource = request.getResource();
// check the last modification time and If-Modified-Since header
ResourceMetadata meta = resource.getResourceMetadata();
long modifTime = meta.getModificationTime();
if (unmodified(request, modifTime)) {
response.setStatus(SC_NOT_MODIFIED);
return;
}
// fall back to plain text rendering if the resource has no stream
InputStream stream = resource.adaptTo(InputStream.class);
if (stream == null) {
super.doGet(request, response);
return;
}
// finally stream the resource
try {
if (modifTime > 0) {
response.setDateHeader(HEADER_LAST_MODIFIED, modifTime);
}
final String defaultContentType = "application/octet-stream";
String contentType = meta.getContentType();
if (contentType == null || defaultContentType.equals(contentType)) {
// if repository doesn't provide a content-type, or
// provides the
// default one,
// try to do better using our servlet context
final String ct = getServletContext().getMimeType(
resource.getPath());
if (ct != null) {
contentType = ct;
}
}
if (contentType != null) {
response.setContentType(contentType);
}
String encoding = meta.getCharacterEncoding();
if (encoding != null) {
response.setCharacterEncoding(encoding);
}
long length = meta.getContentLength();
if (length > 0 && length < Integer.MAX_VALUE) {
response.setContentLength((int) length);
}
OutputStream out = response.getOutputStream();