web拦截器相关逻辑调整.
This commit is contained in:
+3
-3
@@ -252,8 +252,8 @@ public class InterceptorFactory {
|
||||
if (classInterceptorListMap.containsKey(targetMethod.getDeclaringClass())) {
|
||||
interceptors.addAll(classInterceptorListMap.get(targetMethod.getDeclaringClass()));
|
||||
}
|
||||
addInterceptorByAnnotation(interceptors, targetMethod.getDeclaringClass().getAnnotation(Intercept.class));
|
||||
addInterceptorByAnnotation(interceptors, targetMethod.getAnnotation(Intercept.class));
|
||||
addInterceptorByAnnotation(interceptors, ClassUtil.getAnnotation(targetMethod.getDeclaringClass(), Intercept.class));
|
||||
addInterceptorByAnnotation(interceptors, ClassUtil.getAnnotation(targetMethod, Intercept.class));
|
||||
if (CollectionUtil.notEmpty(methodInterceptorListMap.get(targetMethod))) {
|
||||
interceptors.addAll(methodInterceptorListMap.get(targetMethod));
|
||||
}
|
||||
@@ -262,7 +262,7 @@ public class InterceptorFactory {
|
||||
List<Class<?>> interfaceList = ReflectUtil.getInterfaceAll(targetMethod.getDeclaringClass());
|
||||
if (CollectionUtil.notEmpty(interfaceList)) {
|
||||
for (Class<?> clazz : interfaceList) {
|
||||
addInterceptorByAnnotation(interceptors, clazz.getAnnotation(Intercept.class));
|
||||
addInterceptorByAnnotation(interceptors, ClassUtil.getAnnotation(clazz, Intercept.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,6 @@ public class ProxyCache {
|
||||
if (CollectionUtil.isEmpty(exceptionTypes)) {
|
||||
return false;
|
||||
}
|
||||
return exceptionTypes.contains(e.getClass());
|
||||
return exceptionTypes.contains(e.getClass()) || e instanceof RuntimeException;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import fun.asgc.neutrino.core.bean.SimpleBeanFactory;
|
||||
import fun.asgc.neutrino.core.web.HttpRequestHandler;
|
||||
import fun.asgc.neutrino.core.web.WebApplicationContext;
|
||||
import fun.asgc.neutrino.core.web.WebApplicationServer;
|
||||
import fun.asgc.neutrino.core.web.param.WebContextHolder;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -55,6 +56,7 @@ public class ExtensionServiceLoader implements ApplicationRunner {
|
||||
if (null == http || null == http.getEnable() || !http.getEnable()) {
|
||||
return;
|
||||
}
|
||||
applicationBeanFactory.registerBean(WebContextHolder.class);
|
||||
applicationBeanFactory.registerBean(WebApplicationContext.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,16 +79,15 @@ public class HttpRequestHandler {
|
||||
return;
|
||||
}
|
||||
if (HttpRouterType.METHOD == httpRouteResult.getType()) {
|
||||
|
||||
Object invokeResult = invoke(httpRouteResult.getInstance(), httpRouteResult.getMethod());
|
||||
String res = String.valueOf(invokeResult);
|
||||
if (null != invokeResult && !TypeUtil.isNormalBasicType(invokeResult.getClass())) {
|
||||
res = JSONObject.toJSONString(invokeResult);
|
||||
}
|
||||
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);
|
||||
return;
|
||||
Object invokeResult = invoke(httpRouteResult.getInstance(), httpRouteResult.getMethod());
|
||||
String res = String.valueOf(invokeResult);
|
||||
if (null != invokeResult && !TypeUtil.isNormalBasicType(invokeResult.getClass())) {
|
||||
res = JSONObject.toJSONString(invokeResult);
|
||||
}
|
||||
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);
|
||||
return;
|
||||
} else if(HttpRouterType.PAGE == httpRouteResult.getType()) {
|
||||
// 前端页面
|
||||
String mimeType = MimeType.getMimeType(MimeType.parseSuffix(httpRouteResult.getPageLocation()));
|
||||
|
||||
@@ -26,6 +26,8 @@ import fun.asgc.neutrino.core.annotation.*;
|
||||
import fun.asgc.neutrino.core.bean.SimpleBeanFactory;
|
||||
import fun.asgc.neutrino.core.context.ApplicationContext;
|
||||
import fun.asgc.neutrino.core.context.ApplicationRunner;
|
||||
import fun.asgc.neutrino.core.web.interceptor.ControllerGlobalInterceptor;
|
||||
import fun.asgc.neutrino.core.web.param.WebContextHolder;
|
||||
import fun.asgc.neutrino.core.web.router.DefaultHttpRouter;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
@@ -69,11 +71,10 @@ public class WebApplicationContext implements ApplicationRunner {
|
||||
Sets.newHashSet(
|
||||
DefaultHttpRouter.class,
|
||||
HttpRequestHandler.class,
|
||||
ControllerGlobalInterceptor.class,
|
||||
WebApplicationServer.class
|
||||
)
|
||||
);
|
||||
// this.webApplicationBeanFactory.registerBean(HttpRequestHandler.class);
|
||||
// this.webApplicationBeanFactory.registerBean(WebApplicationServer.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -59,7 +59,6 @@ public class WebApplicationServer implements ApplicationRunner {
|
||||
|
||||
@Init
|
||||
public void init() {
|
||||
WebContextHolder.init(applicationConfig.getHttp());
|
||||
this.bossGroup = new NioEventLoopGroup();
|
||||
this.workerGroup = new NioEventLoopGroup();
|
||||
this.serverBootstrap = new ServerBootstrap();
|
||||
|
||||
+3
-1
@@ -23,6 +23,7 @@ package fun.asgc.neutrino.core.web.annotation;
|
||||
|
||||
import fun.asgc.neutrino.core.annotation.Component;
|
||||
import fun.asgc.neutrino.core.annotation.NonIntercept;
|
||||
import fun.asgc.neutrino.core.aop.Intercept;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -36,7 +37,8 @@ import java.lang.annotation.Target;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Component
|
||||
@NonIntercept
|
||||
@Intercept(ignoreGlobal = true)
|
||||
//@NonIntercept
|
||||
public @interface RestController {
|
||||
|
||||
}
|
||||
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.web.interceptor;
|
||||
|
||||
import fun.asgc.neutrino.core.annotation.Component;
|
||||
import fun.asgc.neutrino.core.annotation.Init;
|
||||
import fun.asgc.neutrino.core.annotation.NonIntercept;
|
||||
import fun.asgc.neutrino.core.aop.Invocation;
|
||||
import fun.asgc.neutrino.core.aop.interceptor.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Controller全局拦截器
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/7/27
|
||||
*/
|
||||
@NonIntercept
|
||||
@Component
|
||||
public class ControllerGlobalInterceptor implements Interceptor {
|
||||
/**
|
||||
* 拦截器包装器
|
||||
*/
|
||||
private static final DefaultInterceptor interceptorWrapper = new DefaultInterceptor(ControllerGlobalInterceptor.class.getSimpleName());
|
||||
|
||||
public ControllerGlobalInterceptor() {
|
||||
|
||||
}
|
||||
|
||||
@Init
|
||||
public void init() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intercept(Invocation inv) throws Exception {
|
||||
interceptorWrapper.intercept(inv);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册过滤器
|
||||
* @param filter
|
||||
*/
|
||||
public static synchronized void registerFilter(Filter filter) {
|
||||
interceptorWrapper.registerFilter(filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册过滤器
|
||||
* @param filterList
|
||||
*/
|
||||
public static synchronized void registerFilter(List<Filter> filterList) {
|
||||
interceptorWrapper.registerFilter(filterList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册结果处理器
|
||||
* @param resultAdvice
|
||||
*/
|
||||
public static synchronized void registerResultAdvice(ResultAdvice resultAdvice) {
|
||||
interceptorWrapper.registerResultAdvice(resultAdvice);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册结果处理器
|
||||
* @param resultAdviceList
|
||||
*/
|
||||
public static synchronized void registerResultAdvice(List<ResultAdvice> resultAdviceList) {
|
||||
interceptorWrapper.registerResultAdvice(resultAdviceList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册异常处理器
|
||||
* @param exceptionHandler
|
||||
*/
|
||||
public static synchronized void registerExceptionHandler(ExceptionHandler exceptionHandler) {
|
||||
interceptorWrapper.registerExceptionHandler(exceptionHandler);
|
||||
}
|
||||
|
||||
public static synchronized void registerExceptionHandler(List<ExceptionHandler> exceptionHandlerList) {
|
||||
interceptorWrapper.registerExceptionHandler(exceptionHandlerList);
|
||||
}
|
||||
}
|
||||
@@ -21,27 +21,46 @@
|
||||
*/
|
||||
package fun.asgc.neutrino.core.web.param;
|
||||
|
||||
import fun.asgc.neutrino.core.annotation.Autowired;
|
||||
import fun.asgc.neutrino.core.annotation.Component;
|
||||
import fun.asgc.neutrino.core.annotation.Init;
|
||||
import fun.asgc.neutrino.core.annotation.NonIntercept;
|
||||
import fun.asgc.neutrino.core.bean.BeanWrapper;
|
||||
import fun.asgc.neutrino.core.bean.SimpleBeanFactory;
|
||||
import fun.asgc.neutrino.core.context.ApplicationConfig;
|
||||
import fun.asgc.neutrino.core.util.*;
|
||||
import fun.asgc.neutrino.core.web.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/7/26
|
||||
*/
|
||||
public abstract class WebContextHolder {
|
||||
@NonIntercept
|
||||
@Component
|
||||
public class WebContextHolder {
|
||||
private static ApplicationConfig.Http http;
|
||||
private static volatile String httpContextPath;
|
||||
private static volatile byte[] faviconBytes;
|
||||
private static volatile Long maxContentLength;
|
||||
private static volatile Integer port;
|
||||
private static List<BeanWrapper> controllerBeanWrapperList;
|
||||
@Autowired
|
||||
private ApplicationConfig applicationConfig;
|
||||
@Autowired
|
||||
private SimpleBeanFactory applicationBeanFactory;
|
||||
|
||||
public static void init(ApplicationConfig.Http http) {
|
||||
Assert.notNull(http, "http配置不能为空!");
|
||||
WebContextHolder.http = http;
|
||||
@Init
|
||||
public void init() {
|
||||
WebContextHolder.http = applicationConfig.getHttp();
|
||||
initHttpContextPath();
|
||||
initFavicon();
|
||||
initMaxContentLength();
|
||||
initControllerBeanWrapperList();
|
||||
port = http.getPort();
|
||||
}
|
||||
|
||||
@@ -92,4 +111,17 @@ public abstract class WebContextHolder {
|
||||
maxContentLength = NumberUtil.descriptionToSize(http.getMaxContentLengthDesc(), 64 * 1024);
|
||||
}
|
||||
}
|
||||
|
||||
private void initControllerBeanWrapperList() {
|
||||
List<BeanWrapper> beanWrapperList = applicationBeanFactory.beanWrapperList();
|
||||
if (CollectionUtil.isEmpty(beanWrapperList)) {
|
||||
return;
|
||||
}
|
||||
controllerBeanWrapperList = beanWrapperList.stream()
|
||||
.filter(beanWrapper -> beanWrapper.getType().isAnnotationPresent(RestController.class)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static List<BeanWrapper> getControllerBeanWrapperList() {
|
||||
return controllerBeanWrapperList;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-15
@@ -24,20 +24,15 @@ package fun.asgc.neutrino.core.web.router;
|
||||
import com.google.common.collect.Sets;
|
||||
import fun.asgc.neutrino.core.annotation.*;
|
||||
import fun.asgc.neutrino.core.bean.BeanWrapper;
|
||||
import fun.asgc.neutrino.core.bean.SimpleBeanFactory;
|
||||
import fun.asgc.neutrino.core.context.ApplicationConfig;
|
||||
import fun.asgc.neutrino.core.util.ArrayUtil;
|
||||
import fun.asgc.neutrino.core.util.CollectionUtil;
|
||||
import fun.asgc.neutrino.core.util.FileUtil;
|
||||
import fun.asgc.neutrino.core.util.ReflectUtil;
|
||||
import fun.asgc.neutrino.core.util.*;
|
||||
import fun.asgc.neutrino.core.web.HttpMethod;
|
||||
import fun.asgc.neutrino.core.web.annotation.GetMapping;
|
||||
import fun.asgc.neutrino.core.web.annotation.PostMapping;
|
||||
import fun.asgc.neutrino.core.web.annotation.RequestMapping;
|
||||
import fun.asgc.neutrino.core.web.annotation.RestController;
|
||||
import fun.asgc.neutrino.core.web.param.WebContextHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
@@ -61,8 +56,6 @@ public class DefaultHttpRouter implements HttpRouter {
|
||||
*/
|
||||
private Map<HttpRouteIdentity, HttpRouteInfo> routeCache = new ConcurrentHashMap<>(256);
|
||||
@Autowired
|
||||
private SimpleBeanFactory webApplicationBeanFactory;
|
||||
@Autowired
|
||||
private ApplicationConfig applicationConfig;
|
||||
|
||||
@Init
|
||||
@@ -78,12 +71,8 @@ public class DefaultHttpRouter implements HttpRouter {
|
||||
*/
|
||||
private void initMethodRoute() {
|
||||
log.debug("Http路由器初始化...");
|
||||
List<BeanWrapper> beanWrapperList = webApplicationBeanFactory.beanWrapperList();
|
||||
List<BeanWrapper> beanWrapperList = WebContextHolder.getControllerBeanWrapperList();
|
||||
beanWrapperList.forEach(beanWrapper -> {
|
||||
RestController restController = beanWrapper.getType().getAnnotation(RestController.class);
|
||||
if (null == restController) {
|
||||
return;
|
||||
}
|
||||
Set<HttpMethod> methods = null;
|
||||
Set<String> paths = null;
|
||||
if (beanWrapper.getType().isAnnotationPresent(GetMapping.class)) {
|
||||
@@ -263,7 +252,7 @@ public class DefaultHttpRouter implements HttpRouter {
|
||||
return new HttpRouteResult()
|
||||
.setType(httpRouteInfo.getType())
|
||||
.setMethod(httpRouteInfo.getMethod())
|
||||
.setInstance(webApplicationBeanFactory.getBean(httpRouteInfo.getBeanIdentity()));
|
||||
.setInstance(BeanManager.getBean(httpRouteInfo.getBeanIdentity()));
|
||||
}
|
||||
|
||||
@Destroy
|
||||
|
||||
@@ -23,6 +23,7 @@ package fun.asgc.neutrino.core.web.test1;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import fun.asgc.neutrino.core.annotation.Autowired;
|
||||
import fun.asgc.neutrino.core.annotation.NonIntercept;
|
||||
import fun.asgc.neutrino.core.web.annotation.*;
|
||||
import fun.asgc.neutrino.core.web.param.HttpContextHolder;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
Reference in New Issue
Block a user