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());
}
}
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.
the class ActivityProcessor method showEatingSnake.
/**
* Shows eating snake.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/activity/eating-snake", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = { CSRFToken.class, PermissionGrant.class, StopwatchEndAdvice.class })
public void showEatingSnake(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("/activity/eating-snake.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModelService.fillHeaderAndFooter(request, response, dataModel);
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
final List<JSONObject> maxUsers = activityQueryService.getTopEatingSnakeUsersMax(avatarViewMode, 10);
dataModel.put("maxUsers", (Object) maxUsers);
final List<JSONObject> sumUsers = activityQueryService.getTopEatingSnakeUsersSum(avatarViewMode, 10);
dataModel.put("sumUsers", (Object) sumUsers);
final JSONObject user = (JSONObject) request.getAttribute(User.USER);
final String userId = user.optString(Keys.OBJECT_ID);
final int startPoint = activityQueryService.getEatingSnakeAvgPoint(userId);
String pointActivityEatingSnake = langPropsService.get("activityStartEatingSnakeTipLabel");
pointActivityEatingSnake = pointActivityEatingSnake.replace("{point}", String.valueOf(startPoint));
dataModel.put("activityStartEatingSnakeTipLabel", pointActivityEatingSnake);
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.
the class ActivityProcessor method showGobang.
/**
* Shows gobang.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/activity/gobang", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = { CSRFToken.class, PermissionGrant.class, StopwatchEndAdvice.class })
public void showGobang(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("/activity/gobang.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModelService.fillHeaderAndFooter(request, response, dataModel);
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideTags(dataModel);
String pointActivityGobang = langPropsService.get("activityStartGobangTipLabel");
pointActivityGobang = pointActivityGobang.replace("{point}", String.valueOf(Pointtransfer.TRANSFER_SUM_C_ACTIVITY_GOBANG_START));
dataModel.put("activityStartGobangTipLabel", pointActivityGobang);
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.
the class AdminProcessor method addTag.
/**
* Adds a tag.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @throws Exception exception
*/
@RequestProcessing(value = "/admin/add-tag", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void addTag(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
String title = StringUtils.trim(request.getParameter(Tag.TAG_TITLE));
try {
if (Strings.isEmptyOrNull(title)) {
throw new Exception(langPropsService.get("tagsErrorLabel"));
}
title = Tag.formatTags(title);
if (!Tag.containsWhiteListTags(title)) {
if (!Tag.TAG_TITLE_PATTERN.matcher(title).matches()) {
throw new Exception(langPropsService.get("tagsErrorLabel"));
}
if (title.length() > Tag.MAX_TAG_TITLE_LENGTH) {
throw new Exception(langPropsService.get("tagsErrorLabel"));
}
}
} catch (final Exception e) {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("admin/error.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.put(Keys.MSG, e.getMessage());
dataModelService.fillHeaderAndFooter(request, response, dataModel);
return;
}
final JSONObject admin = (JSONObject) request.getAttribute(User.USER);
final String userId = admin.optString(Keys.OBJECT_ID);
String tagId;
try {
tagId = tagMgmtService.addTag(userId, title);
} catch (final ServiceException e) {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("admin/error.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.put(Keys.MSG, e.getMessage());
dataModelService.fillHeaderAndFooter(request, response, dataModel);
return;
}
response.sendRedirect(Latkes.getServePath() + "/admin/tag/" + tagId);
}
use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.
the class AdminProcessor method updateArticle.
/**
* Updates an article.
*
* @param context the specified context
* @param request the specified request
* @param response the specified response
* @param articleId the specified article id
* @throws Exception exception
*/
@RequestProcessing(value = "/admin/article/{articleId}", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void updateArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String articleId) throws Exception {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
context.setRenderer(renderer);
renderer.setTemplateName("admin/article.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
JSONObject article = articleQueryService.getArticle(articleId);
final Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
final String name = parameterNames.nextElement();
final String value = request.getParameter(name);
if (name.equals(Article.ARTICLE_REWARD_POINT) || name.equals(Article.ARTICLE_STATUS) || name.equals(Article.ARTICLE_TYPE) || name.equals(Article.ARTICLE_GOOD_CNT) || name.equals(Article.ARTICLE_BAD_CNT) || name.equals(Article.ARTICLE_PERFECT) || name.equals(Article.ARTICLE_ANONYMOUS_VIEW)) {
article.put(name, Integer.valueOf(value));
} else {
article.put(name, value);
}
}
final String articleTags = Tag.formatTags(article.optString(Article.ARTICLE_TAGS));
article.put(Article.ARTICLE_TAGS, articleTags);
articleMgmtService.updateArticleByAdmin(articleId, article);
article = articleQueryService.getArticle(articleId);
dataModel.put(Article.ARTICLE, article);
dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
Aggregations