Search in sources :

Example 11 with ContainerRequestContext

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

the class ChildResourceTest method createApplication.

private ApplicationHandler createApplication() {
    final Resource.Builder rootBuilder = Resource.builder("root");
    rootBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() {

        @Override
        public String apply(ContainerRequestContext requestContext) {
            return "root-get";
        }
    });
    rootBuilder.addChildResource("child").addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() {

        @Override
        public String apply(ContainerRequestContext requestContext) {
            return "sub-get";
        }
    }).build();
    Resource.Builder anotherChildBuilder = Resource.builder("another-child");
    anotherChildBuilder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, String>() {

        @Override
        public String apply(ContainerRequestContext requestContext) {
            return "another-child-get";
        }
    });
    rootBuilder.addChildResource(anotherChildBuilder.build());
    Resource resource = rootBuilder.build();
    ResourceConfig resourceConfig = new ResourceConfig().registerResources(resource);
    return new ApplicationHandler(resourceConfig);
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Inflector(org.glassfish.jersey.process.Inflector) Resource(org.glassfish.jersey.server.model.Resource) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 12 with ContainerRequestContext

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

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

the class ProgrammaticResourceMethodsTest method testGet.

@Test
public void testGet() throws Exception {
    final ResourceConfig rc = new ResourceConfig();
    final Resource.Builder resourceBuilder = Resource.builder("test");
    resourceBuilder.addMethod("GET").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", "GET").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)

Example 14 with ContainerRequestContext

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

the class ProgrammaticResourceMethodsTest method testTwoBindersSamePath.

@Test
public void testTwoBindersSamePath() throws Exception {
    final ResourceConfig rc = new ResourceConfig();
    final Resource.Builder resourceBuilder = Resource.builder("/");
    final Resource.Builder childTest1Builder = resourceBuilder.addChildResource("test1");
    childTest1Builder.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            return Response.created(URI.create("/foo")).build();
        }
    });
    Inflector<ContainerRequestContext, Response> inflector1 = new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            return Response.accepted().build();
        }
    };
    final Resource.Builder childTest2Builder = resourceBuilder.addChildResource("test2");
    childTest2Builder.addMethod("GET").handledBy(inflector1);
    childTest2Builder.addMethod("HEAD").handledBy(inflector1);
    Inflector<ContainerRequestContext, Response> inflector2 = new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            return Response.status(203).build();
        }
    };
    childTest1Builder.addMethod("OPTIONS").handledBy(inflector2);
    childTest1Builder.addMethod("HEAD").handledBy(inflector2);
    final Resource resource = resourceBuilder.build();
    rc.registerResources(resource);
    final ApplicationHandler application = new ApplicationHandler(rc);
    checkReturnedStatusEquals(201, RequestContextBuilder.from("/test1", "GET").build(), application);
//        checkReturnedStatusEquals(203, Requests.from("/test1", "HEAD").build(), application);
//        checkReturnedStatusEquals(203, Requests.from("/test1", "OPTIONS").build(), application);
//        checkReturnedStatusEquals(202, Requests.from("/test2", "GET").build(), application);
//        checkReturnedStatusEquals(202, Requests.from("/test2", "HEAD").build(), application);
//        checkReturnedStatusEquals(202, Requests.from("/test2", "OPTIONS").build(), application);
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Inflector(org.glassfish.jersey.process.Inflector) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 15 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)

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