Search in sources :

Example 6 with ContainerRequestFilter

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

the class ApplicationFilterTest method testSingleRequestFilter.

@Test
public void testSingleRequestFilter() throws Exception {
    final AtomicInteger called = new AtomicInteger(0);
    final List<ContainerRequestFilter> requestFilters = new ArrayList<>();
    requestFilters.add(new ContainerRequestFilter() {

        @Override
        public void filter(final ContainerRequestContext context) throws IOException {
            called.incrementAndGet();
        }
    });
    final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(requestFilters, 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());
    assertEquals(1, called.intValue());
}
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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 7 with ContainerRequestFilter

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

the class ContainerFilteringStage method apply.

@Override
@SuppressWarnings("unchecked")
public Continuation<RequestProcessingContext> apply(RequestProcessingContext context) {
    Iterable<ContainerRequestFilter> sortedRequestFilters;
    final boolean postMatching = responseFilters == null;
    final ContainerRequest request = context.request();
    final TracingLogger tracingLogger = TracingLogger.getInstance(request);
    if (postMatching) {
        // post-matching
        final ArrayList<Iterable<RankedProvider<ContainerRequestFilter>>> rankedProviders = new ArrayList<>(2);
        rankedProviders.add(requestFilters);
        rankedProviders.add(request.getRequestFilters());
        sortedRequestFilters = Providers.mergeAndSortRankedProviders(new RankedComparator<ContainerRequestFilter>(), rankedProviders);
        context.monitoringEventBuilder().setContainerRequestFilters(sortedRequestFilters);
        context.triggerEvent(RequestEvent.Type.REQUEST_MATCHED);
    } else {
        // pre-matching (response filter stage is pushed in pre-matching phase, so that if pre-matching filter
        // throws exception, response filters get still invoked)
        context.push(new ResponseFilterStage(context, responseFilters, tracingLogger));
        sortedRequestFilters = Providers.sortRankedProviders(new RankedComparator<ContainerRequestFilter>(), requestFilters);
    }
    final TracingLogger.Event summaryEvent = (postMatching ? ServerTraceEvent.REQUEST_FILTER_SUMMARY : ServerTraceEvent.PRE_MATCH_SUMMARY);
    final long timestamp = tracingLogger.timestamp(summaryEvent);
    int processedCount = 0;
    try {
        final TracingLogger.Event filterEvent = (postMatching ? ServerTraceEvent.REQUEST_FILTER : ServerTraceEvent.PRE_MATCH);
        for (ContainerRequestFilter filter : sortedRequestFilters) {
            final long filterTimestamp = tracingLogger.timestamp(filterEvent);
            try {
                filter.filter(request);
            } catch (Exception exception) {
                throw new MappableException(exception);
            } finally {
                processedCount++;
                tracingLogger.logDuration(filterEvent, filterTimestamp, filter);
            }
            final Response abortResponse = request.getAbortResponse();
            if (abortResponse != null) {
                // abort accepting & return response
                return Continuation.of(context, Stages.asStage(new Endpoint() {

                    @Override
                    public ContainerResponse apply(final RequestProcessingContext requestContext) {
                        return new ContainerResponse(requestContext.request(), abortResponse);
                    }
                }));
            }
        }
    } finally {
        if (postMatching) {
            context.triggerEvent(RequestEvent.Type.REQUEST_FILTERED);
        }
        tracingLogger.logDuration(summaryEvent, timestamp, processedCount);
    }
    return Continuation.of(context, getDefaultNext());
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) ArrayList(java.util.ArrayList) TracingLogger(org.glassfish.jersey.message.internal.TracingLogger) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint) MappableException(org.glassfish.jersey.server.internal.process.MappableException) RankedComparator(org.glassfish.jersey.model.internal.RankedComparator) Response(javax.ws.rs.core.Response) RequestProcessingContext(org.glassfish.jersey.server.internal.process.RequestProcessingContext) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 8 with ContainerRequestFilter

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

the class ProviderBinderTest method testCustomRegistration.

@Test
public void testCustomRegistration() {
    InjectionManager injectionManager = Injections.createInjectionManager();
    ProviderBinder providerBinder = new ProviderBinder(injectionManager);
    providerBinder.bindClasses(Child.class);
    providerBinder.bindClasses(NotFilterChild.class);
    ContainerRequestFilter requestFilter = getRequestFilter(injectionManager);
    ContainerRequestFilter requestFilter2 = getRequestFilter(injectionManager);
    assertEquals(requestFilter, requestFilter2);
    ContainerResponseFilter responseFilter = getResponseFilter(injectionManager);
    ContainerResponseFilter responseFilter2 = getResponseFilter(injectionManager);
    assertTrue(responseFilter == responseFilter2);
    assertTrue(responseFilter == requestFilter);
    // only one filter should be registered
    Collection<ContainerResponseFilter> filters = Providers.getCustomProviders(injectionManager, ContainerResponseFilter.class);
    assertEquals(1, filters.size());
    Child child = injectionManager.getInstance(Child.class);
    Child child2 = injectionManager.getInstance(Child.class);
    assertTrue(child != responseFilter);
    assertTrue(child == child2);
}
Also used : ProviderBinder(org.glassfish.jersey.internal.inject.ProviderBinder) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) 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