Search in sources :

Example 6 with ContainerRequestContext

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());
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ArrayList(java.util.ArrayList) Resource(org.glassfish.jersey.server.model.Resource) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 7 with ContainerRequestContext

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;
}
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 8 with ContainerRequestContext

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());
}
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 9 with ContainerRequestContext

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());
}
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 10 with ContainerRequestContext

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

Aggregations

ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)23 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)22 Response (javax.ws.rs.core.Response)20 Resource (org.glassfish.jersey.server.model.Resource)15 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)12 Test (org.junit.Test)12 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)10 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)4 Inflector (org.glassfish.jersey.process.Inflector)4 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 InputStream (java.io.InputStream)1 ContainerResponseContext (javax.ws.rs.container.ContainerResponseContext)1 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)1 MediaType (javax.ws.rs.core.MediaType)1 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)1