* @param response
* the weblounge response
*/
public boolean service(WebloungeRequest request, WebloungeResponse response) {
Site site = request.getSite();
WebUrl url = request.getUrl();
String path = request.getRequestURI();
String feedType = null;
String feedVersion = null;
// Currently, we only support feeds mapped to our well-known uri
if (!path.startsWith(URI_PREFIX) || !(path.length() > URI_PREFIX.length()))
return false;
// Check for feed type and version
String feedURI = path.substring(URI_PREFIX.length());
String[] feedURIParts = feedURI.split("/");
if (feedURIParts.length == 0) {
logger.debug("Feed request {} does not include feed type", path);
return false;
} else if (feedURIParts.length == 1) {
logger.debug("Feed request {} does not include feed version", path);
return false;
}
// Check the request method. This handler only supports GET
String requestMethod = request.getMethod().toUpperCase();
if ("OPTIONS".equals(requestMethod)) {
String verbs = "OPTIONS,GET";
logger.trace("Answering options request to {} with {}", url, verbs);
response.setHeader("Allow", verbs);
response.setContentLength(0);
return true;
} else if (!"GET".equals(requestMethod)) {
logger.debug("Feed request handler does not support {} requests", url, requestMethod);
DispatchUtils.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, request, response);
return true;
}
feedType = feedURIParts[0];
feedVersion = feedURIParts[1];
// Check for explicit no cache instructions
boolean noCache = request.getParameter(ResponseCache.NOCACHE_PARAM) != null;
// Check if the page is already part of the cache. If so, our task is
// already done!
if (!noCache) {
long expirationTime = Renderer.DEFAULT_VALID_TIME;
long revalidationTime = Renderer.DEFAULT_RECHECK_TIME;
// Create the set of tags that identify the request output
CacheTagSet cacheTags = createPrimaryCacheTags(request);
// Check if the page is already part of the cache
if (response.startResponse(cacheTags.getTags(), expirationTime, revalidationTime)) {
logger.debug("Feed handler answered request for {} from cache", request.getUrl());
return true;
}
}
try {
// Compile the feed
SyndFeed feed = createFeed(feedType, feedVersion, site, request, response);
if (feed == null)
return true;
// Set the response type
String characterEncoding = "utf-8";
if (feedType.startsWith("atom"))
response.setContentType("application/atom+xml; charset=" + characterEncoding);
else if (feedType.startsWith("rss"))
response.setContentType("application/rss+xml; charset=" + characterEncoding);
// Set the character encoding
feed.setEncoding(response.getCharacterEncoding());
// Set the modification date
response.setModificationDate(feed.getPublishedDate());
// Write the feed back to the response
SyndFeedOutput output = new SyndFeedOutput();
Writer responseWriter = new OutputStreamWriter(response.getOutputStream(), characterEncoding);
output.output(feed, responseWriter);
response.getOutputStream().flush();
return true;
} catch (ContentRepositoryException e) {
logger.error("Error loading articles for feeds from {}: {}", site.getIdentifier(), e.getMessage());
DispatchUtils.sendInternalError(request, response);
return true;
} catch (FeedException e) {
logger.error("Error creating {} feed: {}", feedType, e.getMessage());
DispatchUtils.sendInternalError(request, response);