Skip to content

Commit

Permalink
♻️ 重构请求路由 #5
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Feb 11, 2020
1 parent 4134162 commit f65237e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 81 deletions.
62 changes: 30 additions & 32 deletions src/main/java/org/b3log/symphony/processor/CommentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.b3log.latke.Keys;
import org.b3log.latke.http.HttpMethod;
import org.b3log.latke.http.Dispatcher;
import org.b3log.latke.http.Request;
import org.b3log.latke.http.RequestContext;
import org.b3log.latke.http.annotation.After;
import org.b3log.latke.http.annotation.Before;
import org.b3log.latke.http.annotation.RequestProcessing;
import org.b3log.latke.http.annotation.RequestProcessor;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.ioc.Inject;
import org.b3log.latke.ioc.Singleton;
import org.b3log.latke.model.User;
import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.service.ServiceException;
Expand All @@ -37,10 +35,8 @@
import org.b3log.symphony.processor.middleware.CSRFMidware;
import org.b3log.symphony.processor.middleware.LoginCheckMidware;
import org.b3log.symphony.processor.middleware.PermissionMidware;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchEndAdvice;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchStartAdvice;
import org.b3log.symphony.processor.middleware.validate.CommentAddValidation;
import org.b3log.symphony.processor.middleware.validate.CommentUpdateValidation;
import org.b3log.symphony.processor.middleware.validate.CommentAddValidationMidware;
import org.b3log.symphony.processor.middleware.validate.CommentUpdateValidationMidware;
import org.b3log.symphony.service.*;
import org.b3log.symphony.util.*;
import org.json.JSONObject;
Expand All @@ -63,10 +59,10 @@
* </ul>
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.8.0.5, Dec 16, 2018
* @version 2.0.0.0, Feb 11, 2020
* @since 0.2.0
*/
@RequestProcessor
@Singleton
public class CommentProcessor {

/**
Expand Down Expand Up @@ -128,17 +124,37 @@ public class CommentProcessor {
@Inject
private FollowMgmtService followMgmtService;

/**
* Register request handlers.
*/
public static void register() {
final BeanManager beanManager = BeanManager.getInstance();
final LoginCheckMidware loginCheck = beanManager.getReference(LoginCheckMidware.class);
final PermissionMidware permissionMidware = beanManager.getReference(PermissionMidware.class);
final CSRFMidware csrfMidware = beanManager.getReference(CSRFMidware.class);
final CommentUpdateValidationMidware commentUpdateValidationMidware = beanManager.getReference(CommentUpdateValidationMidware.class);
final CommentAddValidationMidware commentAddValidationMidware = beanManager.getReference(CommentAddValidationMidware.class);

final CommentProcessor commentProcessor = beanManager.getReference(CommentProcessor.class);
Dispatcher.post("/comment/accept", commentProcessor::acceptComment, loginCheck::handle, csrfMidware::check, permissionMidware::check);
Dispatcher.post("/comment/{id}/remove", commentProcessor::removeComment, loginCheck::handle, permissionMidware::check);
Dispatcher.get("/comment/{id}/revisions", commentProcessor::getCommentRevisions, loginCheck::handle, permissionMidware::check);
Dispatcher.get("/comment/{id}/content", commentProcessor::getCommentContent, loginCheck::handle);
Dispatcher.put("/comment/{id}", commentProcessor::updateComment, loginCheck::handle, csrfMidware::check, permissionMidware::check, commentUpdateValidationMidware::handle);
Dispatcher.post("/comment/original", commentProcessor::getOriginalComment);
Dispatcher.post("/comment/replies", commentProcessor::getReplies);
Dispatcher.post("/comment", commentProcessor::addComment, loginCheck::handle, csrfMidware::check, permissionMidware::check, commentAddValidationMidware::handle);
Dispatcher.post("/comment/thank", commentProcessor::thankComment, loginCheck::handle, csrfMidware::check, permissionMidware::check);
}

/**
* Accepts a comment.
*
* @param context the specified context
*/
@RequestProcessing(value = "/comment/accept", method = HttpMethod.POST)
@Before({LoginCheckMidware.class, CSRFMidware.class, PermissionMidware.class})
public void acceptComment(final RequestContext context) {
context.renderJSON();

final Request request = context.getRequest();
final JSONObject requestJSONObject = context.requestJSON();
final JSONObject currentUser = Sessions.getUser();
final String userId = currentUser.optString(Keys.OBJECT_ID);
Expand Down Expand Up @@ -179,12 +195,8 @@ public void acceptComment(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/comment/{id}/remove", method = HttpMethod.POST)
@Before({StopwatchStartAdvice.class, LoginCheckMidware.class, PermissionMidware.class})
@After({StopwatchEndAdvice.class})
public void removeComment(final RequestContext context) {
final String id = context.pathVar("id");
final Request request = context.getRequest();
if (StringUtils.isBlank(id)) {
context.sendError(404);

Expand Down Expand Up @@ -226,9 +238,6 @@ public void removeComment(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/comment/{id}/revisions", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, LoginCheckMidware.class, PermissionMidware.class})
@After({StopwatchEndAdvice.class})
public void getCommentRevisions(final RequestContext context) {
final String id = context.pathVar("id");
final JSONObject viewer = Sessions.getUser();
Expand All @@ -245,8 +254,6 @@ public void getCommentRevisions(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/comment/{id}/content", method = HttpMethod.GET)
@Before({LoginCheckMidware.class})
public void getCommentContent(final RequestContext context) {
final String id = context.pathVar("id");
context.renderJSON().renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
Expand Down Expand Up @@ -284,8 +291,6 @@ public void getCommentContent(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/comment/{id}", method = HttpMethod.PUT)
@Before({CSRFMidware.class, LoginCheckMidware.class, CommentUpdateValidation.class, PermissionMidware.class})
public void updateComment(final RequestContext context) {
final String id = context.pathVar("id");
context.renderJSON().renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
Expand Down Expand Up @@ -349,9 +354,7 @@ public void updateComment(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/comment/original", method = HttpMethod.POST)
public void getOriginalComment(final RequestContext context) {
final Request request = context.getRequest();
final JSONObject requestJSONObject = context.requestJSON();
final String commentId = requestJSONObject.optString(Comment.COMMENT_T_ID);
int commentViewMode = requestJSONObject.optInt(UserExt.USER_COMMENT_VIEW_MODE);
Expand Down Expand Up @@ -380,7 +383,6 @@ public void getOriginalComment(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/comment/replies", method = HttpMethod.POST)
public void getReplies(final RequestContext context) {
final JSONObject requestJSONObject = context.requestJSON();
final String commentId = requestJSONObject.optString(Comment.COMMENT_T_ID);
Expand Down Expand Up @@ -434,8 +436,6 @@ public void getReplies(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/comment", method = HttpMethod.POST)
@Before({CSRFMidware.class, LoginCheckMidware.class, CommentAddValidation.class, PermissionMidware.class})
public void addComment(final RequestContext context) {
context.renderJSON().renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);

Expand Down Expand Up @@ -520,8 +520,6 @@ public void addComment(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/comment/thank", method = HttpMethod.POST)
@Before({LoginCheckMidware.class, CSRFMidware.class, PermissionMidware.class})
public void thankComment(final RequestContext context) {
context.renderJSON();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.b3log.latke.Keys;
import org.b3log.latke.http.Request;
import org.b3log.latke.http.RequestContext;
import org.b3log.latke.http.advice.ProcessAdvice;
import org.b3log.latke.http.advice.RequestProcessAdviceException;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.ioc.Singleton;
import org.b3log.latke.service.LangPropsService;
Expand All @@ -38,27 +36,16 @@
* Validates for comment adding locally.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.3.0.4, Apr 7, 2019
* @version 2.0.0.0, Feb 11, 2020
* @since 0.2.0
*/
@Singleton
public class CommentAddValidation extends ProcessAdvice {
public class CommentAddValidationMidware {

@Override
public void doAdvice(final RequestContext context) throws RequestProcessAdviceException {
public void handle(final RequestContext context) {
final Request request = context.getRequest();
final JSONObject requestJSONObject = context.requestJSON();
request.setAttribute(Keys.REQUEST, requestJSONObject);
validateCommentFields(requestJSONObject);
}

/**
* Validates comment fields.
*
* @param requestJSONObject the specified request object
* @throws RequestProcessAdviceException if validate failed
*/
public static void validateCommentFields(final JSONObject requestJSONObject) throws RequestProcessAdviceException {
final BeanManager beanManager = BeanManager.getInstance();
final LangPropsService langPropsService = beanManager.getReference(LangPropsService.class);
final OptionQueryService optionQueryService = beanManager.getReference(OptionQueryService.class);
Expand All @@ -70,33 +57,53 @@ public static void validateCommentFields(final JSONObject requestJSONObject) thr

final String commentContent = StringUtils.trim(requestJSONObject.optString(Comment.COMMENT_CONTENT));
if (StringUtils.isBlank(commentContent) || commentContent.length() > Comment.MAX_COMMENT_CONTENT_LENGTH) {
throw new RequestProcessAdviceException(exception.put(Keys.MSG, langPropsService.get("commentErrorLabel")));
context.renderJSON(exception.put(Keys.MSG, langPropsService.get("commentErrorLabel")));
context.abort();

return;
}

if (optionQueryService.containReservedWord(commentContent)) {
throw new RequestProcessAdviceException(exception.put(Keys.MSG, langPropsService.get("contentContainReservedWordLabel")));
context.renderJSON(exception.put(Keys.MSG, langPropsService.get("contentContainReservedWordLabel")));
context.abort();

return;
}

final String articleId = requestJSONObject.optString(Article.ARTICLE_T_ID);
if (StringUtils.isBlank(articleId)) {
throw new RequestProcessAdviceException(exception.put(Keys.MSG, langPropsService.get("commentArticleErrorLabel")));
context.renderJSON(exception.put(Keys.MSG, langPropsService.get("commentArticleErrorLabel")));
context.abort();

return;
}

final JSONObject article = articleQueryService.getArticleById(articleId);
if (null == article) {
throw new RequestProcessAdviceException(exception.put(Keys.MSG, langPropsService.get("commentArticleErrorLabel")));
context.renderJSON(exception.put(Keys.MSG, langPropsService.get("commentArticleErrorLabel")));
context.abort();

return;
}

if (!article.optBoolean(Article.ARTICLE_COMMENTABLE)) {
throw new RequestProcessAdviceException(exception.put(Keys.MSG, langPropsService.get("notAllowCmtLabel")));
context.renderJSON(exception.put(Keys.MSG, langPropsService.get("notAllowCmtLabel")));
context.abort();

return;
}

final String originalCommentId = requestJSONObject.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
if (StringUtils.isNotBlank(originalCommentId)) {
final JSONObject originalCmt = commentQueryService.getComment(originalCommentId);
if (null == originalCmt) {
throw new RequestProcessAdviceException(exception.put(Keys.MSG, langPropsService.get("commentArticleErrorLabel")));
context.renderJSON(exception.put(Keys.MSG, langPropsService.get("commentArticleErrorLabel")));
context.abort();

return;
}
}

context.handle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.b3log.latke.Keys;
import org.b3log.latke.http.Request;
import org.b3log.latke.http.RequestContext;
import org.b3log.latke.http.advice.ProcessAdvice;
import org.b3log.latke.http.advice.RequestProcessAdviceException;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.ioc.Inject;
import org.b3log.latke.ioc.Singleton;
Expand All @@ -38,11 +36,11 @@
* Validates for comment updating locally.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, J, 2019
* @version 2.0.0.0, Feb 11, 2020
* @since 2.1.0
*/
@Singleton
public class CommentUpdateValidation extends ProcessAdvice {
public class CommentUpdateValidationMidware {

/**
* Language service.
Expand All @@ -68,13 +66,10 @@ public class CommentUpdateValidation extends ProcessAdvice {
@Inject
private OptionQueryService optionQueryService;

/**
* Validates comment fields.
*
* @param requestJSONObject the specified request object
* @throws RequestProcessAdviceException if validate failed
*/
private static void validateCommentFields(final JSONObject requestJSONObject) throws RequestProcessAdviceException {
public void handle(final RequestContext context) {
final Request request = context.getRequest();
final JSONObject requestJSONObject = context.requestJSON();
request.setAttribute(Keys.REQUEST, requestJSONObject);
final BeanManager beanManager = BeanManager.getInstance();
final LangPropsService langPropsService = beanManager.getReference(LangPropsService.class);
final OptionQueryService optionQueryService = beanManager.getReference(OptionQueryService.class);
Expand All @@ -84,27 +79,19 @@ private static void validateCommentFields(final JSONObject requestJSONObject) th

final String commentContent = StringUtils.trim(requestJSONObject.optString(Comment.COMMENT_CONTENT));
if (StringUtils.isBlank(commentContent) || commentContent.length() > Comment.MAX_COMMENT_CONTENT_LENGTH) {
throw new RequestProcessAdviceException(exception.put(Keys.MSG, langPropsService.get("commentErrorLabel")));
}
context.renderJSON(exception.put(Keys.MSG, langPropsService.get("commentErrorLabel")));
context.abort();

if (optionQueryService.containReservedWord(commentContent)) {
throw new RequestProcessAdviceException(exception.put(Keys.MSG, langPropsService.get("contentContainReservedWordLabel")));
return;
}
}

@Override
public void doAdvice(final RequestContext context) throws RequestProcessAdviceException {
final Request request = context.getRequest();
if (optionQueryService.containReservedWord(commentContent)) {
context.renderJSON(exception.put(Keys.MSG, langPropsService.get("contentContainReservedWordLabel")));
context.abort();

JSONObject requestJSONObject;
try {
requestJSONObject = context.requestJSON();
request.setAttribute(Keys.REQUEST, requestJSONObject);
} catch (final Exception e) {
throw new RequestProcessAdviceException(new JSONObject().put(Keys.MSG, e.getMessage()).
put(Keys.STATUS_CODE, StatusCodes.ERR));
return;
}

validateCommentFields(requestJSONObject);
context.handle();
}
}

0 comments on commit f65237e

Please sign in to comment.