Search in sources :

Example 21 with ContainerRequest

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

the class App method createProgrammaticClipboardApp.

/**
     * Create example application resource configuration.
     *
     * @return initialized resource configuration of the example application.
     */
public static ResourceConfig createProgrammaticClipboardApp() {
    final Resource.Builder resourceBuilder = Resource.builder(ROOT_PATH);
    final Clipboard clipboard = new Clipboard();
    resourceBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext data) {
            final String content = clipboard.content();
            if (content.isEmpty()) {
                return Response.noContent().build();
            }
            return Response.ok(content).build();
        }
    });
    resourceBuilder.addMethod("PUT").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext data) {
            if (data != null) {
                clipboard.setContent(((ContainerRequest) data).readEntity(String.class));
            }
            return Response.noContent().build();
        }
    });
    resourceBuilder.addMethod("POST").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext data) {
            String newContent = (data != null) ? clipboard.append(((ContainerRequest) data).readEntity(String.class)) : "";
            return Response.ok(newContent).build();
        }
    });
    resourceBuilder.addMethod("DELETE").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext data) {
            clipboard.clear();
            return Response.noContent().build();
        }
    });
    return new ResourceConfig().registerResources(resourceBuilder.build());
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Resource(org.glassfish.jersey.server.model.Resource) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 22 with ContainerRequest

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

the class AcceptAnnotatedReaderWriterTest method _test.

private void _test(ApplicationHandler app, String expected, String method, Object entity, String mediaType, String... accept) throws ExecutionException, InterruptedException {
    RequestContextBuilder requestContextBuilder = RequestContextBuilder.from("/", method);
    if (entity != null) {
        requestContextBuilder.entity(entity).type(mediaType);
    }
    ContainerRequest requestContext = requestContextBuilder.accept(accept).build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ContainerResponse response = app.apply(requestContext, baos).get();
    assertThat(response.getStatus(), equalTo(Response.Status.OK.getStatusCode()));
    assertThat(baos.toString(), equalTo(expected));
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestContextBuilder(org.glassfish.jersey.server.RequestContextBuilder)

Example 23 with ContainerRequest

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

the class OptionsTest method testOptions.

private ContainerResponse testOptions(MediaType requestType, ApplicationHandler application, String path) throws InterruptedException, ExecutionException {
    final ContainerRequest request = RequestContextBuilder.from(path, "OPTIONS").accept(requestType).build();
    final ContainerResponse response = application.apply(request).get();
    Assert.assertEquals(200, response.getStatus());
    final MediaType type = response.getMediaType();
    Assert.assertEquals(requestType, type);
    return response;
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) MediaType(javax.ws.rs.core.MediaType) ContainerRequest(org.glassfish.jersey.server.ContainerRequest)

Example 24 with ContainerRequest

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

the class OptionsTest method testNoHeadWildcard.

@Test
public void testNoHeadWildcard() throws ExecutionException, InterruptedException {
    final ResourceConfig resourceConfig = new ResourceConfig(ResourceWithoutGetMethod.class);
    resourceConfig.property(ServerProperties.WADL_FEATURE_DISABLE, true);
    ApplicationHandler application = new ApplicationHandler(resourceConfig);
    final ContainerRequest request = RequestContextBuilder.from("/no-get", "OPTIONS").accept(MediaType.MEDIA_TYPE_WILDCARD).build();
    final ContainerResponse response = application.apply(request).get();
    Assert.assertEquals(200, response.getStatus());
    final List<String> strings = response.getStringHeaders().get(HttpHeaders.ALLOW);
    for (String allow : strings) {
        Assert.assertFalse(allow.contains("HEAD"));
    }
}
Also used : 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 25 with ContainerRequest

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

the class AcceptWriterTest method _test.

private void _test(ApplicationHandler app, String expected, String method, Object entity, String mediaType, String... accept) throws ExecutionException, InterruptedException {
    RequestContextBuilder requestContextBuilder = RequestContextBuilder.from("/", method);
    if (entity != null) {
        requestContextBuilder.entity(entity).type(mediaType);
    }
    ContainerRequest requestContext = requestContextBuilder.accept(accept).build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    app.apply(requestContext, baos);
    assertEquals(expected, baos.toString());
}
Also used : ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestContextBuilder(org.glassfish.jersey.server.RequestContextBuilder)

Aggregations

ContainerRequest (org.glassfish.jersey.server.ContainerRequest)39 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)17 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)14 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)10 Response (javax.ws.rs.core.Response)10 IOException (java.io.IOException)8 Resource (org.glassfish.jersey.server.model.Resource)8 URI (java.net.URI)7 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)7 Test (org.junit.Test)7 MapPropertiesDelegate (org.glassfish.jersey.internal.MapPropertiesDelegate)6 MediaType (javax.ws.rs.core.MediaType)4 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)4 ContainerResponseWriter (org.glassfish.jersey.server.spi.ContainerResponseWriter)4 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 PropertiesDelegate (org.glassfish.jersey.internal.PropertiesDelegate)3 RequestContextBuilder (org.glassfish.jersey.server.RequestContextBuilder)3 Future (io.netty.util.concurrent.Future)2