public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
log.debug("Entering");
Weblog weblog = null;
boolean isSiteWide = false;
WeblogFeedRequest feedRequest = null;
try {
// parse the incoming request and extract the relevant data
feedRequest = new WeblogFeedRequest(request);
weblog = feedRequest.getWeblog();
if(weblog == null) {
throw new WebloggerException("unable to lookup weblog: "+
feedRequest.getWeblogHandle());
}
// is this the site-wide weblog?
isSiteWide = WebloggerRuntimeConfig.isSiteWideWeblog(feedRequest.getWeblogHandle());
} catch(Exception e) {
// invalid feed request format or weblog doesn't exist
log.debug("error creating weblog feed 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();
}
// Respond with 304 Not Modified if it is not modified.
if (ModDateHeaderUtil.respondIfNotModified(request,response,lastModified)) {
return;
}
// set last-modified date
ModDateHeaderUtil.setLastModifiedHeader(response, lastModified);
// set content type
String accepts = request.getHeader("Accept");
String userAgent = request.getHeader("User-Agent");
if (WebloggerRuntimeConfig.getBooleanProperty("site.newsfeeds.styledFeeds") &&
accepts != null && accepts.indexOf("*/*") != -1 &&
userAgent != null && userAgent.startsWith("Mozilla")) {
// client is a browser and feed style is enabled so we want
// browsers to load the page rather than popping up the download
// dialog, so we provide a content-type that browsers will display
response.setContentType("text/xml");
} else if("rss".equals(feedRequest.getFormat())) {
response.setContentType("application/rss+xml; charset=utf-8");
} else if("atom".equals(feedRequest.getFormat())) {
response.setContentType("application/atom+xml; charset=utf-8");
}
// generate cache key
String cacheKey = null;
if(isSiteWide) {
cacheKey = siteWideCache.generateKey(feedRequest);
} else {
cacheKey = weblogFeedCache.generateKey(feedRequest);
}
// cached content checking
CachedContent cachedContent = null;
if(isSiteWide) {
cachedContent = (CachedContent) siteWideCache.get(cacheKey);
} else {
cachedContent = (CachedContent) weblogFeedCache.get(cacheKey, lastModified);
}
if(cachedContent != null) {
log.debug("HIT "+cacheKey);
response.setContentLength(cachedContent.getContent().length);
response.getOutputStream().write(cachedContent.getContent());
return;
} else {
log.debug("MISS "+cacheKey);
}
// validation. make sure that request input makes sense.
boolean invalid = false;
if(feedRequest.getLocale() != null) {
// locale view only allowed if weblog has enabled it
if(!feedRequest.getWeblog().isEnableMultiLang()) {
invalid = true;
}
}
if(feedRequest.getWeblogCategoryName() != null) {
// category specified. category must exist.
if(feedRequest.getWeblogCategory() == null) {
invalid = true;
}
} else if(feedRequest.getTags() != null && feedRequest.getTags().size() > 0) {
try {
// tags specified. make sure they exist.
WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
invalid = !wmgr.getTagComboExists(feedRequest.getTags(), (isSiteWide) ? null : weblog);
} catch (WebloggerException ex) {
invalid = true;
}
}
if(invalid) {
if(!response.isCommitted()) response.reset();
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// do we need to force a specific locale for the request?
if(feedRequest.getLocale() == null && !weblog.isShowAllLangs()) {
feedRequest.setLocale(weblog.getLocale());
}
// looks like we need to render content
HashMap model = new HashMap();
String pageId = null;
try {
// determine what template to render with
if (WebloggerRuntimeConfig.isSiteWideWeblog(weblog.getHandle())) {
pageId = "templates/feeds/site-"+feedRequest.getType()+"-"+feedRequest.getFormat()+".vm";
} else {
pageId = "templates/feeds/weblog-"+feedRequest.getType()+"-"+feedRequest.getFormat()+".vm";
}
// populate the rendering model
Map initData = new HashMap();
initData.put("parsedRequest", feedRequest);
// define url strategy
initData.put("urlStrategy", WebloggerFactory.getWeblogger().getUrlStrategy());
// Load models for feeds
String feedModels = WebloggerConfig.getProperty("rendering.feedModels");
ModelLoader.loadModels(feedModels, 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);
}
// Load weblog custom models