in app/src/main/java/org/apache/roller/weblogger/ui/rendering/servlets/PreviewResourceServlet.java [69:178]
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Weblog weblog;
WeblogPreviewResourceRequest resourceRequest;
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);
if(resourceStream != null) {
resourceStream.close();
}
return;
}
}
// if not from theme then see if resource is in weblog's upload dir
if (resourceStream == null) {
try {
MediaFileManager mmgr = WebloggerFactory.getWeblogger()
.getMediaFileManager();
MediaFile mf = mmgr.getMediaFileByOriginalPath(weblog,
resourceRequest.getResourcePath());
resourceLastMod = mf.getLastModified();
resourceStream = mf.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);
if(resourceStream != null) {
resourceStream.close();
}
return;
}
}
// Respond with 304 Not Modified if it is not modified.
if (ModDateHeaderUtil.respondIfNotModified(request, response,
resourceLastMod, resourceRequest.getDeviceType())) {
return;
} else {
// set last-modified date
ModDateHeaderUtil.setLastModifiedHeader(response, resourceLastMod,
resourceRequest.getDeviceType());
}
// set the content type based on whatever is in our web.xml mime defs
response.setContentType(this.context.getMimeType(resourceRequest
.getResourcePath()));
try {
// ok, lets serve up the file
resourceStream.transferTo(response.getOutputStream());
} catch (IOException ex) {
log.error("Error writing resource file", ex);
if (!response.isCommitted()) {
response.reset();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
}