Search in sources :

Example 31 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class MethodSelectingRouter method createHeadEnrichedRouter.

private Router createHeadEnrichedRouter() {
    return new Router() {

        @Override
        public Continuation apply(final RequestProcessingContext context) {
            final ContainerRequest request = context.request();
            if (HttpMethod.HEAD.equals(request.getMethod())) {
                request.setMethodWithoutException(HttpMethod.GET);
                context.push(new Function<ContainerResponse, ContainerResponse>() {

                    @Override
                    public ContainerResponse apply(final ContainerResponse responseContext) {
                        responseContext.getRequestContext().setMethodWithoutException(HttpMethod.HEAD);
                        return responseContext;
                    }
                });
            }
            return Continuation.of(context, getMethodRouter(context));
        }
    };
}
Also used : RequestProcessingContext(org.glassfish.jersey.server.internal.process.RequestProcessingContext) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ContainerRequest(org.glassfish.jersey.server.ContainerRequest)

Example 32 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class JsonWithPaddingInterceptor method getCallbackName.

/**
     * Returns a JavaScript callback name to wrap the JSON entity into. The callback name is determined from the {@link JSONP}
     * annotation.
     *
     * @param jsonp {@link JSONP} annotation to determine the callback name from.
     * @return a JavaScript callback name.
     */
private String getCallbackName(final JSONP jsonp) {
    String callback = jsonp.callback();
    if (!"".equals(jsonp.queryParam())) {
        final ContainerRequest containerRequest = containerRequestProvider.get();
        final UriInfo uriInfo = containerRequest.getUriInfo();
        final MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
        final List<String> queryParameter = queryParameters.get(jsonp.queryParam());
        callback = (queryParameter != null && !queryParameter.isEmpty()) ? queryParameter.get(0) : callback;
    }
    return callback;
}
Also used : ContainerRequest(org.glassfish.jersey.server.ContainerRequest) UriInfo(javax.ws.rs.core.UriInfo)

Example 33 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class ContextBasedInjectionTest method testAsyncApp.

@Test
public void testAsyncApp() throws InterruptedException, ExecutionException {
    ContainerRequest req = RequestContextBuilder.from(BASE_URI, URI.create(BASE_URI.getPath() + uriSuffix), "GET").build();
    Future<ContainerResponse> res = app.apply(req);
    assertEquals(expectedResponse, res.get().getEntity());
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) Test(org.junit.Test)

Example 34 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class MixedResourceConfigurationTest method testPutGet.

@Test
public void testPutGet() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(NameResource.class);
    final Resource.Builder resourceBuilder = Resource.builder("/name");
    resourceBuilder.addMethod("PUT").handledBy(new Inflector<ContainerRequestContext, Response>() {

        @Override
        public Response apply(ContainerRequestContext request) {
            name = ((ContainerRequest) request).readEntity(String.class);
            return Response.ok().build();
        }
    });
    resourceConfig.registerResources(resourceBuilder.build());
    final ApplicationHandler application = new ApplicationHandler(resourceConfig);
    final ContainerResponse response = application.apply(RequestContextBuilder.from("/name", "PUT").entity("Gaga").type(MediaType.TEXT_PLAIN).build()).get();
    assertEquals(200, response.getStatus());
    assertEquals("Gaga", application.apply(RequestContextBuilder.from("/name", "GET").accept(MediaType.TEXT_PLAIN).build()).get().getEntity());
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) 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)

Example 35 with ContainerRequest

use of org.glassfish.jersey.server.ContainerRequest in project jersey by jersey.

the class OptionsTest method testRequestNoType.

@Test
public void testRequestNoType() throws ExecutionException, InterruptedException {
    ApplicationHandler application = new ApplicationHandler(new ResourceConfig(WadlResource.class));
    final ContainerRequest request = RequestContextBuilder.from("/resource", "OPTIONS").build();
    final ContainerResponse response = application.apply(request).get();
    Assert.assertEquals(200, response.getStatus());
    final MediaType type = response.getMediaType();
    Assert.assertTrue(type.equals(MediaTypes.WADL_TYPE) || type.equals(MediaType.TEXT_HTML_TYPE) || type.equals(MediaType.TEXT_PLAIN));
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) MediaType(javax.ws.rs.core.MediaType) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Aggregations

ContainerRequest (org.glassfish.jersey.server.ContainerRequest)40 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