Search in sources :

Example 1 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 2 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 3 with ContainerRequestContext

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

the class App method create.

/**
     * Create example application resource configuration.
     *
     * @return initialized resource configuration of the example application.
     */
public static ResourceConfig create() {
    final ResourceConfig resourceConfig = new ResourceConfig(TracingResource.class);
    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(Stringifier.stringify((ContainerRequest) request), MediaType.TEXT_PLAIN).build();
            }
        }
    });
    return 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 4 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 5 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)

Aggregations

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