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 8f97040 commit 4134162
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
23 changes: 16 additions & 7 deletions src/main/java/org/b3log/symphony/processor/CaptchaProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.b3log.latke.http.HttpMethod;
import org.b3log.latke.http.Dispatcher;
import org.b3log.latke.http.RequestContext;
import org.b3log.latke.http.Response;
import org.b3log.latke.http.annotation.RequestProcessing;
import org.b3log.latke.http.annotation.RequestProcessor;
import org.b3log.latke.http.renderer.PngRenderer;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.ioc.Singleton;
import org.b3log.latke.util.Strings;
import org.b3log.symphony.model.Common;
import org.json.JSONObject;
Expand All @@ -53,10 +53,10 @@
* Captcha processor.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 2.3.0.6, Nov 2, 2018
* @version 3.0.0.0, Feb 11, 2020
* @since 0.2.2
*/
@RequestProcessor
@Singleton
public class CaptchaProcessor {

/**
Expand Down Expand Up @@ -84,6 +84,17 @@ public class CaptchaProcessor {
*/
private static final String CHARS = "acdefhijklmnprstuvwxy234578";

/**
* Register request handlers.
*/
public static void register() {
final BeanManager beanManager = BeanManager.getInstance();
final CaptchaProcessor captchaProcessor = beanManager.getReference(CaptchaProcessor.class);

Dispatcher.get("/captcha", captchaProcessor::get);
Dispatcher.get("/captcha/login", captchaProcessor::getLoginCaptcha);
}

/**
* Checks whether the specified captcha is invalid.
*
Expand All @@ -108,7 +119,6 @@ public static boolean invalidCaptcha(final String captcha) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/captcha", method = HttpMethod.GET)
public void get(final RequestContext context) {
final PngRenderer renderer = new PngRenderer();
context.setRenderer(renderer);
Expand Down Expand Up @@ -154,7 +164,6 @@ public void get(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/captcha/login", method = HttpMethod.GET)
public void getLoginCaptcha(final RequestContext context) {
try {
final Response response = context.getResponse();
Expand Down
39 changes: 23 additions & 16 deletions src/main/java/org/b3log/symphony/processor/ChatroomProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,22 @@
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.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.http.renderer.AbstractFreeMarkerRenderer;
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.util.Locales;
import org.b3log.latke.util.Times;
import org.b3log.symphony.model.Common;
import org.b3log.symphony.model.UserExt;
import org.b3log.symphony.processor.channel.ChatroomChannel;
import org.b3log.symphony.processor.middleware.AnonymousViewCheckMidware;
import org.b3log.symphony.processor.middleware.LoginCheckMidware;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchEndAdvice;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchStartAdvice;
import org.b3log.symphony.processor.middleware.validate.ChatMsgAddValidation;
import org.b3log.symphony.processor.channel.ChatroomChannel;
import org.b3log.symphony.processor.middleware.PermissionMidware;
import org.b3log.symphony.processor.middleware.validate.ChatMsgAddValidationMidware;
import org.b3log.symphony.service.*;
import org.b3log.symphony.util.*;
import org.json.JSONObject;
Expand All @@ -59,10 +56,10 @@
* </ul>
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.3.5.22, Sep 6, 2019
* @version 2.0.0.0, Feb 11, 2020
* @since 1.4.0
*/
@RequestProcessor
@Singleton
public class ChatroomProcessor {

/**
Expand Down Expand Up @@ -129,6 +126,21 @@ public class ChatroomProcessor {
@Inject
private ArticleQueryService articleQueryService;

/**
* 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 AnonymousViewCheckMidware anonymousViewCheckMidware = beanManager.getReference(AnonymousViewCheckMidware.class);
final ChatMsgAddValidationMidware chatMsgAddValidationMidware = beanManager.getReference(ChatMsgAddValidationMidware.class);

final ChatroomProcessor chatroomProcessor = beanManager.getReference(ChatroomProcessor.class);
Dispatcher.post("/chat-room/send", chatroomProcessor::addChatRoomMsg, loginCheck::handle, chatMsgAddValidationMidware::handle);
Dispatcher.get("/cr", chatroomProcessor::showChatRoom, anonymousViewCheckMidware::handle, permissionMidware::grant);
}

/**
* Adds a chat message.
* <p>
Expand All @@ -142,8 +154,6 @@ public class ChatroomProcessor {
*
* @param context the specified context
*/
@RequestProcessing(value = "/chat-room/send", method = HttpMethod.POST)
@Before({LoginCheckMidware.class, ChatMsgAddValidation.class})
public synchronized void addChatRoomMsg(final RequestContext context) {
context.renderJSON();

Expand Down Expand Up @@ -191,9 +201,6 @@ public synchronized void addChatRoomMsg(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/cr", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showChatRoom(final RequestContext context) {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(context, "chat-room.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
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.Inject;
import org.b3log.latke.ioc.Singleton;
import org.b3log.latke.model.User;
Expand All @@ -40,11 +38,11 @@
* Validates for chat message adding.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.2, Jun 2, 2018
* @version 2.0.0.0, Feb 11, 2020
* @since 1.4.0
*/
@Singleton
public class ChatMsgAddValidation extends ProcessAdvice {
public class ChatMsgAddValidationMidware {

/**
* Language service.
Expand All @@ -64,34 +62,36 @@ public class ChatMsgAddValidation extends ProcessAdvice {
@Inject
private UserQueryService userQueryService;

@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);
final JSONObject currentUser = Sessions.getUser();
if (System.currentTimeMillis() - currentUser.optLong(UserExt.USER_LATEST_CMT_TIME) < Symphonys.MIN_STEP_CHAT_TIME
&& !Role.ROLE_ID_C_ADMIN.equals(currentUser.optString(User.USER_ROLE))) {
context.renderJSON(new JSONObject().put(Keys.MSG, langPropsService.get("tooFrequentCmtLabel")));
context.abort();

JSONObject requestJSONObject;
try {
requestJSONObject = context.requestJSON();
request.setAttribute(Keys.REQUEST, requestJSONObject);

final JSONObject currentUser = Sessions.getUser();
if (System.currentTimeMillis() - currentUser.optLong(UserExt.USER_LATEST_CMT_TIME) < Symphonys.MIN_STEP_CHAT_TIME
&& !Role.ROLE_ID_C_ADMIN.equals(currentUser.optString(User.USER_ROLE))) {
throw new Exception(langPropsService.get("tooFrequentCmtLabel"));
}
} catch (final Exception e) {
throw new RequestProcessAdviceException(new JSONObject().put(Keys.MSG, e.getMessage()));
return;
}

String content = requestJSONObject.optString(Common.CONTENT);
content = StringUtils.trim(content);
if (StringUtils.isBlank(content) || content.length() > 4096) {
throw new RequestProcessAdviceException(new JSONObject().put(Keys.MSG, langPropsService.get("commentErrorLabel")));
context.renderJSON(new JSONObject().put(Keys.MSG, langPropsService.get("commentErrorLabel")));
context.abort();

return;
}

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

return;
}

requestJSONObject.put(Common.CONTENT, content);
context.handle();
}
}

0 comments on commit 4134162

Please sign in to comment.