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