use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project solo by b3log.
the class AdminConsole method showAdminFunctions.
/**
* Shows administrator functions with the specified context.
*
* @param request the specified request
* @param context the specified context
*/
@RequestProcessing(value = { "/admin-article.do", "/admin-article-list.do", "/admin-comment-list.do", "/admin-link-list.do", "/admin-page-list.do", "/admin-others.do", "/admin-draft-list.do", "/admin-user-list.do", "/admin-plugin-list.do", "/admin-main.do", "/admin-about.do" }, method = HTTPRequestMethod.GET)
public void showAdminFunctions(final HttpServletRequest request, final HTTPRequestContext context) {
final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
context.setRenderer(renderer);
final String requestURI = request.getRequestURI();
final String templateName = StringUtils.substringBetween(requestURI, Latkes.getContextPath() + '/', ".") + ".ftl";
LOGGER.log(Level.TRACE, "Admin function[templateName={0}]", templateName);
renderer.setTemplateName(templateName);
final Locale locale = Latkes.getLocale();
final Map<String, String> langs = langPropsService.getAll(locale);
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.put("isMySQL", RuntimeDatabase.MYSQL == Latkes.getRuntimeDatabase());
dataModel.putAll(langs);
Keys.fillRuntime(dataModel);
dataModel.put(Option.ID_C_LOCALE_STRING, locale.toString());
fireFreeMarkerActionEvent(templateName, dataModel);
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project solo by b3log.
the class ArticleProcessor method showArticlePwdForm.
/**
* Shows the article view password form.
*
* @param context the specified context
* @param request the specified HTTP servlet request
* @param response the specified HTTP servlet response
* @throws Exception exception
*/
@RequestProcessing(value = "/console/article-pwd", method = HTTPRequestMethod.GET)
public void showArticlePwdForm(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final String articleId = request.getParameter("articleId");
if (Strings.isEmptyOrNull(articleId)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONObject article = articleQueryService.getArticleById(articleId);
if (null == article) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("article-pwd.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.put("articleId", articleId);
dataModel.put("articlePermalink", article.optString(Article.ARTICLE_PERMALINK));
dataModel.put("articleTitle", article.optString(Article.ARTICLE_TITLE));
dataModel.put("articleAbstract", article.optString(Article.ARTICLE_ABSTRACT));
final String msg = request.getParameter(Keys.MSG);
if (!Strings.isEmptyOrNull(msg)) {
dataModel.put(Keys.MSG, langPropsService.get("passwordNotMatchLabel"));
}
final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
dataModel.putAll(langs);
final JSONObject preference = preferenceQueryService.getPreference();
dataModel.put(Option.ID_C_BLOG_TITLE, preference.getString(Option.ID_C_BLOG_TITLE));
dataModel.put(Common.VERSION, SoloServletListener.VERSION);
dataModel.put(Common.STATIC_RESOURCE_VERSION, Latkes.getStaticResourceVersion());
dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
Keys.fillRuntime(dataModel);
filler.fillMinified(dataModel);
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project solo by b3log.
the class ArticleProcessor method showArticle.
/**
* Shows an article with the specified context.
*
* @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 = "/article", method = HTTPRequestMethod.GET)
public void showArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws IOException {
// See PermalinkFilter#dispatchToArticleOrPageProcessor()
final JSONObject article = (JSONObject) request.getAttribute(Article.ARTICLE);
if (null == article) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final String articleId = article.optString(Keys.OBJECT_ID);
LOGGER.log(Level.DEBUG, "Article[id={0}]", articleId);
final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("article.ftl");
try {
final JSONObject preference = preferenceQueryService.getPreference();
final boolean allowVisitDraftViaPermalink = preference.getBoolean(Option.ID_C_ALLOW_VISIT_DRAFT_VIA_PERMALINK);
if (!article.optBoolean(Article.ARTICLE_IS_PUBLISHED) && !allowVisitDraftViaPermalink) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
LOGGER.log(Level.TRACE, "Article[title={0}]", article.getString(Article.ARTICLE_TITLE));
articleQueryService.markdown(article);
// For <meta name="description" content="${article.articleAbstract}"/>
final String metaDescription = Jsoup.parse(article.optString(Article.ARTICLE_ABSTRACT)).text();
article.put(Article.ARTICLE_ABSTRACT, metaDescription);
if (preference.getBoolean(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT)) {
article.put(Common.HAS_UPDATED, articleQueryService.hasUpdated(article));
} else {
article.put(Common.HAS_UPDATED, false);
}
final JSONObject author = articleQueryService.getAuthor(article);
final String authorName = author.getString(User.USER_NAME);
article.put(Common.AUTHOR_NAME, authorName);
final String authorId = author.getString(Keys.OBJECT_ID);
article.put(Common.AUTHOR_ID, authorId);
article.put(Common.AUTHOR_ROLE, author.getString(User.USER_ROLE));
final String userAvatar = author.optString(UserExt.USER_AVATAR);
if (!Strings.isEmptyOrNull(userAvatar)) {
article.put(Common.AUTHOR_THUMBNAIL_URL, userAvatar);
} else {
final String thumbnailURL = Thumbnails.getGravatarURL(author.optString(User.USER_EMAIL), "128");
article.put(Common.AUTHOR_THUMBNAIL_URL, thumbnailURL);
}
final Map<String, Object> dataModel = renderer.getDataModel();
prepareShowArticle(preference, dataModel, article);
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);
if (!Requests.hasBeenServed(request, response)) {
articleMgmtService.incViewCount(articleId);
}
statisticMgmtService.incBlogViewCount(request, response);
// Fire [Before Render Article] event
final JSONObject eventData = new JSONObject();
eventData.put(Article.ARTICLE, article);
try {
eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.BEFORE_RENDER_ARTICLE, eventData));
} catch (final EventException e) {
LOGGER.log(Level.ERROR, "Fires [" + EventTypes.BEFORE_RENDER_ARTICLE + "] event failed", e);
}
} 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.AbstractFreeMarkerRenderer in project solo by b3log.
the class IndexProcessor method showKillBrowser.
/**
* Shows kill browser page with the specified context.
*
* @param context the specified context
* @param request the specified HTTP servlet request
* @param response the specified HTTP servlet response
*/
@RequestProcessing(value = "/kill-browser", method = HTTPRequestMethod.GET)
public void showKillBrowser(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) {
final AbstractFreeMarkerRenderer renderer = new KillBrowserRenderer();
context.setRenderer(renderer);
final Map<String, Object> dataModel = renderer.getDataModel();
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.fillBlogFooter(request, dataModel, preference);
Keys.fillServer(dataModel);
Keys.fillRuntime(dataModel);
filler.fillMinified(dataModel);
} 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.AbstractFreeMarkerRenderer in project solo by b3log.
the class IndexProcessor method showIndex.
/**
* Shows index with the specified context.
*
* @param context the specified context
* @param request the specified HTTP servlet request
* @param response the specified HTTP servlet response
*/
@RequestProcessing(value = { "/\\d*", "" }, uriPatternsMode = URIPatternMode.REGEX, method = HTTPRequestMethod.GET)
public void showIndex(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) {
final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("index.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
final String requestURI = request.getRequestURI();
try {
final int currentPageNum = getCurrentPageNum(requestURI);
final JSONObject preference = preferenceQueryService.getPreference();
// https://github.com/b3log/solo/issues/12060
String specifiedSkin = Skins.getSkinDirName(request);
if (null != specifiedSkin) {
if ("default".equals(specifiedSkin)) {
specifiedSkin = preference.optString(Option.ID_C_SKIN_DIR_NAME);
}
} else {
specifiedSkin = preference.optString(Option.ID_C_SKIN_DIR_NAME);
}
Templates.MAIN_CFG.setServletContextForTemplateLoading(SoloServletListener.getServletContext(), "/skins/" + specifiedSkin);
request.setAttribute(Keys.TEMAPLTE_DIR_NAME, specifiedSkin);
Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
filler.fillIndexArticles(request, dataModel, currentPageNum, preference);
filler.fillSide(request, dataModel, preference);
filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference);
dataModel.put(Pagination.PAGINATION_CURRENT_PAGE_NUM, currentPageNum);
final int previousPageNum = currentPageNum > 1 ? currentPageNum - 1 : 0;
dataModel.put(Pagination.PAGINATION_PREVIOUS_PAGE_NUM, previousPageNum);
final Integer pageCount = (Integer) dataModel.get(Pagination.PAGINATION_PAGE_COUNT);
final int nextPageNum = currentPageNum + 1 > pageCount ? pageCount : currentPageNum + 1;
dataModel.put(Pagination.PAGINATION_NEXT_PAGE_NUM, nextPageNum);
dataModel.put(Common.PATH, "");
statisticMgmtService.incBlogViewCount(request, response);
// https://github.com/b3log/solo/issues/12060
final Cookie cookie = new Cookie(Skin.SKIN, specifiedSkin);
cookie.setPath("/");
response.addCookie(cookie);
} 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());
}
}
}
Aggregations