in app/src/main/java/org/apache/roller/weblogger/ui/rendering/servlets/PageServlet.java [136:550]
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
log.debug("Entering");
// do referrer processing, if it's enabled
// NOTE: this *must* be done first because it triggers a hibernate flush
// which will close the active session and cause lazy init exceptions
// otherwise
if (this.processReferrers) {
boolean spam = this.processReferrer(request);
if (spam) {
log.debug("spammer, giving 'em a 403");
if (!response.isCommitted()) {
response.reset();
}
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
}
Weblog weblog;
boolean isSiteWide;
WeblogPageRequest pageRequest;
try {
pageRequest = new WeblogPageRequest(request);
weblog = pageRequest.getWeblog();
if (weblog == null) {
throw new WebloggerException("unable to lookup weblog: "
+ pageRequest.getWeblogHandle());
}
// is this the site-wide weblog?
isSiteWide = WebloggerRuntimeConfig.isSiteWideWeblog(pageRequest
.getWeblogHandle());
} catch (Exception e) {
// some kind of error parsing the request or looking up weblog
log.debug("error creating page request", e);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// determine the lastModified date for this content
long lastModified = System.currentTimeMillis();
if (isSiteWide) {
lastModified = siteWideCache.getLastModified().getTime();
} else if (weblog.getLastModified() != null) {
lastModified = weblog.getLastModified().getTime();
}
// 304 Not Modified handling.
// We skip this for logged in users to avoid the scenerio where a user
// views their weblog, logs in, then gets a 304 without the 'edit' links
if (!pageRequest.isLoggedIn()) {
if (ModDateHeaderUtil.respondIfNotModified(request, response,
lastModified, pageRequest.getDeviceType())) {
return;
} else {
// set last-modified date
ModDateHeaderUtil.setLastModifiedHeader(response, lastModified,
pageRequest.getDeviceType());
}
}
// generate cache key
String cacheKey;
if (isSiteWide) {
cacheKey = siteWideCache.generateKey(pageRequest);
} else {
cacheKey = weblogPageCache.generateKey(pageRequest);
}
// Development only. Reload if theme has been modified
if (themeReload
&& !weblog.getEditorTheme().equals(WeblogTheme.CUSTOM)
&& (pageRequest.getPathInfo() == null || pageRequest
.getPathInfo() != null
&& !pageRequest.getPathInfo().endsWith(".css"))) {
try {
ThemeManager manager = WebloggerFactory.getWeblogger()
.getThemeManager();
boolean reloaded = manager.reLoadThemeFromDisk(weblog
.getEditorTheme());
if (reloaded) {
if (isSiteWide) {
siteWideCache.clear();
} else {
weblogPageCache.clear();
}
I18nMessages.reloadBundle(weblog.getLocaleInstance());
}
} catch (Exception ex) {
log.error("ERROR - reloading theme " + ex);
}
}
// cached content checking
if ((!this.excludeOwnerPages || !pageRequest.isLoggedIn())
&& request.getAttribute("skipCache") == null
&& request.getParameter("skipCache") == null) {
CachedContent cachedContent;
if (isSiteWide) {
cachedContent = (CachedContent) siteWideCache.get(cacheKey);
} else {
cachedContent = (CachedContent) weblogPageCache.get(cacheKey,
lastModified);
}
if (cachedContent != null) {
log.debug("HIT " + cacheKey);
// allow for hit counting
if (!isSiteWide
&& (pageRequest.isWebsitePageHit() || pageRequest
.isOtherPageHit())) {
this.processHit(weblog);
}
response.setContentLength(cachedContent.getContent().length);
response.setContentType(cachedContent.getContentType());
response.getOutputStream().write(cachedContent.getContent());
return;
} else {
log.debug("MISS " + cacheKey);
}
}
log.debug("Looking for template to use for rendering");
// figure out what template to use
ThemeTemplate page = null;
// If this is a popup request, then deal with it specially
// TODO: do we really need to keep supporting this?
if (request.getParameter("popup") != null) {
try {
// Does user have a popupcomments page?
page = weblog.getTheme().getTemplateByName("_popupcomments");
} catch (Exception e) {
// ignored ... considered page not found
}
// User doesn't have one so return the default
if (page == null) {
page = new StaticThemeTemplate(
"templates/weblog/popupcomments.vm", TemplateLanguage.VELOCITY);
}
// If request specified the page, then go with that
} else if ("page".equals(pageRequest.getContext())) {
page = pageRequest.getWeblogPage();
// if we don't have this page then 404, we don't let
// this one fall through to the default template
if (page == null) {
if (!response.isCommitted()) {
response.reset();
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// If request specified tags section index, then look for custom
// template
} else if ("tags".equals(pageRequest.getContext())
&& pageRequest.getTags() != null) {
try {
page = weblog.getTheme().getTemplateByAction(
ComponentType.TAGSINDEX);
} catch (Exception e) {
log.error("Error getting weblog page for action 'tagsIndex'", e);
}
// if we don't have a custom tags page then 404, we don't let
// this one fall through to the default template
if (page == null) {
if (!response.isCommitted()) {
response.reset();
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// If this is a permalink then look for a permalink template
} else if (pageRequest.getWeblogAnchor() != null) {
try {
page = weblog.getTheme().getTemplateByAction(
ComponentType.PERMALINK);
} catch (Exception e) {
log.error("Error getting weblog page for action 'permalink'", e);
}
}
// if we haven't found a page yet then try our default page
if (page == null) {
try {
page = weblog.getTheme().getDefaultTemplate();
} catch (Exception e) {
log.error(
"Error getting default page for weblog = "
+ weblog.getHandle(), e);
}
}
// Still no page? Then that is a 404
if (page == null) {
if (!response.isCommitted()) {
response.reset();
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
log.debug("page found, dealing with it");
// validation. make sure that request input makes sense.
boolean invalid = false;
if (pageRequest.getWeblogPageName() != null && page.isHidden()) {
invalid = true;
}
// locale view allowed only if weblog has enabled it
if (pageRequest.getLocale() != null
&& !pageRequest.getWeblog().isEnableMultiLang()) {
invalid = true;
}
if (pageRequest.getWeblogAnchor() != null) {
// permalink specified.
// entry must exist, be published before current time, and locale
// must match
WeblogEntry entry = pageRequest.getWeblogEntry();
if (entry == null) {
invalid = true;
} else if (pageRequest.getLocale() != null
&& !entry.getLocale().startsWith(pageRequest.getLocale())) {
invalid = true;
} else if (!entry.isPublished()) {
invalid = true;
} else if (new Date().before(entry.getPubTime())) {
invalid = true;
}
} else if (pageRequest.getWeblogCategoryName() != null) {
// category specified. category must exist.
if (pageRequest.getWeblogCategory() == null) {
invalid = true;
}
} else if (pageRequest.getTags() != null && !pageRequest.getTags().isEmpty()) {
try {
// tags specified. make sure they exist.
WeblogEntryManager wmgr = WebloggerFactory.getWeblogger()
.getWeblogEntryManager();
invalid = !wmgr.getTagComboExists(pageRequest.getTags(),
(isSiteWide) ? null : weblog);
} catch (WebloggerException ex) {
invalid = true;
}
}
if (invalid) {
log.debug("page failed validation, bailing out");
if (!response.isCommitted()) {
response.reset();
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// do we need to force a specific locale for the request?
if (pageRequest.getLocale() == null && !weblog.isShowAllLangs()) {
pageRequest.setLocale(weblog.getLocale());
}
// allow for hit counting
if (!isSiteWide
&& (pageRequest.isWebsitePageHit() || pageRequest
.isOtherPageHit())) {
this.processHit(weblog);
}
// looks like we need to render content
// set the content deviceType
String contentType;
if (StringUtils.isNotEmpty(page.getOutputContentType())) {
contentType = page.getOutputContentType() + "; charset=utf-8";
} else {
final String defaultContentType = "text/html; charset=utf-8";
if (page.getLink() == null) {
contentType = defaultContentType;
} else {
String mimeType = RollerContext.getServletContext().getMimeType(
page.getLink());
if (mimeType != null) {
// we found a match ... set the content deviceType
contentType = mimeType + "; charset=utf-8";
} else {
contentType = defaultContentType;
}
}
}
HashMap<String, Object> model = new HashMap<>();
try {
PageContext pageContext = JspFactory.getDefaultFactory()
.getPageContext(this, request, response, "", false,
RollerConstants.EIGHT_KB_IN_BYTES, true);
// special hack for menu tag
request.setAttribute("pageRequest", pageRequest);
// populate the rendering model
Map<String, Object> initData = new HashMap<>();
initData.put("requestParameters", request.getParameterMap());
initData.put("parsedRequest", pageRequest);
initData.put("pageContext", pageContext);
// define url strategy
initData.put("urlStrategy", WebloggerFactory.getWeblogger()
.getUrlStrategy());
// if this was a comment posting, check for comment form
WeblogEntryCommentForm commentForm = (WeblogEntryCommentForm) request
.getAttribute("commentForm");
if (commentForm != null) {
initData.put("commentForm", commentForm);
}
// Load models for pages
String pageModels = WebloggerConfig
.getProperty("rendering.pageModels");
ModelLoader.loadModels(pageModels, model, initData, true);
// Load special models for site-wide blog
if (WebloggerRuntimeConfig.isSiteWideWeblog(weblog.getHandle())) {
String siteModels = WebloggerConfig
.getProperty("rendering.siteModels");
ModelLoader.loadModels(siteModels, model, initData, true);
}
} catch (WebloggerException ex) {
log.error("Error loading model objects for page", ex);
if (!response.isCommitted()) {
response.reset();
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
// lookup Renderer we are going to use
Renderer renderer;
try {
log.debug("Looking up renderer");
renderer = RendererManager.getRenderer(page,
pageRequest.getDeviceType());
} catch (Exception e) {
// nobody wants to render my content :(
log.error("Couldn't find renderer for page " + page.getId(), e);
if (!response.isCommitted()) {
response.reset();
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// render content
CachedContent rendererOutput = new CachedContent(
RollerConstants.TWENTYFOUR_KB_IN_BYTES, contentType);
try {
log.debug("Doing rendering");
renderer.render(model, rendererOutput.getCachedWriter());
// flush rendered output and close
rendererOutput.flush();
rendererOutput.close();
} catch (Exception e) {
// bummer, error during rendering
log.error("Error during rendering for page " + page.getId(), e);
if (!response.isCommitted()) {
response.reset();
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// post rendering process
// flush rendered content to response
log.debug("Flushing response output");
response.setContentType(contentType);
response.setContentLength(rendererOutput.getContent().length);
response.getOutputStream().write(rendererOutput.getContent());
// cache rendered content. only cache if user is not logged in?
if ((!this.excludeOwnerPages || !pageRequest.isLoggedIn())
&& request.getAttribute("skipCache") == null) {
log.debug("PUT " + cacheKey);
// put it in the right cache
if (isSiteWide) {
siteWideCache.put(cacheKey, rendererOutput);
} else {
weblogPageCache.put(cacheKey, rendererOutput);
}
} else {
log.debug("SKIPPED " + cacheKey);
}
log.debug("Exiting");
}