Search in sources :

Example 1 with WebFilterInitializer

use of cn.taketoday.web.servlet.initializer.WebFilterInitializer in project today-framework by TAKETODAY.

the class WebServletApplicationLoader method configureFilterInitializers.

/**
 * Configure {@link Filter}
 *
 * @param applicationContext {@link ApplicationContext}
 * @param contextInitializers {@link WebApplicationInitializer}s
 */
protected void configureFilterInitializers(WebApplicationContext applicationContext, List<WebApplicationInitializer> contextInitializers) {
    List<Filter> filters = applicationContext.getAnnotatedBeans(WebFilter.class);
    for (Filter filter : filters) {
        Class<?> beanClass = filter.getClass();
        WebFilterInitializer<Filter> webFilterInitializer = new WebFilterInitializer<>(filter);
        WebFilter webFilter = beanClass.getAnnotation(WebFilter.class);
        Set<String> urlPatterns = new HashSet<>();
        Collections.addAll(urlPatterns, webFilter.value());
        Collections.addAll(urlPatterns, webFilter.urlPatterns());
        webFilterInitializer.addUrlMappings(StringUtils.toStringArray(urlPatterns));
        webFilterInitializer.addServletNames(webFilter.servletNames());
        webFilterInitializer.setAsyncSupported(webFilter.asyncSupported());
        for (WebInitParam initParam : webFilter.initParams()) {
            webFilterInitializer.addInitParameter(initParam.name(), initParam.value());
        }
        String name = webFilter.filterName();
        if (StringUtils.isEmpty(name)) {
            String displayName = webFilter.displayName();
            if (StringUtils.isEmpty(displayName)) {
                name = BeanDefinitionBuilder.defaultBeanName(beanClass);
            } else {
                name = displayName;
            }
        }
        webFilterInitializer.setName(name);
        webFilterInitializer.setDispatcherTypes(webFilter.dispatcherTypes());
        contextInitializers.add(webFilterInitializer);
    }
}
Also used : WebFilter(jakarta.servlet.annotation.WebFilter) Filter(jakarta.servlet.Filter) WebFilter(jakarta.servlet.annotation.WebFilter) FileFilter(java.io.FileFilter) WebInitParam(jakarta.servlet.annotation.WebInitParam) WebFilterInitializer(cn.taketoday.web.servlet.initializer.WebFilterInitializer) HashSet(java.util.HashSet)

Aggregations

WebFilterInitializer (cn.taketoday.web.servlet.initializer.WebFilterInitializer)1 Filter (jakarta.servlet.Filter)1 WebFilter (jakarta.servlet.annotation.WebFilter)1 WebInitParam (jakarta.servlet.annotation.WebInitParam)1 FileFilter (java.io.FileFilter)1 HashSet (java.util.HashSet)1