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 14c103e commit 752dd1c
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/main/java/org/b3log/symphony/processor/TagProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,20 @@
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.util.Paginator;
import org.b3log.latke.util.URLs;
import org.b3log.symphony.model.*;
import org.b3log.symphony.processor.middleware.AnonymousViewCheckMidware;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchEndAdvice;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchStartAdvice;
import org.b3log.symphony.processor.middleware.LoginCheckMidware;
import org.b3log.symphony.processor.middleware.PermissionMidware;
import org.b3log.symphony.service.*;
import org.b3log.symphony.util.Sessions;
import org.b3log.symphony.util.Symphonys;
Expand All @@ -55,10 +53,10 @@
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="http://vanessa.b3log.org">Liyuan Li</a>
* @version 1.7.0.15, Jan 5, 2019
* @version 3.0.0.0, Feb 11, 2020
* @since 0.2.0
*/
@RequestProcessor
@Singleton
public class TagProcessor {

/**
Expand Down Expand Up @@ -91,12 +89,26 @@ public class TagProcessor {
@Inject
private DataModelService dataModelService;

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

final TagProcessor tagProcessor = beanManager.getReference(TagProcessor.class);
Dispatcher.get("/tags/query", tagProcessor::queryTags, loginCheck::handle);
Dispatcher.get("/tags", tagProcessor::showTagsWall, anonymousViewCheckMidware::handle, permissionMidware::grant);
Dispatcher.group().middlewares(anonymousViewCheckMidware::handle, permissionMidware::grant).router().get().uris(new String[]{"/tag/{tagURI}", "/tag/{tagURI}/hot", "/tag/{tagURI}/good", "/tag/{tagURI}/reply", "/tag/{tagURI}/perfect"}).handler(tagProcessor::showTagArticles);
}

/**
* Queries tags.
*
* @param context the specified context
*/
@RequestProcessing(value = "/tags/query", method = HttpMethod.GET)
public void queryTags(final RequestContext context) {
if (!Sessions.isLoggedIn()) {
context.setStatus(403);
Expand Down Expand Up @@ -129,9 +141,6 @@ public void queryTags(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/tags", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showTagsWall(final RequestContext context) {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(context, "tags.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
Expand All @@ -150,10 +159,6 @@ public void showTagsWall(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = {"/tag/{tagURI}", "/tag/{tagURI}/hot", "/tag/{tagURI}/good", "/tag/{tagURI}/reply",
"/tag/{tagURI}/perfect"}, method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, AnonymousViewCheckMidware.class})
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showTagArticles(final RequestContext context) {
final String tagURI = context.pathVar("tagURI");
final Request request = context.getRequest();
Expand Down

0 comments on commit 752dd1c

Please sign in to comment.