Search in sources :

Example 16 with ContainerRequest

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;
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) InputStream(java.io.InputStream) Resource(org.glassfish.jersey.server.model.Resource) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 17 with ContainerRequest

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());
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) Resource(org.glassfish.jersey.server.model.Resource) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 18 with ContainerRequest

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());
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) Resource(org.glassfish.jersey.server.model.Resource) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 19 with ContainerRequest

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());
}
Also used : Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Resource(org.glassfish.jersey.server.model.Resource) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 20 with ContainerRequest

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());
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Aggregations

ContainerRequest (org.glassfish.jersey.server.ContainerRequest)39 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)17 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)14 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)10 Response (javax.ws.rs.core.Response)10 IOException (java.io.IOException)8 Resource (org.glassfish.jersey.server.model.Resource)8 URI (java.net.URI)7 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)7 Test (org.junit.Test)7 MapPropertiesDelegate (org.glassfish.jersey.internal.MapPropertiesDelegate)6 MediaType (javax.ws.rs.core.MediaType)4 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)4 ContainerResponseWriter (org.glassfish.jersey.server.spi.ContainerResponseWriter)4 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 PropertiesDelegate (org.glassfish.jersey.internal.PropertiesDelegate)3 RequestContextBuilder (org.glassfish.jersey.server.RequestContextBuilder)3 Future (io.netty.util.concurrent.Future)2