Search in sources :

Example 66 with AbstractFreeMarkerRenderer

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());
        }
    }
}
Also used : Cookie(javax.servlet.http.Cookie) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONObject(org.json.JSONObject) FreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) IOException(java.io.IOException) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 67 with AbstractFreeMarkerRenderer

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);
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 68 with AbstractFreeMarkerRenderer

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);
}
Also used : JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 69 with AbstractFreeMarkerRenderer

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);
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) ServiceException(org.b3log.latke.service.ServiceException) ParseException(java.text.ParseException) IOException(java.io.IOException) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 70 with AbstractFreeMarkerRenderer

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);
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)120 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)119 JSONObject (org.json.JSONObject)117 After (org.b3log.latke.servlet.annotation.After)105 Before (org.b3log.latke.servlet.annotation.Before)105 ServiceException (org.b3log.latke.service.ServiceException)22 IOException (java.io.IOException)15 Date (java.util.Date)12 ArrayList (java.util.ArrayList)8 JSONArray (org.json.JSONArray)8 Auth (com.qiniu.util.Auth)7 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)7 ConsoleRenderer (org.b3log.solo.processor.renderer.ConsoleRenderer)7 ParseException (java.text.ParseException)6 List (java.util.List)6 EventException (org.b3log.latke.event.EventException)3 JSONException (org.json.JSONException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Locale (java.util.Locale)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2