use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class PageProcessor method showPage.
/**
* Shows page with the specified context.
*
* @param context the specified context
*/
@RequestProcessing(value = "/page", method = HTTPRequestMethod.GET)
public void showPage(final HTTPRequestContext context) {
final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("page.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
final HttpServletRequest request = context.getRequest();
final HttpServletResponse response = context.getResponse();
try {
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
Skins.fillLangs(preference.getString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
// See PermalinkFilter#dispatchToArticleOrPageProcessor()
final JSONObject page = (JSONObject) request.getAttribute(Page.PAGE);
if (null == page) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final String pageId = page.getString(Keys.OBJECT_ID);
page.put(Common.COMMENTABLE, preference.getBoolean(Option.ID_C_COMMENTABLE) && page.getBoolean(Page.PAGE_COMMENTABLE));
page.put(Common.PERMALINK, page.getString(Page.PAGE_PERMALINK));
dataModel.put(Page.PAGE, page);
final List<JSONObject> comments = commentQueryService.getComments(pageId);
dataModel.put(Page.PAGE_COMMENTS_REF, comments);
// Markdown
if ("CodeMirror-Markdown".equals(page.optString(Page.PAGE_EDITOR_TYPE))) {
Stopwatchs.start("Markdown Page[id=" + page.optString(Keys.OBJECT_ID) + "]");
String content = page.optString(Page.PAGE_CONTENT);
content = Emotions.convert(content);
content = Markdowns.toHTML(content);
page.put(Page.PAGE_CONTENT, content);
Stopwatchs.end();
}
filler.fillSide(request, dataModel, preference);
filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference);
statisticMgmtService.incBlogViewCount(request, response);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
}
}
use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class RepairProcessor method removeUnusedArticleProperties.
/**
* Removes unused properties of each article.
*
* @param context the specified context
*/
@RequestProcessing(value = "/fix/normalization/articles/properties", method = HTTPRequestMethod.POST)
public void removeUnusedArticleProperties(final HTTPRequestContext context) {
LOGGER.log(Level.INFO, "Processes remove unused article properties");
final TextHTMLRenderer renderer = new TextHTMLRenderer();
context.setRenderer(renderer);
Transaction transaction = null;
try {
final JSONArray articles = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS);
if (articles.length() <= 0) {
renderer.setContent("No unused article properties");
return;
}
transaction = articleRepository.beginTransaction();
final Set<String> keyNames = Repositories.getKeyNames(Article.ARTICLE);
for (int i = 0; i < articles.length(); i++) {
final JSONObject article = articles.getJSONObject(i);
final JSONArray names = article.names();
final Set<String> nameSet = CollectionUtils.<String>jsonArrayToSet(names);
if (nameSet.removeAll(keyNames)) {
for (final String unusedName : nameSet) {
article.remove(unusedName);
}
articleRepository.update(article.getString(Keys.OBJECT_ID), article);
LOGGER.log(Level.INFO, "Found an article[id={0}] exists unused properties[{1}]", new Object[] { article.getString(Keys.OBJECT_ID), nameSet });
}
}
transaction.commit();
} catch (final Exception e) {
if (null != transaction && transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, e.getMessage(), e);
renderer.setContent("Removes unused article properties failed, error msg[" + e.getMessage() + "]");
}
}
use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class RepairProcessor method removeAllDataPOST.
/**
* Removes all data.
*
* @param context the specified context
*/
@RequestProcessing(value = "/rm-all-data.do", method = HTTPRequestMethod.POST)
public void removeAllDataPOST(final HTTPRequestContext context) {
LOGGER.info("Removing all data....");
boolean succeed = false;
try {
remove(beanManager.getReference(ArchiveDateArticleRepositoryImpl.class));
remove(beanManager.getReference(ArchiveDateRepositoryImpl.class));
remove(beanManager.getReference(ArticleRepositoryImpl.class));
remove(beanManager.getReference(CommentRepositoryImpl.class));
remove(beanManager.getReference(LinkRepositoryImpl.class));
remove(beanManager.getReference(OptionRepositoryImpl.class));
remove(beanManager.getReference(PageRepositoryImpl.class));
remove(beanManager.getReference(PluginRepositoryImpl.class));
remove(beanManager.getReference(StatisticRepositoryImpl.class));
remove(beanManager.getReference(TagArticleRepositoryImpl.class));
remove(beanManager.getReference(TagRepositoryImpl.class));
remove(beanManager.getReference(UserRepositoryImpl.class));
succeed = true;
} catch (final Exception e) {
LOGGER.log(Level.WARN, "Removed partial data only", e);
}
final StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.append("<html><head><title>Result</title></head><body>");
try {
final TextHTMLRenderer renderer = new TextHTMLRenderer();
context.setRenderer(renderer);
if (succeed) {
htmlBuilder.append("Removed all data!");
} else {
htmlBuilder.append("Refresh this page and run this remover again.");
}
htmlBuilder.append("</body></html>");
renderer.setContent(htmlBuilder.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
LOGGER.info("Removed all data....");
}
use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class RepairProcessor method repairTagArticleCounter.
/**
* Repairs tag article counter.
*
* @param context the specified context
*/
@RequestProcessing(value = "/fix/tag-article-counter-repair.do", method = HTTPRequestMethod.GET)
@Transactional
public void repairTagArticleCounter(final HTTPRequestContext context) {
final TextHTMLRenderer renderer = new TextHTMLRenderer();
context.setRenderer(renderer);
try {
final JSONObject result = tagRepository.get(new Query());
final JSONArray tagArray = result.getJSONArray(Keys.RESULTS);
final List<JSONObject> tags = CollectionUtils.jsonArrayToList(tagArray);
for (final JSONObject tag : tags) {
final String tagId = tag.getString(Keys.OBJECT_ID);
final JSONObject tagArticleResult = tagArticleRepository.getByTagId(tagId, 1, Integer.MAX_VALUE);
final JSONArray tagArticles = tagArticleResult.getJSONArray(Keys.RESULTS);
final int tagRefCnt = tagArticles.length();
int publishedTagRefCnt = 0;
for (int i = 0; i < tagRefCnt; i++) {
final JSONObject tagArticle = tagArticles.getJSONObject(i);
final String articleId = tagArticle.getString(Article.ARTICLE + "_" + Keys.OBJECT_ID);
final JSONObject article = articleRepository.get(articleId);
if (null == article) {
tagArticleRepository.remove(tagArticle.optString(Keys.OBJECT_ID));
continue;
}
if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
publishedTagRefCnt++;
}
}
tag.put(Tag.TAG_REFERENCE_COUNT, tagRefCnt);
tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedTagRefCnt);
tagRepository.update(tagId, tag);
LOGGER.log(Level.INFO, "Repaired tag[title={0}, refCnt={1}, publishedTagRefCnt={2}]", new Object[] { tag.getString(Tag.TAG_TITLE), tagRefCnt, publishedTagRefCnt });
}
renderer.setContent("Repair sucessfully!");
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
renderer.setContent("Repairs failed, error msg[" + e.getMessage() + "]");
}
}
use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class SitemapProcessor method sitemap.
/**
* Returns the sitemap.
*
* @param context the specified context
*/
@RequestProcessing(value = "/sitemap.xml", method = HTTPRequestMethod.GET)
public void sitemap(final HTTPRequestContext context) {
final TextXMLRenderer renderer = new TextXMLRenderer();
context.setRenderer(renderer);
final Sitemap sitemap = new Sitemap();
try {
addArticles(sitemap);
addNavigations(sitemap);
addTags(sitemap);
addArchives(sitemap);
LOGGER.log(Level.INFO, "Generating sitemap....");
final String content = sitemap.toString();
LOGGER.log(Level.INFO, "Generated sitemap");
renderer.setContent(content);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get blog article feed error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
}
Aggregations