diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java index 169100dc..a69b4684 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java @@ -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 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 getInterceptorsForPath(String lookupPath) { List result = new ArrayList(); InterceptorRegistry interceptorRegistry = WebContextHolder.getInterceptorRegistry(); diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/context/HttpContextHolder.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/context/HttpContextHolder.java index be4ccc82..f962fc38 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/context/HttpContextHolder.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/context/HttpContextHolder.java @@ -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 httpRequestParserHolder = new ThreadLocal<>(); private static ThreadLocal fullHttpRequestHolder = new ThreadLocal<>(); private static ThreadLocal channelHandlerContextHolder = new ThreadLocal<>(); + private static ThreadLocal> 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 interceptorList) { + interceptorListHolder.set(interceptorList); + } + + public static List getInterceptorList() { + return interceptorListHolder.get(); + } + public static HttpRequestParser getHttpRequestParser() { return httpRequestParserHolder.get(); } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/HandlerInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/HandlerInterceptor.java index 1c12abbb..fd964964 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/HandlerInterceptor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/HandlerInterceptor.java @@ -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 { - } - } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/MappedInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/MappedInterceptor.java index 5703d735..7069e534 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/MappedInterceptor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/MappedInterceptor.java @@ -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); - } } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/BaseAuthInterceptor.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/BaseAuthInterceptor.java index 652d1d0a..3316270b 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/BaseAuthInterceptor.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/BaseAuthInterceptor.java @@ -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 { - - } }