Search in sources :

Example 46 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 47 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 48 with ContainerRequestContext

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

the class TraceSupportTest method configure.

@Override
protected Application configure() {
    ResourceConfig config = new ResourceConfig(TracingResource.class);
    config.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.PAYLOAD_ANY));
    final Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH_PROGRAMMATIC);
    resourceBuilder.addMethod(TRACE.NAME).handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            if (request == null) {
                return Response.noContent().build();
            } else {
                return Response.ok(stringify((ContainerRequest) request), MediaType.TEXT_PLAIN).build();
            }
        }
    });
    return config.registerResources(resourceBuilder.build());
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) Resource(org.glassfish.jersey.server.model.Resource) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 49 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 50 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)

Aggregations

ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)81 Test (org.junit.Test)40 Response (javax.ws.rs.core.Response)32 UriInfo (javax.ws.rs.core.UriInfo)23 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)22 Test (org.junit.jupiter.api.Test)18 Resource (org.glassfish.jersey.server.model.Resource)15 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)14 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)12 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)10 File (java.io.File)8 ContainerResponseContext (javax.ws.rs.container.ContainerResponseContext)8 ResourceInfo (javax.ws.rs.container.ResourceInfo)8 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)8 IOException (java.io.IOException)7 URI (java.net.URI)6 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)6 FF4jAuthenticationFilter (org.ff4j.web.api.security.FF4jAuthenticationFilter)6 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)6 ApiKeyValidatorFilter (org.ff4j.web.api.filter.ApiKeyValidatorFilter)5