use of org.glassfish.jersey.server.ContainerRequest 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 org.glassfish.jersey.server.ContainerRequest 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 org.glassfish.jersey.server.ContainerRequest 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 org.glassfish.jersey.server.ContainerRequest 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());
}
use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.
the class ValidatorTest method testDisableFailOnErrors.
@Test
public void testDisableFailOnErrors() throws ExecutionException, InterruptedException {
final ResourceConfig rc = new ResourceConfig(AmbiguousLocatorResource1.class, AmbiguousLocatorResource2.class, AmbiguousParameterResource.class, AmbiguousResource1.class, AmbiguousResource2.class, ConcreteGenericArrayResource.class, ConcreteParameterizedTypeResource.class, EmptyResource.class, GenericArrayResource.class, MethodAndLocatorResource.class, MethodAndLocatorResource2.class, MyBeanParam.class, ParameterizedTypeResource.class, PercentEncodedCaseSensitiveTest.class, PercentEncodedTest.class, ResourceAsProvider.class, ResourceMethodWithVoidReturnType.class, ResourceRoot.class, ResourceRootNotUnique.class, ResourceSubPathRoot.class, ResourceWithMultipleScopes.class, TestAmbiguousParams.class, TestAsyncGetRMReturningVoid.class, TestEmptyPathSegment.class, TestEntityParamOnSRL.class, TestGetRMConsumingEntity.class, TestGetRMConsumingFormParam.class, TestGetRMReturningVoid.class, TestGetSRMConsumingEntity.class, TestGetSRMConsumingFormParam.class, TestGetSRMReturningVoid.class, TestMoreThanOneEntity.class, TestMultipleHttpMethodDesignatorsRM.class, TestMultipleHttpMethodDesignatorsSRM.class, TestNonConflictingHttpMethodDelete.class, TestNonPublicRM.class, TestRelaxedProducesConsumesParserRules.class, TestRootResourceNonAmbigCtors.class, TestSRLReturningVoid.class, TwoLocatorsResource.class, TypeVariableResource.class, UniqueResource.class, // we should still be able to invoke a GET on this one.
TestDisableValidationFailOnErrorResource.class);
rc.property(ServerProperties.RESOURCE_VALIDATION_IGNORE_ERRORS, true);
ApplicationHandler ah = new ApplicationHandler(rc);
final ContainerRequest request = RequestContextBuilder.from("/test-disable-validation-fail-on-error", "GET").build();
ContainerResponse response = ah.apply(request).get();
assertEquals(200, response.getStatus());
assertEquals("PASSED", response.getEntity());
}
Aggregations