public Registration(WebFilter annotation) { urlPatterns = new ArrayList<>(); dispatcherTypes = new ArrayList<>(); EnumSet<DispatcherType> dispatchers = EnumSet.noneOf(DispatcherType.class); dispatchers.addAll(Arrays.asList(annotation.dispatcherTypes())); if (annotation.value().length > 0) { addMappingForUrlPatterns(dispatchers, true, annotation.value()); } if (annotation.urlPatterns().length > 0) { addMappingForUrlPatterns(dispatchers, true, annotation.urlPatterns()); } asyncSupported = annotation.asyncSupported(); }
public MappedFilter toMappedFilter(Filter filter, int order) { WebFilter wfAnnotation = filter.getClass().getAnnotation(WebFilter.class); if (wfAnnotation == null) { throw new IllegalArgumentException( "Filter contains no @WebFilter annotation and can not be mapped directly. Wrap it in a MappedFilter instead."); } String name = wfAnnotation.filterName() != null && wfAnnotation.filterName().length() > 0 ? wfAnnotation.filterName() : null; Set<String> urlPatterns = new HashSet<>(asList(wfAnnotation.urlPatterns())); Map<String, String> initParams = new HashMap<>(); WebInitParam[] paramsArray = wfAnnotation.initParams(); if (paramsArray != null) { asList(paramsArray).forEach(p -> initParams.put(p.name(), p.value())); } return new MappedFilter(filter, urlPatterns, name, initParams, order); }
private void registerJeeComponents(ConfigurableListableBeanFactory beanFactory, ClassLoader cl, Set<BeanDefinition> candiates) { for (BeanDefinition bd : candiates) { try { Class<?> beanClass = ClassUtils.forName(bd.getBeanClassName(), cl); WebServlet webServlet = beanClass.getDeclaredAnnotation(WebServlet.class); WebFilter webFilter = beanClass.getDeclaredAnnotation(WebFilter.class); WebListener webListener = beanClass.getDeclaredAnnotation(WebListener.class); DefaultListableBeanFactory targetBeanFactory = (DefaultListableBeanFactory) beanFactory; if (webServlet != null) { createAndRegisterServletBean(targetBeanFactory, bd, beanClass, webServlet); } else if (webFilter != null) { createAndRegisterServletFilterBean(targetBeanFactory, bd, beanClass, webFilter); } else if (webListener != null) { createAndRegisterWebListenerBean(targetBeanFactory, bd, beanClass, webListener); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
/** * Method invoked for tracking filters that are registered via the OSGi service registry * * @param filter The filter to add * @return The name of the filter, if added */ private String addFilter(Filter filter) { Class<?> clz = filter.getClass(); WebFilter ann = clz.getAnnotation(WebFilter.class); if (ann == null) { return null; } String name = ann.filterName(); DynamicFRegistration reg = addFilter(name, filter); if (reg == null) { return null; } reg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, ann.urlPatterns()); reg.addMappingForServletNames(EnumSet.allOf(DispatcherType.class), false, ann.servletNames()); doParameters(reg, ann.initParams()); return name; }
@Override public void install(final Environment environment, final Filter instance) { final Class<? extends Filter> extType = FeatureUtils.getInstanceClass(instance); final WebFilter annotation = FeatureUtils.getAnnotation(extType, WebFilter.class); final String[] servlets = annotation.servletNames(); final String[] patterns = annotation.urlPatterns().length > 0 ? annotation.urlPatterns() : annotation.value(); Preconditions.checkArgument(servlets.length > 0 || patterns.length > 0, "Filter %s not specified servlet or pattern for mapping", extType.getName()); Preconditions.checkArgument(servlets.length == 0 || patterns.length == 0, "Filter %s specifies both servlets and patters, when only one allowed", extType.getName()); final boolean servletMapping = servlets.length > 0; final AdminContext context = FeatureUtils.getAnnotation(extType, AdminContext.class); final String name = WebUtils.getFilterName(annotation, extType); reporter.line("%-15s %-5s %-2s (%s) %s", Joiner.on(",").join(servletMapping ? servlets : patterns), WebUtils.getAsyncMarker(annotation), WebUtils.getContextMarkers(context), extType.getName(), name); if (WebUtils.isForMain(context)) { configure(environment.servlets(), instance, name, annotation); } if (WebUtils.isForAdmin(context)) { configure(environment.admin(), instance, name, annotation); } }
private void configure(final ServletEnvironment environment, final Filter filter, final String name, final WebFilter annotation) { final FilterRegistration.Dynamic mapping = environment.addFilter(name, filter); final EnumSet<DispatcherType> dispatcherTypes = EnumSet.copyOf(Arrays.asList(annotation.dispatcherTypes())); if (annotation.servletNames().length > 0) { mapping.addMappingForServletNames(dispatcherTypes, false, annotation.servletNames()); } else { final String[] urlPatterns = annotation.urlPatterns().length > 0 ? annotation.urlPatterns() : annotation.value(); mapping.addMappingForUrlPatterns(dispatcherTypes, false, urlPatterns); } if (annotation.initParams().length > 0) { for (WebInitParam param : annotation.initParams()) { mapping.setInitParameter(param.name(), param.value()); } } mapping.setAsyncSupported(annotation.asyncSupported()); }
private String readAnnotatedFilterName() { if (isAnnotated()) { WebFilter regAnnotation = filter.getClass().getAnnotation(WebFilter.class); if (!"".equals(regAnnotation.filterName().trim())) { return regAnnotation.filterName(); } else { return filter.getClass().getName(); } } else { return null; } }
private Map<String, String> readAnnotatedInitParams() { Map<String, String> initParams = new HashMap<>(); if (isAnnotated()) { WebFilter regAnnotation = filter.getClass().getAnnotation(WebFilter.class); for (WebInitParam param : regAnnotation.initParams()) { initParams.put(param.name(), param.value()); } } return initParams; }
private WebFilter getAnnotation() { if (isAnnotated()) { return filter.getClass().getAnnotation(WebFilter.class); } else { return null; } }
private void createAndRegisterServletFilterBean(DefaultListableBeanFactory beanFactory, BeanDefinition bd, Class<?> beanClass, WebFilter webFilter) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(FilterRegistrationBean.class); builder.addPropertyValue("filter", bd); builder.addPropertyValue("urlPatterns", new LinkedHashSet<>(Arrays.asList(webFilter.urlPatterns()))); String beanName = webFilter.filterName().isEmpty() ? StringUtils.uncapitalize(beanClass.getSimpleName()) : webFilter.filterName(); beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition()); }
private void processFilters() { Consumer<Class<? extends Filter>> c = filter -> { WebFilter webFilter = ClassUtils.getAnnotation(filter, WebFilter.class); if(webFilter != null) { FilterDescriptor filterDescriptor = new FilterDescriptor(webFilter.filterName(), webFilter.value(), mapUrls(webFilter.urlPatterns()), webFilter.dispatcherTypes(), webFilter.initParams(), webFilter.asyncSupported(), webFilter.servletNames(), filter); webServer.addFilter(filterDescriptor); } }; extension.processFilters(c); }
/** * Sets up filters that are annotated with the {@link WebFilter} annotation. * Every class in the classpath is searched for the annotation. */ @SuppressWarnings("unchecked") private void bindAnnotatedFilters() { String appsPackages = null; if (System.getProperties().containsKey(IO_JOYNR_APPS_PACKAGES)) { logger.info("Using property {} from system properties", IO_JOYNR_APPS_PACKAGES); appsPackages = System.getProperty(IO_JOYNR_APPS_PACKAGES); } else { Properties servletProperties = PropertyLoader.loadProperties("servlet.properties"); if (servletProperties.containsKey(IO_JOYNR_APPS_PACKAGES)) { appsPackages = servletProperties.getProperty(IO_JOYNR_APPS_PACKAGES); } } if (appsPackages != null) { String[] packageNames = appsPackages.split(";"); logger.info("Searching packages for @WebFilter annotation: {}", Arrays.toString(packageNames)); PackageNamesScanner scanner = new PackageNamesScanner(packageNames); AnnotationScannerListener sl = new AnnotationScannerListener(WebFilter.class); scanner.scan(sl); for (Class<?> webFilterAnnotatedClass : sl.getAnnotatedClasses()) { if (Filter.class.isAssignableFrom(webFilterAnnotatedClass)) { bind(webFilterAnnotatedClass).in(Singleton.class); filter("/*").through((Class<? extends Filter>) webFilterAnnotatedClass); logger.info("Adding filter {} for '/*'", webFilterAnnotatedClass.getName()); } } } }
WebFilterHandler() { super(WebFilter.class); }
public boolean isAnnotated() { return filter.getClass().isAnnotationPresent(WebFilter.class); }
@Override public boolean matches(final Class<?> type) { return FeatureUtils.is(type, Filter.class) && FeatureUtils.hasAnnotation(type, WebFilter.class); }
public UndertowComponent addFilter(WebFilter webFilter, Class<? extends Filter> clazz) { filters.put(webFilter,clazz); return this; }
public void findFilters(@Observes @WithAnnotations({WebFilter.class}) ProcessAnnotatedType<? extends Filter> pat) { filters.add(pat.getAnnotatedType().getJavaClass()); }
/** * When filter name not set in annotation, name generates as: . (dot) at the beginning to indicate * generated name, followed by lower-cased class name. If class ends with "filter" then it will be cut off. * For example, for class "MyCoolFilter" generated name will be ".mycool". * * @param filter filter annotation * @param type filter type * @return filter name or generated name if name not provided */ public static String getFilterName(final WebFilter filter, final Class<? extends Filter> type) { final String name = Strings.emptyToNull(filter.filterName()); return name != null ? name : generateName(type, "filter"); }
/** * @param annotation filter registration annotation * @return "async" string if filter support async and empty string otherwise */ public static String getAsyncMarker(final WebFilter annotation) { return getAsyncMarker(annotation.asyncSupported()); }