Java Code Examples for javax.servlet.annotation.WebFilter#value()

The following examples show how to use javax.servlet.annotation.WebFilter#value() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterHolder.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 2
Source File: WebFilterInstaller.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
@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("%-25s %-8s %-4s %s   %s", Joiner.on(",").join(servletMapping ? servlets : patterns),
            WebUtils.getAsyncMarker(annotation), WebUtils.getContextMarkers(context),
            RenderUtils.renderClassLine(extType), name);

    if (WebUtils.isForMain(context)) {
        configure(environment.servlets(), instance, name, annotation);
    }
    if (WebUtils.isForAdmin(context)) {
        configure(environment.admin(), instance, name, annotation);
    }
}
 
Example 3
Source File: WebFilterInstaller.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
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());
}
 
Example 4
Source File: WebServletPlugin.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private List<FilterDefinition> detectFilters(Collection<Class<?>> filterClasses) {
    List<FilterDefinition> filterDefinitions = new ArrayList<>();
    for (Class<?> candidate : filterClasses) {
        if (Filter.class.isAssignableFrom(candidate)) {
            Class<? extends Filter> filterClass = (Class<? extends Filter>) candidate;
            WebFilter annotation = filterClass.getAnnotation(WebFilter.class);
            FilterDefinition filterDefinition = new FilterDefinition(
                    Strings.isNullOrEmpty(
                            annotation.filterName()) ? filterClass.getCanonicalName() : annotation.filterName(),
                    filterClass
            );
            filterDefinition.setAsyncSupported(annotation.asyncSupported());
            if (annotation.servletNames().length > 0) {
                filterDefinition.addServletMappings(
                        convert(annotation.dispatcherTypes(), false, annotation.servletNames()));
            }
            if (annotation.value().length > 0) {
                filterDefinition.addMappings(convert(annotation.dispatcherTypes(), false, annotation.value()));
            }
            if (annotation.urlPatterns().length > 0) {
                filterDefinition.addMappings(
                        convert(annotation.dispatcherTypes(), false, annotation.urlPatterns()));
            }
            filterDefinition.addInitParameters(convert(annotation.initParams()));
            filterDefinition.setPriority(priorityOf(filterClass));

            filterDefinitions.add(filterDefinition);
        }
    }

    return filterDefinitions;
}
 
Example 5
Source File: StartWebServer.java    From hammock with Apache License 2.0 5 votes vote down vote up
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);
}