diff --git a/src/main/java/org/b3log/symphony/processor/CommentProcessor.java b/src/main/java/org/b3log/symphony/processor/CommentProcessor.java
index b44d967c2..3df288f71 100644
--- a/src/main/java/org/b3log/symphony/processor/CommentProcessor.java
+++ b/src/main/java/org/b3log/symphony/processor/CommentProcessor.java
@@ -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;
@@ -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;
@@ -63,10 +59,10 @@
*
*
* @author Liang Ding
- * @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 {
/**
@@ -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);
@@ -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);
@@ -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();
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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();
diff --git a/src/main/java/org/b3log/symphony/processor/middleware/validate/CommentAddValidation.java b/src/main/java/org/b3log/symphony/processor/middleware/validate/CommentAddValidationMidware.java
similarity index 70%
rename from src/main/java/org/b3log/symphony/processor/middleware/validate/CommentAddValidation.java
rename to src/main/java/org/b3log/symphony/processor/middleware/validate/CommentAddValidationMidware.java
index 9c78b4e40..1481920ef 100644
--- a/src/main/java/org/b3log/symphony/processor/middleware/validate/CommentAddValidation.java
+++ b/src/main/java/org/b3log/symphony/processor/middleware/validate/CommentAddValidationMidware.java
@@ -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;
@@ -38,27 +36,16 @@
* Validates for comment adding locally.
*
* @author Liang Ding
- * @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);
@@ -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();
}
}
diff --git a/src/main/java/org/b3log/symphony/processor/middleware/validate/CommentUpdateValidation.java b/src/main/java/org/b3log/symphony/processor/middleware/validate/CommentUpdateValidationMidware.java
similarity index 68%
rename from src/main/java/org/b3log/symphony/processor/middleware/validate/CommentUpdateValidation.java
rename to src/main/java/org/b3log/symphony/processor/middleware/validate/CommentUpdateValidationMidware.java
index de2f7ea4a..b16c8b0c1 100644
--- a/src/main/java/org/b3log/symphony/processor/middleware/validate/CommentUpdateValidation.java
+++ b/src/main/java/org/b3log/symphony/processor/middleware/validate/CommentUpdateValidationMidware.java
@@ -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;
@@ -38,11 +36,11 @@
* Validates for comment updating locally.
*
* @author Liang Ding
- * @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.
@@ -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);
@@ -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();
}
}