use of org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer in project solo by b3log.
the class ArticleProcessor method showAuthorArticles.
/**
* Shows author articles with the specified context.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws IOException io exception
* @throws JSONException json exception
*/
@RequestProcessing(value = "/authors/**", method = HTTPRequestMethod.GET)
public void showAuthorArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws IOException, JSONException {
final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("author-articles.ftl");
try {
String requestURI = request.getRequestURI();
if (!requestURI.endsWith("/")) {
requestURI += "/";
}
final String authorId = getAuthorId(requestURI);
LOGGER.log(Level.DEBUG, "Request author articles[requestURI={0}, authorId={1}]", new Object[] { requestURI, authorId });
final int currentPageNum = getAuthorCurrentPageNum(requestURI, authorId);
if (-1 == currentPageNum) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
LOGGER.log(Level.DEBUG, "Request author articles[authorId={0}, currentPageNum={1}]", new Object[] { authorId, currentPageNum });
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
final int windowSize = preference.getInt(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE);
final JSONObject result = userQueryService.getUser(authorId);
if (null == result) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONObject author = result.getJSONObject(User.USER);
final String authorEmail = author.getString(User.USER_EMAIL);
final List<JSONObject> articles = articleQueryService.getArticlesByAuthorEmail(authorEmail, currentPageNum, pageSize);
if (articles.isEmpty()) {
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
}
filler.setArticlesExProperties(request, articles, author, preference);
if (preference.optBoolean(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT)) {
Collections.sort(articles, Comparators.ARTICLE_UPDATE_DATE_COMPARATOR);
} else {
Collections.sort(articles, Comparators.ARTICLE_CREATE_DATE_COMPARATOR);
}
final int articleCount = author.getInt(UserExt.USER_PUBLISHED_ARTICLE_COUNT);
final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
final Map<String, Object> dataModel = renderer.getDataModel();
prepareShowAuthorArticles(pageNums, dataModel, pageCount, currentPageNum, articles, author);
filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference);
filler.fillSide(request, dataModel, preference);
Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
statisticMgmtService.incBlogViewCount(request, response);
} catch (final ServiceException 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.renderer.freemarker.FreeMarkerRenderer in project solo by b3log.
the class ArticleProcessor method showArchiveArticles.
/**
* Shows archive articles with the specified context.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
*/
@RequestProcessing(value = "/archives/**", method = HTTPRequestMethod.GET)
public void showArchiveArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) {
final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("archive-articles.ftl");
try {
String requestURI = request.getRequestURI();
if (!requestURI.endsWith("/")) {
requestURI += "/";
}
final String archiveDateString = getArchiveDate(requestURI);
final int currentPageNum = getArchiveCurrentPageNum(requestURI);
if (-1 == currentPageNum) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
LOGGER.log(Level.DEBUG, "Request archive date[string={0}, currentPageNum={1}]", new Object[] { archiveDateString, currentPageNum });
final JSONObject result = archiveDateQueryService.getByArchiveDateString(archiveDateString);
if (null == result) {
LOGGER.log(Level.WARN, "Can not find articles for the specified archive date[string={0}]", archiveDateString);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONObject archiveDate = result.getJSONObject(ArchiveDate.ARCHIVE_DATE);
final String archiveDateId = archiveDate.getString(Keys.OBJECT_ID);
final JSONObject preference = preferenceQueryService.getPreference();
final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
final int articleCount = archiveDate.getInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT);
final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
final List<JSONObject> articles = articleQueryService.getArticlesByArchiveDate(archiveDateId, currentPageNum, pageSize);
if (articles.isEmpty()) {
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
}
final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
if (hasMultipleUsers) {
filler.setArticlesExProperties(request, articles, preference);
} else if (!articles.isEmpty()) {
final JSONObject author = articleQueryService.getAuthor(articles.get(0));
filler.setArticlesExProperties(request, articles, author, preference);
}
sort(preference, articles);
final Map<String, Object> dataModel = renderer.getDataModel();
Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
prepareShowArchiveArticles(preference, dataModel, articles, currentPageNum, pageCount, archiveDateString, archiveDate);
filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference);
filler.fillSide(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.renderer.freemarker.FreeMarkerRenderer 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.renderer.freemarker.FreeMarkerRenderer in project solo by b3log.
the class TagProcessor method showTagArticles.
/**
* Shows articles related with a tag with the specified context.
*
* @param context the specified context
* @throws IOException io exception
*/
@RequestProcessing(value = "/tags/**", method = HTTPRequestMethod.GET)
public void showTagArticles(final HTTPRequestContext context) throws IOException {
final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("tag-articles.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
final HttpServletRequest request = context.getRequest();
final HttpServletResponse response = context.getResponse();
try {
String requestURI = request.getRequestURI();
if (!requestURI.endsWith("/")) {
requestURI += "/";
}
String tagTitle = getTagTitle(requestURI);
final int currentPageNum = getCurrentPageNum(requestURI, tagTitle);
if (-1 == currentPageNum) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
LOGGER.log(Level.DEBUG, "Tag[title={0}, currentPageNum={1}]", new Object[] { tagTitle, currentPageNum });
tagTitle = URLDecoder.decode(tagTitle, "UTF-8");
final JSONObject result = tagQueryService.getTagByTitle(tagTitle);
if (null == result) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONObject tag = result.getJSONObject(Tag.TAG);
final String tagId = tag.getString(Keys.OBJECT_ID);
final JSONObject preference = preferenceQueryService.getPreference();
Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
final int windowSize = preference.getInt(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE);
final List<JSONObject> articles = articleQueryService.getArticlesByTag(tagId, currentPageNum, pageSize);
if (articles.isEmpty()) {
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
}
final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
if (hasMultipleUsers) {
filler.setArticlesExProperties(request, articles, preference);
} else {
// All articles composed by the same author
final JSONObject author = articleQueryService.getAuthor(articles.get(0));
filler.setArticlesExProperties(request, articles, author, preference);
}
final int tagArticleCount = tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
final int pageCount = (int) Math.ceil((double) tagArticleCount / (double) pageSize);
LOGGER.log(Level.TRACE, "Paginate tag-articles[currentPageNum={0}, pageSize={1}, pageCount={2}, windowSize={3}]", new Object[] { currentPageNum, pageSize, pageCount, windowSize });
final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
LOGGER.log(Level.TRACE, "tag-articles[pageNums={0}]", pageNums);
Collections.sort(articles, Comparators.ARTICLE_CREATE_DATE_COMPARATOR);
fillPagination(dataModel, pageCount, currentPageNum, articles, pageNums);
dataModel.put(Common.PATH, "/tags/" + URLEncoder.encode(tagTitle, "UTF-8"));
dataModel.put(Keys.OBJECT_ID, tagId);
dataModel.put(Tag.TAG, tag);
filler.fillSide(request, dataModel, preference);
filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference);
statisticMgmtService.incBlogViewCount(request, response);
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
} catch (final JSONException 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.renderer.freemarker.FreeMarkerRenderer in project solo by b3log.
the class UserTemplateProcessor method showPage.
/**
* Shows the user template page.
*
* @param context the specified context
* @param request the specified HTTP servlet request
* @param response the specified HTTP servlet response
* @throws IOException io exception
*/
@RequestProcessing(value = "/*.html", method = HTTPRequestMethod.GET)
public void showPage(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws IOException {
final String requestURI = request.getRequestURI();
String templateName = StringUtils.substringAfterLast(requestURI, "/");
templateName = StringUtils.substringBefore(templateName, ".") + ".ftl";
LOGGER.log(Level.DEBUG, "Shows page[requestURI={0}, templateName={1}]", new Object[] { requestURI, templateName });
final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
context.setRenderer(renderer);
renderer.setTemplateName(templateName);
final Map<String, Object> dataModel = renderer.getDataModel();
final Template template = Templates.getTemplate((String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), templateName);
if (null == template) {
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
}
try {
final Map<String, String> langs = langPropsService.getAll(Locales.getLocale(request));
dataModel.putAll(langs);
final JSONObject preference = preferenceQueryService.getPreference();
filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillUserTemplate(request, template, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference);
Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
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());
}
}
}
Aggregations