Search in sources :

Example 1 with HandlerInterceptor

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;
}
Also used : WebApplicationContext(cn.taketoday.web.WebApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) HandlerInterceptor(cn.taketoday.web.interceptor.HandlerInterceptor) ArrayList(java.util.ArrayList) Interceptor(cn.taketoday.web.annotation.Interceptor) HandlerInterceptor(cn.taketoday.web.interceptor.HandlerInterceptor)

Example 2 with HandlerInterceptor

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;
}
Also used : CrossOrigin(cn.taketoday.web.annotation.CrossOrigin) CorsHandlerInterceptor(cn.taketoday.web.interceptor.CorsHandlerInterceptor) CorsConfiguration(cn.taketoday.web.cors.CorsConfiguration) HandlerInterceptor(cn.taketoday.web.interceptor.HandlerInterceptor) CorsHandlerInterceptor(cn.taketoday.web.interceptor.CorsHandlerInterceptor)

Example 3 with HandlerInterceptor

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;
}
Also used : CrossOrigin(cn.taketoday.web.annotation.CrossOrigin) CorsHandlerInterceptor(cn.taketoday.web.interceptor.CorsHandlerInterceptor) CorsConfiguration(cn.taketoday.web.cors.CorsConfiguration) HandlerInterceptor(cn.taketoday.web.interceptor.HandlerInterceptor) CorsHandlerInterceptor(cn.taketoday.web.interceptor.CorsHandlerInterceptor)

Example 4 with HandlerInterceptor

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;
}
Also used : HandlerInterceptor(cn.taketoday.web.interceptor.HandlerInterceptor) BeanDefinitionStoreException(cn.taketoday.beans.factory.BeanDefinitionStoreException) ConfigurationException(cn.taketoday.core.ConfigurationException) BeanDefinitionRegistrar(cn.taketoday.context.loader.BeanDefinitionRegistrar)

Aggregations

HandlerInterceptor (cn.taketoday.web.interceptor.HandlerInterceptor)4 CrossOrigin (cn.taketoday.web.annotation.CrossOrigin)2 CorsConfiguration (cn.taketoday.web.cors.CorsConfiguration)2 CorsHandlerInterceptor (cn.taketoday.web.interceptor.CorsHandlerInterceptor)2 BeanDefinitionStoreException (cn.taketoday.beans.factory.BeanDefinitionStoreException)1 ApplicationContext (cn.taketoday.context.ApplicationContext)1 BeanDefinitionRegistrar (cn.taketoday.context.loader.BeanDefinitionRegistrar)1 ConfigurationException (cn.taketoday.core.ConfigurationException)1 WebApplicationContext (cn.taketoday.web.WebApplicationContext)1 Interceptor (cn.taketoday.web.annotation.Interceptor)1 ArrayList (java.util.ArrayList)1