use of cn.taketoday.web.interceptor.HandlerInterceptor in project today-framework by TAKETODAY.
the class HandlerMethodRegistry method getInterceptors.
/**
* Get list of intercepters.
*
* @param controllerClass controller class
* @param action method
* @return List of {@link HandlerInterceptor} objects
*/
protected List<HandlerInterceptor> getInterceptors(Class<?> controllerClass, Method action) {
ArrayList<HandlerInterceptor> ret = new ArrayList<>();
Set<Interceptor> controllerInterceptors = AnnotatedElementUtils.getAllMergedAnnotations(controllerClass, Interceptor.class);
// get interceptor on class
if (CollectionUtils.isNotEmpty(controllerInterceptors)) {
for (Interceptor controllerInterceptor : controllerInterceptors) {
Collections.addAll(ret, getInterceptors(controllerInterceptor.value()));
}
}
// HandlerInterceptor on a method
Set<Interceptor> actionInterceptors = AnnotatedElementUtils.getAllMergedAnnotations(action, Interceptor.class);
if (CollectionUtils.isNotEmpty(actionInterceptors)) {
ApplicationContext beanFactory = obtainApplicationContext();
for (Interceptor actionInterceptor : actionInterceptors) {
Collections.addAll(ret, getInterceptors(actionInterceptor.value()));
// exclude interceptors
for (Class<? extends HandlerInterceptor> interceptor : actionInterceptor.exclude()) {
ret.remove(beanFactory.getBean(interceptor));
}
}
}
return ret;
}
use of cn.taketoday.web.interceptor.HandlerInterceptor in project today-framework by TAKETODAY.
the class HandlerCorsCustomizer method customize.
@Override
public Object customize(ActionMappingAnnotationHandler handler) {
// 预防已经设置
HandlerInterceptor[] interceptors = handler.getInterceptors();
if (ObjectUtils.isNotEmpty(interceptors)) {
for (HandlerInterceptor interceptor : interceptors) {
if (interceptor instanceof CorsHandlerInterceptor) {
return handler;
}
}
}
CrossOrigin methodCrossOrigin = handler.getMethod().getMethodAnnotation(CrossOrigin.class);
CrossOrigin classCrossOrigin = handler.getMethod().getDeclaringClassAnnotation(CrossOrigin.class);
if (classCrossOrigin == null && methodCrossOrigin == null) {
// 没有 @CrossOrigin 配置
return handler;
}
CorsConfiguration config = new CorsConfiguration();
updateCorsConfig(config, classCrossOrigin);
updateCorsConfig(config, methodCrossOrigin);
// 覆盖默认
config.applyPermitDefaultValues();
CorsHandlerInterceptor interceptor = new CorsHandlerInterceptor(config);
interceptor.setCorsProcessor(processor);
interceptor.setOrder(Ordered.HIGHEST_PRECEDENCE);
handler.addInterceptors(interceptor);
return handler;
}
use of cn.taketoday.web.interceptor.HandlerInterceptor in project today-infrastructure by TAKETODAY.
the class HandlerCorsCustomizer method customize.
@Override
public Object customize(ActionMappingAnnotationHandler handler) {
// 预防已经设置
HandlerInterceptor[] interceptors = handler.getInterceptors();
if (ObjectUtils.isNotEmpty(interceptors)) {
for (HandlerInterceptor interceptor : interceptors) {
if (interceptor instanceof CorsHandlerInterceptor) {
return handler;
}
}
}
CrossOrigin methodCrossOrigin = handler.getMethod().getMethodAnnotation(CrossOrigin.class);
CrossOrigin classCrossOrigin = handler.getMethod().getDeclaringClassAnnotation(CrossOrigin.class);
if (classCrossOrigin == null && methodCrossOrigin == null) {
// 没有 @CrossOrigin 配置
return handler;
}
CorsConfiguration config = new CorsConfiguration();
updateCorsConfig(config, classCrossOrigin);
updateCorsConfig(config, methodCrossOrigin);
// 覆盖默认
config.applyPermitDefaultValues();
CorsHandlerInterceptor interceptor = new CorsHandlerInterceptor(config);
interceptor.setCorsProcessor(processor);
interceptor.setOrder(Ordered.HIGHEST_PRECEDENCE);
handler.addInterceptors(interceptor);
return handler;
}
use of cn.taketoday.web.interceptor.HandlerInterceptor in project today-framework by TAKETODAY.
the class HandlerMethodRegistry method getInterceptors.
/**
* Get {@link HandlerInterceptor} objects
*
* @param interceptors
* {@link HandlerInterceptor} class
* @return Array of {@link HandlerInterceptor} objects
*/
public HandlerInterceptor[] getInterceptors(Class<? extends HandlerInterceptor>[] interceptors) {
if (ObjectUtils.isEmpty(interceptors)) {
return HandlerInterceptor.EMPTY_ARRAY;
}
int i = 0;
HandlerInterceptor[] ret = new HandlerInterceptor[interceptors.length];
BeanDefinitionRegistrar registrar = obtainApplicationContext().unwrap(BeanDefinitionRegistrar.class);
for (Class<? extends HandlerInterceptor> interceptor : interceptors) {
if (!registry.containsBeanDefinition(interceptor, true)) {
try {
registrar.registerBean(interceptor);
} catch (BeanDefinitionStoreException e) {
throw new ConfigurationException("Interceptor: [" + interceptor.getName() + "] register error", e);
}
}
HandlerInterceptor instance = this.beanFactory.getBean(interceptor);
Assert.state(instance != null, "Can't get target interceptor bean");
ret[i++] = instance;
}
return ret;
}
Aggregations