* @param context the specified context
* @throws IOException io exception
*/
@RequestProcessing(value = { "/tag-articles-feed.do"}, method = { HTTPRequestMethod.GET, HTTPRequestMethod.HEAD})
public void tagArticlesAtom(final HTTPRequestContext context) throws IOException {
final AtomRenderer renderer = new AtomRenderer();
context.setRenderer(renderer);
final HttpServletRequest request = context.getRequest();
final HttpServletResponse response = context.getResponse();
final String queryString = request.getQueryString();
if (Strings.isEmptyOrNull(queryString)) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
final String oIdMap = queryString.split("&")[0];
final String tagId = oIdMap.split("=")[1];
final Feed feed = new Feed();
try {
final JSONObject tag = tagRepository.get(tagId);
if (null == tag) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final String tagTitle = tag.getString(Tag.TAG_TITLE);
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final String blogTitle = preference.getString(Preference.BLOG_TITLE);
final String blogSubtitle = preference.getString(Preference.BLOG_SUBTITLE) + ", " + tagTitle;
final int outputCnt = preference.getInt(Preference.FEED_OUTPUT_CNT);
feed.setTitle(StringEscapeUtils.escapeXml(blogTitle));
feed.setSubtitle(StringEscapeUtils.escapeXml(blogSubtitle));
feed.setUpdated(new Date());
feed.setAuthor(StringEscapeUtils.escapeXml(blogTitle));
feed.setLink(Latkes.getServePath() + "/tag-articles-feed.do");
feed.setId(Latkes.getServePath() + "/");
final JSONObject tagArticleResult = tagArticleRepository.getByTagId(tagId, 1, outputCnt);
final JSONArray tagArticleRelations = tagArticleResult.getJSONArray(Keys.RESULTS);
if (0 == tagArticleRelations.length()) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final List<JSONObject> articles = new ArrayList<JSONObject>();
for (int i = 0; i < tagArticleRelations.length(); i++) {
final JSONObject tagArticleRelation = tagArticleRelations.getJSONObject(i);
final String articleId = tagArticleRelation.getString(Article.ARTICLE + "_" + Keys.OBJECT_ID);
final JSONObject article = articleRepository.get(articleId);
if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED) // Skips the unpublished article
&& Strings.isEmptyOrNull(article.optString(Article.ARTICLE_VIEW_PWD))) { // Skips article with password
articles.add(article);
}
}
final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
String authorName = "";
if (!hasMultipleUsers && !articles.isEmpty()) {
authorName = articleQueryService.getAuthor(articles.get(0)).getString(User.USER_NAME);
}
final boolean isFullContent = "fullContent".equals(preference.getString(Preference.FEED_OUTPUT_MODE));
for (int i = 0; i < articles.size(); i++) {
final JSONObject article = articles.get(i);
final Entry entry = new Entry();
feed.addEntry(entry);
final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE));
entry.setTitle(title);
final String summary = isFullContent
? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT))
: StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT));
entry.setSummary(summary);
final Date updated = (Date) article.get(Article.ARTICLE_UPDATE_DATE);
entry.setUpdated(updated);
final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK);
entry.setLink(link);
entry.setId(link);
if (hasMultipleUsers) {
authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME));
}
entry.setAuthor(authorName);
final String tagsString = article.getString(Article.ARTICLE_TAGS_REF);
final String[] tagStrings = tagsString.split(",");
for (int j = 0; j < tagStrings.length; j++) {
final Category catetory = new Category();
entry.addCatetory(catetory);
catetory.setTerm(tagStrings[j]);
}
}
renderer.setContent(feed.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get tag article feed error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);