Search in sources :

Example 1 with ContainerRequestFilter

use of javax.ws.rs.container.ContainerRequestFilter in project dropwizard by dropwizard.

the class PolymorphicAuthDynamicFeature method configure.

@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
    final AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod());
    final Annotation[][] parameterAnnotations = am.getParameterAnnotations();
    final Class<?>[] parameterTypes = am.getParameterTypes();
    final Type[] parameterGenericTypes = am.getGenericParameterTypes();
    for (int i = 0; i < parameterAnnotations.length; i++) {
        final Class<?> type = parameterTypes[i];
        // If the parameter type is an Optional, extract its type
        // parameter. Otherwise, use the parameter type itself.
        final Type paramType = type == Optional.class ? ((ParameterizedType) parameterGenericTypes[i]).getActualTypeArguments()[0] : type;
        for (final Annotation annotation : parameterAnnotations[i]) {
            if (annotation instanceof Auth && authFilterMap.containsKey(paramType)) {
                if (type == Optional.class) {
                    final ContainerRequestFilter filter = authFilterMap.get(paramType);
                    context.register(new WebApplicationExceptionCatchingFilter(filter));
                    return;
                } else {
                    context.register(authFilterMap.get(type));
                    return;
                }
            }
        }
    }
}
Also used : AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod) Optional(java.util.Optional) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 2 with ContainerRequestFilter

use of javax.ws.rs.container.ContainerRequestFilter in project jersey by jersey.

the class ProviderBinderTest method getRequestFilter.

private ContainerRequestFilter getRequestFilter(InjectionManager injectionManager) {
    ContainerRequestFilter requestFilter = injectionManager.getInstance(ContainerRequestFilter.class, CustomAnnotationLiteral.INSTANCE);
    assertEquals(Child.class, requestFilter.getClass());
    return requestFilter;
}
Also used : ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 3 with ContainerRequestFilter

use of javax.ws.rs.container.ContainerRequestFilter in project jersey by jersey.

the class ApplicationHandler method filterNameBound.

/**
     * Takes collection of all filters/interceptors (either request/reader or response/writer)
     * and separates out all name-bound filters/interceptors, returns them as a separate MultivaluedMap,
     * mapping the name-bound annotation to the list of name-bound filters/interceptors. The same key values
     * are also added into the inverse map passed in {@code inverseNameBoundMap}.
     * <p/>
     * Note, the name-bound filters/interceptors are removed from the original filters/interceptors collection.
     * If non-null collection is passed in the postMatching parameter (applicable for filters only),
     * this method also removes all the global
     * postMatching filters from the original collection and adds them to the collection passed in the postMatching
     * parameter.
     *
     * @param all                     Collection of all filters to be processed.
     * @param preMatchingFilters      Collection into which pre-matching filters should be added.
     * @param componentBag            Component bag
     * @param applicationNameBindings Collection of name binding annotations attached to the JAX-RS application.
     * @param inverseNameBoundMap     Inverse name bound map into which the name bound providers should be inserted. The keys
     *                                are providers (filters, interceptor)
     * @return {@link MultivaluedMap} of all name-bound filters.
     */
private static <T> MultivaluedMap<Class<? extends Annotation>, RankedProvider<T>> filterNameBound(final Iterable<RankedProvider<T>> all, final Collection<RankedProvider<ContainerRequestFilter>> preMatchingFilters, final ComponentBag componentBag, final Collection<Class<? extends Annotation>> applicationNameBindings, final MultivaluedMap<RankedProvider<T>, Class<? extends Annotation>> inverseNameBoundMap) {
    final MultivaluedMap<Class<? extends Annotation>, RankedProvider<T>> result = new MultivaluedHashMap<>();
    for (final Iterator<RankedProvider<T>> it = all.iterator(); it.hasNext(); ) {
        final RankedProvider<T> provider = it.next();
        Class<?> providerClass = provider.getProvider().getClass();
        final Set<Type> contractTypes = provider.getContractTypes();
        if (contractTypes != null && !contractTypes.contains(providerClass)) {
            providerClass = ReflectionHelper.theMostSpecificTypeOf(contractTypes);
        }
        ContractProvider model = componentBag.getModel(providerClass);
        if (model == null) {
            // the provider was (most likely) bound in HK2 externally
            model = ComponentBag.modelFor(providerClass);
        }
        final boolean preMatching = providerClass.getAnnotation(PreMatching.class) != null;
        if (preMatching && preMatchingFilters != null) {
            it.remove();
            preMatchingFilters.add(new RankedProvider<>((ContainerRequestFilter) provider.getProvider(), model.getPriority(ContainerRequestFilter.class)));
        }
        boolean nameBound = model.isNameBound();
        if (nameBound && !applicationNameBindings.isEmpty() && applicationNameBindings.containsAll(model.getNameBindings())) {
            // override the name-bound flag
            nameBound = false;
        }
        if (nameBound) {
            // not application-bound
            if (!preMatching) {
                it.remove();
                for (final Class<? extends Annotation> binding : model.getNameBindings()) {
                    result.add(binding, provider);
                    inverseNameBoundMap.add(provider, binding);
                }
            } else {
                LOGGER.warning(LocalizationMessages.PREMATCHING_ALSO_NAME_BOUND(providerClass));
            }
        }
    }
    return result;
}
Also used : ContractProvider(org.glassfish.jersey.model.ContractProvider) Annotation(java.lang.annotation.Annotation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) RuntimeType(javax.ws.rs.RuntimeType) GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) RankedProvider(org.glassfish.jersey.model.internal.RankedProvider) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) PreMatching(javax.ws.rs.container.PreMatching)

Example 4 with ContainerRequestFilter

use of javax.ws.rs.container.ContainerRequestFilter in project jersey by jersey.

the class ApplicationFilterTest method testFilterExceptionHandling.

@Test
public void testFilterExceptionHandling() throws Exception {
    final List<ContainerRequestFilter> requestFilterList = new ArrayList<>();
    requestFilterList.add(new ExceptionFilter());
    final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(requestFilterList, ContainerRequestFilter.class));
    final Resource.Builder rb = Resource.builder("test");
    rb.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(final ContainerRequestContext request) {
            return Response.ok().build();
        }
    });
    resourceConfig.registerResources(rb.build());
    final ApplicationHandler application = new ApplicationHandler(resourceConfig);
    try {
        application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus();
        Assert.fail("should throw an exception");
    } catch (final Exception e) {
    // ok
    }
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ArrayList(java.util.ArrayList) Resource(org.glassfish.jersey.server.model.Resource) IOException(java.io.IOException) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 5 with ContainerRequestFilter

use of javax.ws.rs.container.ContainerRequestFilter in project jersey by jersey.

the class ApplicationFilterTest method testMultipleFiltersWithBindingPriority.

@Test
public void testMultipleFiltersWithBindingPriority() throws Exception {
    final Filter1 filter1 = new Filter1();
    final Filter10 filter10 = new Filter10();
    final Filter100 filter100 = new Filter100();
    filter1.setFilters(filter10, filter100);
    filter10.setFilters(filter1, filter100);
    filter100.setFilters(filter1, filter10);
    final List<ContainerRequestFilter> requestFilterList = new ArrayList<>();
    requestFilterList.add(filter100);
    requestFilterList.add(filter1);
    requestFilterList.add(filter10);
    final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(requestFilterList, ContainerRequestFilter.class));
    final Resource.Builder rb = Resource.builder("test");
    rb.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(final ContainerRequestContext request) {
            return Response.ok().build();
        }
    });
    resourceConfig.registerResources(rb.build());
    final ApplicationHandler application = new ApplicationHandler(resourceConfig);
    assertEquals(200, application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus());
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ArrayList(java.util.ArrayList) Resource(org.glassfish.jersey.server.model.Resource) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Aggregations

ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)8 ArrayList (java.util.ArrayList)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)3 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)3 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)3 Resource (org.glassfish.jersey.server.model.Resource)3 IOException (java.io.IOException)2 Annotation (java.lang.annotation.Annotation)2 Type (java.lang.reflect.Type)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Optional (java.util.Optional)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RuntimeType (javax.ws.rs.RuntimeType)1 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)1 PreMatching (javax.ws.rs.container.PreMatching)1 GenericType (javax.ws.rs.core.GenericType)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1