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 3fa7e70 commit a816f2a
Showing 1 changed file with 33 additions and 44 deletions.
77 changes: 33 additions & 44 deletions src/main/java/org/b3log/symphony/processor/UserProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,23 @@
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
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.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.Pagination;
import org.b3log.latke.model.User;
import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.latke.util.Paginator;
import org.b3log.symphony.model.*;
import org.b3log.symphony.processor.middleware.AnonymousViewCheckMidware;
import org.b3log.symphony.processor.middleware.CSRFMidware;
import org.b3log.symphony.processor.middleware.PermissionMidware;
import org.b3log.symphony.processor.middleware.UserCheckMidware;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchEndAdvice;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchStartAdvice;
import org.b3log.symphony.service.*;
import org.b3log.symphony.util.*;
import org.json.JSONObject;
Expand All @@ -65,10 +63,10 @@
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="https://hacpai.com/member/ZephyrJung">Zephyr</a>
* @author <a href="http://vanessa.b3log.org">Liyuan Li</a>
* @version 1.27.1.0, Jul 7, 2019
* @version 2.0.0.0, Feb 11, 2020
* @since 0.2.0
*/
@RequestProcessor
@Singleton
public class UserProcessor {

/**
Expand Down Expand Up @@ -167,14 +165,37 @@ public class UserProcessor {
@Inject
private BreezemoonQueryService breezemoonQueryService;

/**
* Register request handlers.
*/
public static void register() {
final BeanManager beanManager = BeanManager.getInstance();
final PermissionMidware permissionMidware = beanManager.getReference(PermissionMidware.class);
final AnonymousViewCheckMidware anonymousViewCheckMidware = beanManager.getReference(AnonymousViewCheckMidware.class);
final CSRFMidware csrfMidware = beanManager.getReference(CSRFMidware.class);
final UserCheckMidware userCheckMidware = beanManager.getReference(UserCheckMidware.class);

final UserProcessor userProcessor = beanManager.getReference(UserProcessor.class);
Dispatcher.get("/member/{userName}", userProcessor::showHome, anonymousViewCheckMidware::handle, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.group().middlewares(anonymousViewCheckMidware::handle, userCheckMidware::handle, csrfMidware::fill, permissionMidware::grant).router().get().uris(new String[]{"/member/{userName}/breezemoons", "/member/{userName}/breezemoons/{breezemoonId}"}).handler(userProcessor::showHomeBreezemoons);
Dispatcher.get("/member/{userName}/comments/anonymous", userProcessor::showHomeAnonymousComments, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.get("/member/{userName}/articles/anonymous", userProcessor::showAnonymousArticles, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.get("/member/{userName}/comments", userProcessor::showHomeComments, anonymousViewCheckMidware::handle, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.get("/member/{userName}/following/users", userProcessor::showHomeFollowingUsers, anonymousViewCheckMidware::handle, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.get("/member/{userName}/following/tags", userProcessor::showHomeFollowingTags, anonymousViewCheckMidware::handle, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.get("/member/{userName}/following/articles", userProcessor::showHomeFollowingArticles, anonymousViewCheckMidware::handle, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.get("/member/{userName}/watching/articles", userProcessor::showHomeWatchingArticles, anonymousViewCheckMidware::handle, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.get("/member/{userName}/followers", userProcessor::showHomeFollowers, anonymousViewCheckMidware::handle, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.get("/member/{userName}/points", userProcessor::showHomePoints, anonymousViewCheckMidware::handle, userCheckMidware::handle, permissionMidware::grant);
Dispatcher.post("/users/names", userProcessor::listNames);
Dispatcher.get("/users/emotions", userProcessor::getFrequentEmotions);
}

/**
* Shows user home breezemoons page.
*
* @param context the specified context
*/
@RequestProcessing(value = {"/member/{userName}/breezemoons", "/member/{userName}/breezemoons/{breezemoonId}"}, method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class, UserCheckMidware.class})
@After({CSRFToken.class, PermissionGrant.class, StopwatchEndAdvice.class})
public void showHomeBreezemoons(final RequestContext context) {
final String breezemoonId = context.pathVar("breezemoonId");
final Request request = context.getRequest();
Expand Down Expand Up @@ -247,9 +268,6 @@ public void showHomeBreezemoons(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}/comments/anonymous", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showHomeAnonymousComments(final RequestContext context) {
final Request request = context.getRequest();

Expand Down Expand Up @@ -321,9 +339,6 @@ public void showHomeAnonymousComments(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}/articles/anonymous", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showAnonymousArticles(final RequestContext context) {
final String userName = context.pathVar("userName");
final Request request = context.getRequest();
Expand Down Expand Up @@ -397,9 +412,6 @@ public void showAnonymousArticles(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showHome(final RequestContext context) {
final String userName = context.pathVar("userName");
final Request request = context.getRequest();
Expand Down Expand Up @@ -466,9 +478,6 @@ public void showHome(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}/comments", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showHomeComments(final RequestContext context) {
final Request request = context.getRequest();

Expand Down Expand Up @@ -528,9 +537,6 @@ public void showHomeComments(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}/following/users", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showHomeFollowingUsers(final RequestContext context) {
final Request request = context.getRequest();

Expand Down Expand Up @@ -591,9 +597,6 @@ public void showHomeFollowingUsers(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}/following/tags", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showHomeFollowingTags(final RequestContext context) {
final Request request = context.getRequest();

Expand Down Expand Up @@ -654,9 +657,6 @@ public void showHomeFollowingTags(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}/following/articles", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showHomeFollowingArticles(final RequestContext context) {
final Request request = context.getRequest();

Expand Down Expand Up @@ -717,9 +717,6 @@ public void showHomeFollowingArticles(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}/watching/articles", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showHomeWatchingArticles(final RequestContext context) {
final Request request = context.getRequest();

Expand Down Expand Up @@ -780,9 +777,6 @@ public void showHomeWatchingArticles(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}/followers", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showHomeFollowers(final RequestContext context) {
final Request request = context.getRequest();

Expand Down Expand Up @@ -849,9 +843,6 @@ public void showHomeFollowers(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/member/{userName}/points", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class, UserCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showHomePoints(final RequestContext context) {
final Request request = context.getRequest();

Expand Down Expand Up @@ -906,7 +897,6 @@ public void showHomePoints(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/users/names", method = HttpMethod.POST)
public void listNames(final RequestContext context) {
final JSONObject result = Results.newSucc();
context.renderJSON(result);
Expand Down Expand Up @@ -939,7 +929,6 @@ public void listNames(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/users/emotions", method = HttpMethod.GET)
public void getFrequentEmotions(final RequestContext context) {
final JSONObject result = Results.newSucc();
context.renderJSON(result);
Expand Down

0 comments on commit a816f2a

Please sign in to comment.