Search in sources :

Example 26 with ContainerRequest

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

the class EncodingFilterTest method testNoEntity.

@Test
public void testNoEntity() throws IOException {
    EncodingFilter filter = initializeAndGetFilter();
    ContainerRequest request = RequestContextBuilder.from("/resource", "GET").header(HttpHeaders.ACCEPT_ENCODING, "gzip").build();
    ContainerResponse response = new ContainerResponse(request, Response.ok().build());
    filter.filter(request, response);
    assertNull(response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
    assertNull(response.getHeaders().getFirst(HttpHeaders.VARY));
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) Test(org.junit.Test)

Example 27 with ContainerRequest

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

the class PropertyValidationTest method assertApply.

private void assertApply(int responseStatus, ResourceConfig resourceConfig, URI uri) throws InterruptedException, ExecutionException {
    final ApplicationHandler applicationHandler = new ApplicationHandler(resourceConfig);
    final ContainerRequest requestContext = new ContainerRequest(uri, uri, "POST", null, new MapPropertiesDelegate());
    final ContainerResponse containerResponse = applicationHandler.apply(requestContext).get();
    assertEquals(responseStatus, containerResponse.getStatus());
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ContainerRequest(org.glassfish.jersey.server.ContainerRequest)

Example 28 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 29 with ContainerRequest

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

the class EncodingFilterTest method testEncoding.

private void testEncoding(String expected, String... accepted) throws IOException {
    EncodingFilter filter = initializeAndGetFilter();
    RequestContextBuilder builder = RequestContextBuilder.from("/resource", "GET");
    for (String a : accepted) {
        builder.header(HttpHeaders.ACCEPT_ENCODING, a);
    }
    ContainerRequest request = builder.build();
    ContainerResponse response = new ContainerResponse(request, Response.ok("OK!").build());
    filter.filter(request, response);
    if (response.getStatus() != 200) {
        throw new WebApplicationException(Response.status(response.getStatus()).build());
    }
    assertEquals(expected, response.getHeaderString(HttpHeaders.CONTENT_ENCODING));
    assertEquals(HttpHeaders.ACCEPT_ENCODING, response.getHeaderString(HttpHeaders.VARY));
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) WebApplicationException(javax.ws.rs.WebApplicationException) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) RequestContextBuilder(org.glassfish.jersey.server.RequestContextBuilder)

Example 30 with ContainerRequest

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

the class JdkHttpHandlerContainer method handle.

@Override
public void handle(final HttpExchange exchange) throws IOException {
    /**
         * This is a URI that contains the path, query and fragment components.
         */
    URI exchangeUri = exchange.getRequestURI();
    /**
         * The base path specified by the HTTP context of the HTTP handler. It
         * is in decoded form.
         */
    String decodedBasePath = exchange.getHttpContext().getPath();
    // Ensure that the base path ends with a '/'
    if (!decodedBasePath.endsWith("/")) {
        if (decodedBasePath.equals(exchangeUri.getPath())) {
            /**
                 * This is an edge case where the request path does not end in a
                 * '/' and is equal to the context path of the HTTP handler.
                 * Both the request path and base path need to end in a '/'
                 * Currently the request path is modified.
                 *
                 * TODO support redirection in accordance with resource configuration feature.
                 */
            exchangeUri = UriBuilder.fromUri(exchangeUri).path("/").build();
        }
        decodedBasePath += "/";
    }
    /*
         * The following is madness, there is no easy way to get the complete
         * URI of the HTTP request!!
         *
         * TODO this is missing the user information component, how can this be obtained?
         */
    final boolean isSecure = exchange instanceof HttpsExchange;
    final String scheme = isSecure ? "https" : "http";
    final URI baseUri = getBaseUri(exchange, decodedBasePath, scheme);
    final URI requestUri = getRequestUri(exchange, baseUri);
    final ResponseWriter responseWriter = new ResponseWriter(exchange);
    final ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, exchange.getRequestMethod(), getSecurityContext(exchange.getPrincipal(), isSecure), new MapPropertiesDelegate());
    requestContext.setEntityStream(exchange.getRequestBody());
    requestContext.getHeaders().putAll(exchange.getRequestHeaders());
    requestContext.setWriter(responseWriter);
    try {
        appHandler.handle(requestContext);
    } finally {
        // if the response was not committed yet by the JerseyApplication
        // then commit it and log warning
        responseWriter.closeAndLogWarning();
    }
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) HttpsExchange(com.sun.net.httpserver.HttpsExchange) ContainerResponseWriter(org.glassfish.jersey.server.spi.ContainerResponseWriter) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) URI(java.net.URI)

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