return;
}
}
WebsiteData weblog = null;
boolean isSiteWide = false;
WeblogPageRequest pageRequest = null;
try {
pageRequest = new WeblogPageRequest(request);
weblog = pageRequest.getWeblog();
if(weblog == null) {
throw new RollerException("unable to lookup weblog: "+
pageRequest.getWeblogHandle());
}
// is this the site-wide weblog?
isSiteWide = RollerRuntimeConfig.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)) {
return;
} else {
// set last-modified date
ModDateHeaderUtil.setLastModifiedHeader(response,lastModified);
}
}
// generate cache key
String cacheKey = null;
if(isSiteWide) {
cacheKey = siteWideCache.generateKey(pageRequest);
} else {
cacheKey = weblogPageCache.generateKey(pageRequest);
}
// cached content checking
if((!this.excludeOwnerPages || !pageRequest.isLoggedIn()) &&
request.getAttribute("skipCache") == null) {
CachedContent cachedContent = null;
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) {
this.processHit(weblog, request.getRequestURL().toString(), request.getHeader("referer"));
}
response.setContentLength(cachedContent.getContent().length);
response.setContentType(cachedContent.getContentType());
response.getOutputStream().write(cachedContent.getContent());
return;
} else {
log.debug("MISS "+cacheKey);
}
}
// figure out what we are going to render
Template 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.getPageByName("_popupcomments");
} catch(Exception e ) {
// ignored ... considered page not found
}
// User doesn't have one so return the default
if(page == null) {
page = new WeblogTemplate("templates/weblog/popupcomments.vm", weblog,
"Comments", "Comments", "dummy_link",
"dummy_template", new Date(), "velocity", true, false, null);
}
// If request specified the page, then go with that
} else if (pageRequest.getWeblogPageName() != null) {
page = pageRequest.getWeblogPage();
// If page not available from request, then use weblog's default
} else {
try {
page = weblog.getDefaultPage();
} catch(Exception e) {
log.error("Error getting weblogs default page", 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;
}
if(pageRequest.getLocale() != null) {
// locale view only allowed if weblog has enabled it
if(!pageRequest.getWeblog().isEnableMultiLang()) {
invalid = true;
}
}
if(pageRequest.getWeblogAnchor() != null) {
// permalink specified.
// entry must exist, be published before current time, and locale must match
WeblogEntryData 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().size() > 0) {
try {
// tags specified. make sure they exist.
WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
invalid = !wmgr.getTagComboExists(pageRequest.getTags(), (isSiteWide) ? null : weblog);
} catch (RollerException ex) {
invalid = true;
}
}
if(invalid) {
if(!response.isCommitted()) response.reset();
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// allow for hit counting
if(!isSiteWide) {
this.processHit(weblog, request.getRequestURL().toString(), request.getHeader("referer"));
}
// looks like we need to render content
// set the content type
String mimeType = RollerContext.getServletContext().getMimeType(page.getLink());
String contentType = "text/html; charset=utf-8";
if(mimeType != null) {
// we found a match ... set the content type
contentType = mimeType+"; charset=utf-8";
}
HashMap model = new HashMap();
try {
PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(
this, request, response,"", false, 8192, true);
// special hack for menu tag
request.setAttribute("pageRequest", pageRequest);
// populate the rendering model
Map initData = new HashMap();
initData.put("request", request);
initData.put("requestParameters", request.getParameterMap());
initData.put("weblogRequest", pageRequest);
initData.put("pageContext", pageContext);
// 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 = RollerConfig.getProperty("rendering.pageModels");
ModelLoader.loadModels(pageModels, model, initData, true);
// Load special models for site-wide blog
if(RollerRuntimeConfig.isSiteWideWeblog(weblog.getHandle())) {
String siteModels = RollerConfig.getProperty("rendering.siteModels");
ModelLoader.loadModels(siteModels, model, initData, true);
}
// Load weblog custom models