use of javax.ws.rs.container.ContainerRequestContext in project jersey by jersey.
the class ApplicationFilterTest method testMultipleFiltersWithBindingPriority.
@Test
public void testMultipleFiltersWithBindingPriority() throws Exception {
final Filter1 filter1 = new Filter1();
final Filter10 filter10 = new Filter10();
final Filter100 filter100 = new Filter100();
filter1.setFilters(filter10, filter100);
filter10.setFilters(filter1, filter100);
filter100.setFilters(filter1, filter10);
final List<ContainerRequestFilter> requestFilterList = new ArrayList<>();
requestFilterList.add(filter100);
requestFilterList.add(filter1);
requestFilterList.add(filter10);
final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(requestFilterList, 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());
}
use of javax.ws.rs.container.ContainerRequestContext in project jersey by jersey.
the class AbstractJsonTest method configureJaxrsApplication.
/**
* Creates and configures a JAX-RS {@link Application} for given {@link JsonTestSetup}. The {@link Application} will
* contain one resource that can be accessed via {@code POST} method at {@code <jsonProviderName>/<entityClassName>} path.
* The resource also checks whether is the incoming JSON same as the one stored in a appropriate file.
*
* @param jsonTestSetup configuration to create a JAX-RS {@link Application} for.
* @return an {@link Application} instance.
*/
private static Application configureJaxrsApplication(final JsonTestSetup jsonTestSetup) {
final Resource.Builder resourceBuilder = Resource.builder();
final String providerName = getProviderPathPart(jsonTestSetup);
final String testName = getEntityPathPart(jsonTestSetup);
resourceBuilder.path("/" + providerName + "/" + testName).addMethod("POST").consumes(MediaType.APPLICATION_JSON_TYPE).produces(MediaType.APPLICATION_JSON_TYPE).handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(final ContainerRequestContext containerRequestContext) {
final ContainerRequest containerRequest = (ContainerRequest) containerRequestContext;
// Check if the JSON is the same as in the previous version.
containerRequest.bufferEntity();
try {
String json = JsonTestHelper.getResourceAsString(PKG_NAME, providerName + "_" + testName + (moxyJaxbProvider() || runningOnJdk7AndLater() ? "_MOXy" : "") + ".json").trim();
final InputStream entityStream = containerRequest.getEntityStream();
String retrievedJson = JsonTestHelper.getEntityAsString(entityStream).trim();
entityStream.reset();
// JAXB-RI and MOXy generate namespace prefixes differently - unify them (ns1/ns2 into ns0)
if (jsonTestSetup.getJsonProvider() instanceof JsonTestProvider.JettisonBadgerfishJsonTestProvider) {
if (retrievedJson.contains("\"ns1\"")) {
json = json.replace("ns1", "ns0");
retrievedJson = retrievedJson.replace("ns1", "ns0");
} else if (retrievedJson.contains("\"ns2\"")) {
json = json.replace("ns2", "ns0");
retrievedJson = retrievedJson.replace("ns2", "ns0");
}
}
if (!json.equals(retrievedJson)) {
LOGGER.log(Level.SEVERE, "Expected: " + json);
LOGGER.log(Level.SEVERE, "Actual: " + retrievedJson);
return Response.ok("{\"error\":\"JSON values doesn't match.\"}").build();
}
} catch (final IOException e) {
return Response.ok("{\"error\":\"Cannot find original JSON file.\"}").build();
}
final Object testBean = containerRequest.readEntity(jsonTestSetup.getEntityClass());
return Response.ok(testBean).build();
}
});
final ResourceConfig resourceConfig = new ResourceConfig().registerResources(resourceBuilder.build()).register(jsonTestSetup.getJsonProvider().getFeature());
resourceConfig.registerInstances(getJaxbContextResolver(jsonTestSetup));
if (jsonTestSetup.getProviders() != null) {
resourceConfig.registerInstances(jsonTestSetup.getProviders());
}
return resourceConfig;
}
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());
}
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());
}
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());
}
Aggregations