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