Search in sources :

Example 16 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext 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 17 with ContainerRequestContext

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

the class MixedResourceConfigurationTest method testPutGet.

@Test
public void testPutGet() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(NameResource.class);
    final Resource.Builder resourceBuilder = Resource.builder("/name");
    resourceBuilder.addMethod("PUT").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            name = ((ContainerRequest) request).readEntity(String.class);
            return Response.ok().build();
        }
    });
    resourceConfig.registerResources(resourceBuilder.build());
    final ApplicationHandler application = new ApplicationHandler(resourceConfig);
    final ContainerResponse response = application.apply(RequestContextBuilder.from("/name", "PUT").entity("Gaga").type(MediaType.TEXT_PLAIN).build()).get();
    assertEquals(200, response.getStatus());
    assertEquals("Gaga", application.apply(RequestContextBuilder.from("/name", "GET").accept(MediaType.TEXT_PLAIN).build()).get().getEntity());
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 18 with ContainerRequestContext

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

the class ResourcePathOverrideTest method testOverride.

@Test
public void testOverride() throws Exception {
    ResourceConfig resourceConfig = new ResourceConfig(HelloResource.class);
    Resource.Builder resourceBuilder = Resource.builder(HelloResource.class).path("hello2");
    resourceBuilder.addChildResource("world").addMethod("GET").produces("text/plain").handledBy(new Inflector<ContainerRequestContext, String>() {

        @Override
        public String apply(ContainerRequestContext request) {
            return "Hello World!";
        }
    });
    final Resource resource = resourceBuilder.build();
    resourceConfig.registerResources(resource);
    ApplicationHandler app = createApplication(resourceConfig);
    ContainerRequest request;
    request = RequestContextBuilder.from("/hello", "GET").build();
    assertEquals("Hello!", app.apply(request).get().getEntity());
    request = RequestContextBuilder.from("/hello2", "GET").build();
    assertEquals("Hello!", app.apply(request).get().getEntity());
    request = RequestContextBuilder.from("/hello2/world", "GET").build();
    final ContainerResponse response = app.apply(request).get();
    assertEquals(200, response.getStatus());
    assertEquals("Hello World!", response.getEntity());
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 19 with ContainerRequestContext

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

the class App method createApp.

/**
     * Create example application resource configuration.
     *
     * @return initialized resource configuration of the example application.
     */
public static ResourceConfig createApp() {
    MediaType[] jsonAndTextTypes = new MediaType[] { MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE };
    final ResourceConfig resourceConfig = new ResourceConfig(ClipboardResource.class, ClipboardDataProvider.ApplicationJson.class, ClipboardDataProvider.TextPlain.class);
    final Resource.Builder resourceBuilder = Resource.builder("echo");
    ResourceMethod.Builder rmBuilder = resourceBuilder.addMethod("POST");
    rmBuilder.consumes(jsonAndTextTypes).produces(jsonAndTextTypes).handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            ClipboardData data = (request != null) ? ((ContainerRequest) request).readEntity(ClipboardData.class) : null;
            return Response.ok(data).build();
        }
    });
    resourceConfig.registerResources(resourceBuilder.build());
    return resourceConfig;
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Resource(org.glassfish.jersey.server.model.Resource) Response(javax.ws.rs.core.Response) MediaType(javax.ws.rs.core.MediaType) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 20 with ContainerRequestContext

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

the class ProgrammaticResourceMethodsTest method testOptions.

@Test
public void testOptions() throws Exception {
    final ResourceConfig rc = new ResourceConfig();
    final Resource.Builder resourceBuilder = Resource.builder("test");
    resourceBuilder.addMethod("OPTIONS").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            return Response.ok().build();
        }
    });
    rc.registerResources(resourceBuilder.build());
    final ApplicationHandler application = new ApplicationHandler(rc);
    checkReturnedStatus(RequestContextBuilder.from("/test", "OPTIONS").build(), application);
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Aggregations

ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)23 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)22 Response (javax.ws.rs.core.Response)20 Resource (org.glassfish.jersey.server.model.Resource)15 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)12 Test (org.junit.Test)12 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)10 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)4 Inflector (org.glassfish.jersey.process.Inflector)4 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 InputStream (java.io.InputStream)1 ContainerResponseContext (javax.ws.rs.container.ContainerResponseContext)1 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)1 MediaType (javax.ws.rs.core.MediaType)1 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)1