Search in sources :

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

Example 52 with ContainerRequestContext

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

the class ProgrammaticResourceMethodsTest method testHead.

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

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

the class ProgrammaticResourceMethodsTest method testMultiple.

@Test
public void testMultiple() throws Exception {
    Inflector<ContainerRequestContext, Response> inflector = new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            return Response.ok().build();
        }
    };
    final ResourceConfig rc = new ResourceConfig();
    final Resource.Builder resourceBuilder = Resource.builder("test");
    resourceBuilder.addMethod("GET").handledBy(inflector);
    resourceBuilder.addMethod("OPTIONS").handledBy(inflector);
    resourceBuilder.addMethod("HEAD").handledBy(inflector);
    rc.registerResources(resourceBuilder.build());
    final ApplicationHandler application = new ApplicationHandler(rc);
    checkReturnedStatus(RequestContextBuilder.from("/test", "GET").build(), application);
    checkReturnedStatus(RequestContextBuilder.from("/test", "HEAD").build(), application);
    checkReturnedStatus(RequestContextBuilder.from("/test", "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 54 with ContainerRequestContext

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

the class SingletonResourceTest method configure.

@Override
protected ResourceConfig configure() {
    final ResourceConfig resourceConfig = new ResourceConfig(SingletonResource.class, ChildInheritsParentAnnotation.class, ChildImplementsInterfaceAnnotation.class, TestResource.class, RequestScopeResource.class, PerLookupScopeResource.class, SingletonScopeResource.class);
    final Resource.Builder resourceBuilder1 = Resource.builder();
    resourceBuilder1.name("resource-programmatic/instance/").path("programmatic/instance/").addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {

        private int counter = 1;

        @Override
        public Response apply(ContainerRequestContext data) {
            return Response.ok("prg-instance:" + counter++).build();
        }
    });
    resourceConfig.registerResources(resourceBuilder1.build());
    final Resource.Builder resourceBuilder2 = Resource.builder();
    resourceBuilder2.name("resource-programmatic/singleton/").path("programmatic/singleton/").addMethod("GET").handledBy(SingletonProgrammatic.class);
    resourceConfig.registerResources(resourceBuilder2.build());
    final Resource.Builder resourceBuilder3 = Resource.builder();
    resourceBuilder3.name("resource-programmatic/reused-singleton/").path("programmatic/reused-singleton/").addMethod("GET").handledBy(SubResourceSingleton.class);
    resourceConfig.registerResources(resourceBuilder3.build());
    final Resource.Builder resourceBuilder4 = Resource.builder();
    resourceBuilder4.name("resource-programmatic/not-singleton/").path("programmatic/not-singleton/").addMethod("GET").handledBy(NotSingletonProgrammatic.class);
    resourceConfig.registerResources(resourceBuilder4.build());
    return resourceConfig;
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Resource(org.glassfish.jersey.server.model.Resource) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 55 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project nifi by apache.

the class TestRedirectResourceFilter method testUnmatched.

@Test
public void testUnmatched() throws Exception {
    String path = "unmatched";
    String baseUri = "http://example.com:8080/nifi-api/";
    UriInfo uriInfo = mock(UriInfo.class);
    when(uriInfo.getPath()).thenReturn(path);
    when(uriInfo.getBaseUri()).thenReturn(new URI(baseUri));
    when(uriInfo.getRequestUri()).thenReturn(new URI(baseUri + path));
    ContainerRequestContext request = mock(ContainerRequestContext.class);
    when(request.getUriInfo()).thenReturn(uriInfo);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            fail("setUris shouldn't be called");
            return null;
        }
    }).when(request).setRequestUri(any(URI.class), any(URI.class));
    RedirectResourceFilter filter = new RedirectResourceFilter();
    filter.filter(request);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) URI(java.net.URI) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

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