Search in sources :

Example 66 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class ErrorProcessor method handleErrorPage.

/**
 * Handles the error.
 *
 * @param context    the specified context
 * @param request    the specified HTTP servlet request
 * @param response   the specified HTTP servlet response
 * @param statusCode the specified status code
 * @throws Exception exception
 */
@RequestProcessing(value = "/error/{statusCode}", method = { HTTPRequestMethod.GET, HTTPRequestMethod.POST })
@Before(adviceClass = StopwatchStartAdvice.class)
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void handleErrorPage(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String statusCode) throws Exception {
    if (StringUtils.equals("GET", request.getMethod())) {
        final String requestURI = request.getRequestURI();
        final String templateName = statusCode + ".ftl";
        LOGGER.log(Level.TRACE, "Shows error page[requestURI={0}, templateName={1}]", requestURI, templateName);
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        renderer.setTemplateName("error/" + templateName);
        context.setRenderer(renderer);
        final Map<String, Object> dataModel = renderer.getDataModel();
        dataModel.putAll(langPropsService.getAll(Locales.getLocale()));
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        if (HttpServletResponse.SC_FORBIDDEN == Integer.valueOf(statusCode)) {
            dataModelService.fillSideHotArticles(UserExt.USER_AVATAR_VIEW_MODE_C_ORIGINAL, dataModel);
            dataModelService.fillRandomArticles(UserExt.USER_AVATAR_VIEW_MODE_C_ORIGINAL, dataModel);
            dataModelService.fillSideTags(dataModel);
        }
    } else {
        context.renderJSON().renderMsg(statusCode);
    }
}
Also used : 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 67 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class FollowProcessor method followUser.

/**
 * Follows a user.
 * <p>
 * The request json object:
 * <pre>
 * {
 *   "followingId": ""
 * }
 * </pre>
 * </p>
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/follow/user", method = HTTPRequestMethod.POST)
@Before(adviceClass = LoginCheck.class)
public void followUser(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    context.renderJSON();
    final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
    final String followingUserId = requestJSONObject.optString(Follow.FOLLOWING_ID);
    final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
    final String followerUserId = currentUser.optString(Keys.OBJECT_ID);
    followMgmtService.followUser(followerUserId, followingUserId);
    if (!FOLLOWS.contains(followingUserId + followerUserId)) {
        final JSONObject notification = new JSONObject();
        notification.put(Notification.NOTIFICATION_USER_ID, followingUserId);
        notification.put(Notification.NOTIFICATION_DATA_ID, followerUserId);
        notificationMgmtService.addNewFollowerNotification(notification);
    }
    FOLLOWS.add(followingUserId + followerUserId);
    context.renderTrueResult();
}
Also used : JSONObject(org.json.JSONObject) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 68 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class FollowProcessor method unfollowArticle.

/**
 * Unfollows an article.
 * <p>
 * The request json object:
 * <pre>
 * {
 *   "followingId": ""
 * }
 * </pre>
 * </p>
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/follow/article", method = HTTPRequestMethod.DELETE)
@Before(adviceClass = LoginCheck.class)
public void unfollowArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    context.renderJSON();
    final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
    final String followingArticleId = requestJSONObject.optString(Follow.FOLLOWING_ID);
    final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
    final String followerUserId = currentUser.optString(Keys.OBJECT_ID);
    followMgmtService.unfollowArticle(followerUserId, followingArticleId);
    context.renderTrueResult();
}
Also used : JSONObject(org.json.JSONObject) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 69 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class FollowProcessor method unwatchArticle.

/**
 * Unwatches an article.
 * <p>
 * The request json object:
 * <pre>
 * {
 *   "followingId": ""
 * }
 * </pre>
 * </p>
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/follow/article-watch", method = HTTPRequestMethod.DELETE)
@Before(adviceClass = LoginCheck.class)
public void unwatchArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    context.renderJSON();
    final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
    final String followingArticleId = requestJSONObject.optString(Follow.FOLLOWING_ID);
    final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
    final String followerUserId = currentUser.optString(Keys.OBJECT_ID);
    followMgmtService.unwatchArticle(followerUserId, followingArticleId);
    context.renderTrueResult();
}
Also used : JSONObject(org.json.JSONObject) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 70 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class StatisticProcessor method loadStatData.

/**
 * Loads statistic data.
 *
 * @param request  the specified HTTP servlet request
 * @param response the specified HTTP servlet response
 * @param context  the specified HTTP request context
 * @throws Exception exception
 */
@RequestProcessing(value = "/cron/stat", method = HTTPRequestMethod.GET)
@Before(adviceClass = StopwatchStartAdvice.class)
@After(adviceClass = StopwatchEndAdvice.class)
public void loadStatData(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    final Date end = new Date();
    final Date dayStart = DateUtils.addDays(end, -30);
    monthDays.clear();
    userCnts.clear();
    articleCnts.clear();
    commentCnts.clear();
    months.clear();
    historyArticleCnts.clear();
    historyCommentCnts.clear();
    historyUserCnts.clear();
    for (int i = 0; i < 31; i++) {
        final Date day = DateUtils.addDays(dayStart, i);
        monthDays.add(DateFormatUtils.format(day, "yyyy-MM-dd"));
        final int userCnt = userQueryService.getUserCntInDay(day);
        userCnts.add(userCnt);
        final int articleCnt = articleQueryService.getArticleCntInDay(day);
        articleCnts.add(articleCnt);
        final int commentCnt = commentQueryService.getCommentCntInDay(day);
        commentCnts.add(commentCnt);
    }
    final JSONObject firstAdmin = userQueryService.getAdmins().get(0);
    final long monthStartTime = Times.getMonthStartTime(firstAdmin.optLong(Keys.OBJECT_ID));
    final Date monthStart = new Date(monthStartTime);
    int i = 1;
    while (true) {
        final Date month = DateUtils.addMonths(monthStart, i);
        if (month.after(end)) {
            break;
        }
        i++;
        months.add(DateFormatUtils.format(month, "yyyy-MM"));
        final int userCnt = userQueryService.getUserCntInMonth(month);
        historyUserCnts.add(userCnt);
        final int articleCnt = articleQueryService.getArticleCntInMonth(month);
        historyArticleCnts.add(articleCnt);
        final int commentCnt = commentQueryService.getCommentCntInMonth(month);
        historyCommentCnts.add(commentCnt);
    }
}
Also used : JSONObject(org.json.JSONObject) Date(java.util.Date) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

Before (org.b3log.latke.servlet.annotation.Before)169 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)169 JSONObject (org.json.JSONObject)166 After (org.b3log.latke.servlet.annotation.After)135 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)105 ServiceException (org.b3log.latke.service.ServiceException)37 Date (java.util.Date)13 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 JSONArray (org.json.JSONArray)8 Auth (com.qiniu.util.Auth)7 ParseException (java.text.ParseException)6 List (java.util.List)6 ServletException (javax.servlet.ServletException)3 Configuration (com.qiniu.storage.Configuration)1 UploadManager (com.qiniu.storage.UploadManager)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1