Search in sources :

Example 6 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 7 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 8 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)

Example 9 with ApplicationHandler

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

the class JsonProcessingAutoDiscoverableServerTest method _test.

private void _test(final String response, final Boolean globalDisable, final Boolean serverDisable) throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(Resource.class, Filter.class);
    if (globalDisable != null) {
        resourceConfig.property(CommonProperties.JSON_PROCESSING_FEATURE_DISABLE, globalDisable);
    }
    if (serverDisable != null) {
        resourceConfig.property(ServerProperties.JSON_PROCESSING_FEATURE_DISABLE, serverDisable);
    }
    final ApplicationHandler app = new ApplicationHandler(resourceConfig);
    final URI baseUri = URI.create("/");
    assertEquals(response, app.apply(new ContainerRequest(baseUri, baseUri, "GET", null, new MapPropertiesDelegate())).get().getEntity());
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) URI(java.net.URI)

Example 10 with ApplicationHandler

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

the class SubResourceProgrammaticTest method testComplex.

@Test
public void testComplex() throws ExecutionException, InterruptedException {
    ApplicationHandler handler = new ApplicationHandler(new ResourceConfig(RootResource.class));
    int singletonCounter = 0;
    _test(handler, "/root/complex/", "inflector");
    _test(handler, "/root/complex/singleton", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex/singleton/iteration", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex/singleton/iteration/iteration", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex/singleton/iteration-class", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex/singleton/iteration-instance", "singleton:0");
    _test(handler, "/root/complex/standard", "standard:0");
    _test(handler, "/root/complex/standard/iteration", "standard:0");
    _test(handler, "/root/complex/singleton-method/", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex/singleton/iteration/iteration-standard", "standard:0");
    _test(handler, "/root/complex/standard/iteration/iteration-singleton", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex/standard/iteration/iteration-singleton/iteration/iteration-standard", "standard:0");
    _test(handler, "/root/complex/standard/iteration/iteration-singleton/iteration/iteration-standard/iteration", "standard:0");
    _test(handler, "/root/complex/standard/iteration/iteration-singleton/iteration/iteration-standard/iteration" + "/iteration-singleton/iteration-class", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex/standard/iteration/iteration-singleton/iteration/iteration-standard/iteration" + "/iteration-singleton/iteration-class/iteration-instance", "singleton:0");
    _test(handler, "/root/complex/standard/iteration/iteration-singleton", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex/singleton-method/", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex/singleton", "singleton:" + singletonCounter++);
    _test(handler, "/root/complex", "inflector");
}
Also used : 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