Search in sources :

Example 61 with Before

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

the class CommentProcessor method updateComment.

/**
 * Updates a comment locally.
 * <p>
 * The request json object:
 * <pre>
 * {
 *     "commentContent": ""
 * }
 * </pre>
 * </p>
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws IOException io exception
 */
@RequestProcessing(value = "/comment/{id}", method = HTTPRequestMethod.PUT)
@Before(adviceClass = { CSRFCheck.class, LoginCheck.class, CommentUpdateValidation.class, PermissionCheck.class })
public void updateComment(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String id) throws IOException {
    context.renderJSON().renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
    try {
        final JSONObject comment = commentQueryService.getComment(id);
        if (null == comment) {
            LOGGER.warn("Not found comment [id=" + id + "] to update");
            return;
        }
        final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
        if (!currentUser.optString(Keys.OBJECT_ID).equals(comment.optString(Comment.COMMENT_AUTHOR_ID))) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        final JSONObject requestJSONObject = (JSONObject) request.getAttribute(Keys.REQUEST);
        String commentContent = requestJSONObject.optString(Comment.COMMENT_CONTENT);
        final String ip = Requests.getRemoteAddr(request);
        String ua = request.getHeader(Common.USER_AGENT);
        comment.put(Comment.COMMENT_CONTENT, commentContent);
        comment.put(Comment.COMMENT_IP, "");
        if (StringUtils.isNotBlank(ip)) {
            comment.put(Comment.COMMENT_IP, ip);
        }
        comment.put(Comment.COMMENT_UA, "");
        if (StringUtils.isNotBlank(ua)) {
            ua = Jsoup.clean(ua, Whitelist.none());
            comment.put(Comment.COMMENT_UA, ua);
        }
        commentMgmtService.updateComment(comment.optString(Keys.OBJECT_ID), comment);
        commentContent = comment.optString(Comment.COMMENT_CONTENT);
        commentContent = shortLinkQueryService.linkArticle(commentContent);
        commentContent = shortLinkQueryService.linkTag(commentContent);
        commentContent = Emotions.toAliases(commentContent);
        commentContent = Emotions.convert(commentContent);
        commentContent = Markdowns.toHTML(commentContent);
        commentContent = Markdowns.clean(commentContent, "");
        commentContent = MP3Players.render(commentContent);
        commentContent = VideoPlayers.render(commentContent);
        context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.SUCC);
        context.renderJSONValue(Comment.COMMENT_CONTENT, commentContent);
    } catch (final ServiceException e) {
        context.renderMsg(e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 62 with Before

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

the class CommentProcessor method thankComment.

/**
 * Thanks a comment.
 * <p>
 * The request json object:
 * <pre>
 * {
 *     "commentId": "",
 * }
 * </pre>
 * </p>
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws IOException      io exception
 * @throws ServletException servlet exception
 */
@RequestProcessing(value = "/comment/thank", method = HTTPRequestMethod.POST)
@Before(adviceClass = { LoginCheck.class, CSRFCheck.class, PermissionCheck.class })
public void thankComment(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
    context.renderJSON();
    JSONObject requestJSONObject;
    try {
        requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
        request.setAttribute(Keys.REQUEST, requestJSONObject);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Thank comment error", e);
        return;
    }
    final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
    final String commentId = requestJSONObject.optString(Comment.COMMENT_T_ID);
    try {
        commentMgmtService.thankComment(commentId, currentUser.optString(Keys.OBJECT_ID));
        context.renderTrueResult().renderMsg(langPropsService.get("thankSentLabel"));
    } catch (final ServiceException e) {
        context.renderMsg(e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServletException(javax.servlet.ServletException) ServiceException(org.b3log.latke.service.ServiceException) IOException(java.io.IOException) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 63 with Before

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

the class CommentProcessor method getCommentRevisions.

/**
 * Gets a comment's revisions.
 *
 * @param context the specified context
 * @param id      the specified comment id
 */
@RequestProcessing(value = "/comment/{id}/revisions", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class, PermissionCheck.class })
@After(adviceClass = { StopwatchEndAdvice.class })
public void getCommentRevisions(final HTTPRequestContext context, final String id) {
    final List<JSONObject> revisions = revisionQueryService.getCommentRevisions(id);
    final JSONObject ret = new JSONObject();
    ret.put(Keys.STATUS_CODE, true);
    ret.put(Revision.REVISIONS, (Object) revisions);
    context.renderJSON(ret);
}
Also used : JSONObject(org.json.JSONObject) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 64 with Before

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

the class CommentProcessor method addComment.

/**
 * Adds a comment locally.
 * <p>
 * The request json object (a comment):
 * <pre>
 * {
 *     "articleId": "",
 *     "commentContent": "",
 *     "commentAnonymous": boolean,
 *     "commentOriginalCommentId": "", // optional
 *     "userCommentViewMode": int
 * }
 * </pre>
 * </p>
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws IOException      io exception
 * @throws ServletException servlet exception
 */
@RequestProcessing(value = "/comment", method = HTTPRequestMethod.POST)
@Before(adviceClass = { CSRFCheck.class, CommentAddValidation.class, PermissionCheck.class })
public void addComment(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
    context.renderJSON().renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
    final JSONObject requestJSONObject = (JSONObject) request.getAttribute(Keys.REQUEST);
    final String articleId = requestJSONObject.optString(Article.ARTICLE_T_ID);
    final String commentContent = requestJSONObject.optString(Comment.COMMENT_CONTENT);
    final String commentOriginalCommentId = requestJSONObject.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
    final int commentViewMode = requestJSONObject.optInt(UserExt.USER_COMMENT_VIEW_MODE);
    final String ip = Requests.getRemoteAddr(request);
    String ua = request.getHeader(Common.USER_AGENT);
    final boolean isAnonymous = requestJSONObject.optBoolean(Comment.COMMENT_ANONYMOUS, false);
    final JSONObject comment = new JSONObject();
    comment.put(Comment.COMMENT_CONTENT, commentContent);
    comment.put(Comment.COMMENT_ON_ARTICLE_ID, articleId);
    comment.put(UserExt.USER_COMMENT_VIEW_MODE, commentViewMode);
    comment.put(Comment.COMMENT_IP, "");
    if (StringUtils.isNotBlank(ip)) {
        comment.put(Comment.COMMENT_IP, ip);
    }
    comment.put(Comment.COMMENT_UA, "");
    if (StringUtils.isNotBlank(ua)) {
        ua = Jsoup.clean(ua, Whitelist.none());
        comment.put(Comment.COMMENT_UA, ua);
    }
    comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, commentOriginalCommentId);
    try {
        final JSONObject currentUser = userQueryService.getCurrentUser(request);
        if (null == currentUser) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        final String currentUserName = currentUser.optString(User.USER_NAME);
        final JSONObject article = articleQueryService.getArticle(articleId);
        final String articleContent = article.optString(Article.ARTICLE_CONTENT);
        final String articleAuthorId = article.optString(Article.ARTICLE_AUTHOR_ID);
        final JSONObject articleAuthor = userQueryService.getUser(articleAuthorId);
        final String articleAuthorName = articleAuthor.optString(User.USER_NAME);
        final Set<String> userNames = userQueryService.getUserNames(articleContent);
        if (Article.ARTICLE_TYPE_C_DISCUSSION == article.optInt(Article.ARTICLE_TYPE) && !articleAuthorName.equals(currentUserName)) {
            boolean invited = false;
            for (final String userName : userNames) {
                if (userName.equals(currentUserName)) {
                    invited = true;
                    break;
                }
            }
            if (!invited) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
        }
        final String commentAuthorId = currentUser.optString(Keys.OBJECT_ID);
        comment.put(Comment.COMMENT_AUTHOR_ID, commentAuthorId);
        comment.put(Comment.COMMENT_T_COMMENTER, currentUser);
        comment.put(Comment.COMMENT_ANONYMOUS, isAnonymous ? Comment.COMMENT_ANONYMOUS_C_ANONYMOUS : Comment.COMMENT_ANONYMOUS_C_PUBLIC);
        commentMgmtService.addComment(comment);
        if (!commentAuthorId.equals(articleAuthorId)) {
            followMgmtService.watchArticle(commentAuthorId, articleId);
        }
        context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.SUCC);
    } catch (final ServiceException e) {
        context.renderMsg(e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 65 with Before

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

the class DomainProcessor method showDomains.

/**
 * Shows domains.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/domains", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showDomains(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("domains.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final JSONObject statistic = optionQueryService.getStatistic();
    final int tagCnt = statistic.optInt(Option.ID_C_STATISTIC_TAG_COUNT);
    dataModel.put(Tag.TAG_T_COUNT, tagCnt);
    final int domainCnt = statistic.optInt(Option.ID_C_STATISTIC_DOMAIN_COUNT);
    dataModel.put(Domain.DOMAIN_T_COUNT, domainCnt);
    final List<JSONObject> domains = domainQueryService.getAllDomains();
    dataModel.put(Common.ALL_DOMAINS, domains);
    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

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