Weblog weblog = null;
String context = request.getContextPath();
String servlet = request.getServletPath();
String reqURI = request.getRequestURI();
WeblogPreviewResourceRequest resourceRequest = null;
try {
// parse the incoming request and extract the relevant data
resourceRequest = new WeblogPreviewResourceRequest(request);
weblog = resourceRequest.getWeblog();
if(weblog == null) {
throw new WebloggerException("unable to lookup weblog: "+
resourceRequest.getWeblogHandle());
}
} catch(Exception e) {
// invalid resource request or weblog doesn't exist
log.debug("error creating weblog resource request", e);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
log.debug("Resource requested ["+resourceRequest.getResourcePath()+"]");
long resourceLastMod = 0;
InputStream resourceStream = null;
// first, see if we have a preview theme to operate from
if(!StringUtils.isEmpty(resourceRequest.getThemeName())) {
Theme theme = resourceRequest.getTheme();
ThemeResource resource = theme.getResource(resourceRequest.getResourcePath());
if(resource != null) {
resourceLastMod = resource.getLastModified();
resourceStream = resource.getInputStream();
}
}
// second, see if resource comes from weblog's configured shared theme
if(resourceStream == null) {
try {
WeblogTheme weblogTheme = weblog.getTheme();
if(weblogTheme != null) {
ThemeResource resource = weblogTheme.getResource(resourceRequest.getResourcePath());
if(resource != null) {
resourceLastMod = resource.getLastModified();
resourceStream = resource.getInputStream();
}
}
} catch (Exception ex) {
// hmmm, some kind of error getting theme. that's an error.
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
}
// if not from theme then see if resource is in weblog's upload dir
if(resourceStream == null) {
try {
FileManager fileMgr = WebloggerFactory.getWeblogger().getFileManager();
ThemeResource resource = fileMgr.getFile(weblog,
resourceRequest.getResourcePath());
resourceLastMod = resource.getLastModified();
resourceStream = resource.getInputStream();
} catch (Exception ex) {
// still not found? then we don't have it, 404.
log.debug("Unable to get resource", ex);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
}
// Respond with 304 Not Modified if it is not modified.
if (ModDateHeaderUtil.respondIfNotModified(request, response, resourceLastMod)) {
return;
} else {
// set last-modified date
ModDateHeaderUtil.setLastModifiedHeader(response, resourceLastMod);
}
// set the content type based on whatever is in our web.xml mime defs
response.setContentType(this.context.getMimeType(resourceRequest.getResourcePath()));
OutputStream out = null;
try {
// ok, lets serve up the file
byte[] buf = new byte[8192];