Search in sources :

Example 81 with ApplicationHandler

use of org.glassfish.jersey.server.ApplicationHandler in project jersey by jersey.

the class ApplicationFilterTest method testFilterCalledOn200.

@Test
public void testFilterCalledOn200() throws Exception {
    final SimpleFilter simpleFilter = new SimpleFilter();
    final ResourceConfig resourceConfig = new ResourceConfig(SimpleResource.class).register(simpleFilter);
    final ApplicationHandler application = new ApplicationHandler(resourceConfig);
    final ContainerResponse response = application.apply(RequestContextBuilder.from("/simple", "GET").build()).get();
    assertEquals(200, response.getStatus());
    Assert.assertTrue(simpleFilter.called);
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 82 with ApplicationHandler

use of org.glassfish.jersey.server.ApplicationHandler in project jersey by jersey.

the class ApplicationFilterTest method testFilterNotCalledOn404.

@Test
public void testFilterNotCalledOn404() throws Exception {
    // not found
    final SimpleFilter simpleFilter = new SimpleFilter();
    final ResourceConfig resourceConfig = new ResourceConfig(SimpleResource.class).register(simpleFilter);
    final ApplicationHandler application = new ApplicationHandler(resourceConfig);
    final ContainerResponse response = application.apply(RequestContextBuilder.from("/NOT-FOUND", "GET").build()).get();
    assertEquals(404, response.getStatus());
    Assert.assertFalse(simpleFilter.called);
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 83 with ApplicationHandler

use of org.glassfish.jersey.server.ApplicationHandler 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 84 with ApplicationHandler

use of org.glassfish.jersey.server.ApplicationHandler in project jersey by jersey.

the class ApplicationFilterTest method testSingleResponseFilter.

@Test
public void testSingleResponseFilter() throws Exception {
    final AtomicInteger called = new AtomicInteger(0);
    final List<ContainerResponseFilter> responseFilterList = new ArrayList<>();
    responseFilterList.add(new ContainerResponseFilter() {

        @Override
        public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) throws IOException {
            called.incrementAndGet();
        }
    });
    final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(responseFilterList, ContainerResponseFilter.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) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) Resource(org.glassfish.jersey.server.model.Resource) IOException(java.io.IOException) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 85 with ApplicationHandler

use of org.glassfish.jersey.server.ApplicationHandler 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

ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)162 Test (org.junit.Test)142 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)104 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)99 ApplicationInfoListener (org.glassfish.jersey.server.internal.monitoring.ApplicationInfoListener)17 MonitoringEventListener (org.glassfish.jersey.server.internal.monitoring.MonitoringEventListener)17 MBeanExposer (org.glassfish.jersey.server.internal.monitoring.jmx.MBeanExposer)17 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)12 Response (javax.ws.rs.core.Response)10 MediaType (javax.ws.rs.core.MediaType)8 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)7 MonitoringFeature (org.glassfish.jersey.server.internal.monitoring.MonitoringFeature)6 Resource (org.glassfish.jersey.server.model.Resource)6 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)5 ArrayList (java.util.ArrayList)4 Ignore (org.junit.Ignore)4 IOException (java.io.IOException)3 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)3 Inflector (org.glassfish.jersey.process.Inflector)3 Before (org.junit.Before)3