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());
}
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());
}
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());
}
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;
}
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);
}
Aggregations