新增Controller全局拦截支持.
This commit is contained in:
@@ -84,6 +84,8 @@ public class HttpRequestHandler {
|
||||
HttpServerUtil.send404Response(context, requestParser.getUrl());
|
||||
return;
|
||||
}
|
||||
HttpContextHolder.setInterceptorList(getInterceptorsForPath(httpRouteResult.getPageRoute()));
|
||||
|
||||
if (HttpRouterType.METHOD == httpRouteResult.getType()) {
|
||||
if (!preHandle(context, requestParser, httpRouteResult.getPageRoute(), httpRouteResult.getMethod())) {
|
||||
return;
|
||||
@@ -97,6 +99,8 @@ public class HttpRequestHandler {
|
||||
FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(res.getBytes()));
|
||||
fullHttpResponse.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
|
||||
context.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE);
|
||||
|
||||
postHandle(context, requestParser, httpRouteResult.getPageRoute(), httpRouteResult.getMethod());
|
||||
return;
|
||||
} else if(HttpRouterType.PAGE == httpRouteResult.getType()) {
|
||||
if (!preHandle(context, requestParser, httpRouteResult.getPageRoute(), null)) {
|
||||
@@ -114,6 +118,8 @@ public class HttpRequestHandler {
|
||||
fullHttpResponse.headers().add(HttpHeaderNames.SERVER, MetaDataConstant.SERVER_VS);
|
||||
fullHttpResponse.headers().add(HttpHeaderNames.DATE, new Date());
|
||||
context.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE);
|
||||
|
||||
postHandle(context, requestParser, httpRouteResult.getPageRoute(), null);
|
||||
return;
|
||||
} else {
|
||||
HttpServerUtil.send404Response(context, requestParser.getUrl());
|
||||
@@ -127,9 +133,8 @@ public class HttpRequestHandler {
|
||||
}
|
||||
|
||||
private boolean preHandle(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod) throws Exception {
|
||||
List<HandlerInterceptor> list = getInterceptorsForPath(route);
|
||||
if (!CollectionUtil.isEmpty(list)) {
|
||||
for (HandlerInterceptor handlerInterceptor : list) {
|
||||
if (!CollectionUtil.isEmpty(HttpContextHolder.getInterceptorList())) {
|
||||
for (HandlerInterceptor handlerInterceptor : HttpContextHolder.getInterceptorList()) {
|
||||
if (!handlerInterceptor.preHandle(context, requestParser, route, targetMethod)) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
@@ -138,6 +143,14 @@ public class HttpRequestHandler {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
private void postHandle(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod) throws Exception {
|
||||
if (!CollectionUtil.isEmpty(HttpContextHolder.getInterceptorList())) {
|
||||
for (HandlerInterceptor handlerInterceptor : HttpContextHolder.getInterceptorList()) {
|
||||
handlerInterceptor.postHandle(context, requestParser, route, targetMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
|
||||
List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>();
|
||||
InterceptorRegistry interceptorRegistry = WebContextHolder.getInterceptorRegistry();
|
||||
|
||||
+13
-1
@@ -21,10 +21,12 @@
|
||||
*/
|
||||
package fun.asgc.neutrino.core.web.context;
|
||||
|
||||
import fun.asgc.neutrino.core.web.interceptor.InterceptorRegistry;
|
||||
import fun.asgc.neutrino.core.web.interceptor.HandlerInterceptor;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.http.FullHttpRequest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author: aoshiguchen
|
||||
@@ -34,11 +36,13 @@ public abstract class HttpContextHolder {
|
||||
private static final ThreadLocal<HttpRequestParser> httpRequestParserHolder = new ThreadLocal<>();
|
||||
private static ThreadLocal<FullHttpRequest> fullHttpRequestHolder = new ThreadLocal<>();
|
||||
private static ThreadLocal<ChannelHandlerContext> channelHandlerContextHolder = new ThreadLocal<>();
|
||||
private static ThreadLocal<List<HandlerInterceptor>> interceptorListHolder = new ThreadLocal<>();
|
||||
|
||||
public static void remove() {
|
||||
httpRequestParserHolder.remove();
|
||||
fullHttpRequestHolder.remove();
|
||||
channelHandlerContextHolder.remove();
|
||||
interceptorListHolder.remove();
|
||||
}
|
||||
|
||||
public static void setFullHttpRequest(FullHttpRequest request) {
|
||||
@@ -50,6 +54,14 @@ public abstract class HttpContextHolder {
|
||||
channelHandlerContextHolder.set(context);
|
||||
}
|
||||
|
||||
public static void setInterceptorList(List<HandlerInterceptor> interceptorList) {
|
||||
interceptorListHolder.set(interceptorList);
|
||||
}
|
||||
|
||||
public static List<HandlerInterceptor> getInterceptorList() {
|
||||
return interceptorListHolder.get();
|
||||
}
|
||||
|
||||
public static HttpRequestParser getHttpRequestParser() {
|
||||
return httpRequestParserHolder.get();
|
||||
}
|
||||
|
||||
-4
@@ -38,8 +38,4 @@ public interface HandlerInterceptor {
|
||||
|
||||
default void postHandle(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod) throws Exception {
|
||||
}
|
||||
|
||||
default void afterCompletion(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod, Throwable e) throws Exception {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-4
@@ -107,8 +107,4 @@ public class MappedInterceptor {
|
||||
public void postHandle(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod) throws Exception {
|
||||
this.interceptor.postHandle(context, requestParser, route, targetMethod);
|
||||
}
|
||||
|
||||
public void afterCompletion(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod, Throwable e) throws Exception {
|
||||
this.interceptor.afterCompletion(context, requestParser, route, targetMethod, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +59,4 @@ public class BaseAuthInterceptor implements HandlerInterceptor {
|
||||
public void postHandle(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod, Throwable e) throws Exception {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user