Search in sources :

Example 11 with MapPropertiesDelegate

use of org.glassfish.jersey.internal.MapPropertiesDelegate in project jersey by jersey.

the class ContainerRequestTest method testAcceptableMediaTypes.

@Test
public void testAcceptableMediaTypes() throws URISyntaxException {
    ContainerRequest r = new ContainerRequest(URI.create("http://example.org/app"), URI.create("http://example.org/app/resource"), "GET", SECURITY_CONTEXT, new MapPropertiesDelegate());
    r.header(HttpHeaders.ACCEPT, "application/xml, text/plain");
    r.header(HttpHeaders.ACCEPT, "application/json");
    assertEquals(r.getAcceptableMediaTypes().size(), 3);
    assertTrue(r.getAcceptableMediaTypes().contains(MediaType.APPLICATION_XML_TYPE));
    assertTrue(r.getAcceptableMediaTypes().contains(MediaType.TEXT_PLAIN_TYPE));
    assertTrue(r.getAcceptableMediaTypes().contains(MediaType.APPLICATION_JSON_TYPE));
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) Test(org.junit.Test)

Example 12 with MapPropertiesDelegate

use of org.glassfish.jersey.internal.MapPropertiesDelegate in project jersey by jersey.

the class ContainerRequestTest method testPreconditionsNoneMatch.

@Test
public void testPreconditionsNoneMatch() {
    ContainerRequest r = new ContainerRequest(URI.create("http://example.org/app"), URI.create("http://example.org/app/resource"), "GET", SECURITY_CONTEXT, new MapPropertiesDelegate());
    r.header(HttpHeaders.IF_NONE_MATCH, "\"686897696a7c876b7e\"");
    assertEquals(r.evaluatePreconditions(new EntityTag("686897696a7c876b7e")).build().getStatus(), Response.Status.NOT_MODIFIED.getStatusCode());
    assertNull(r.evaluatePreconditions(new EntityTag("000000000000000000")));
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) EntityTag(javax.ws.rs.core.EntityTag) Test(org.junit.Test)

Example 13 with MapPropertiesDelegate

use of org.glassfish.jersey.internal.MapPropertiesDelegate in project jersey by jersey.

the class ContainerRequestTest method testPreconditionsModified.

@Test
public void testPreconditionsModified() throws ParseException {
    ContainerRequest r = new ContainerRequest(URI.create("http://example.org/app"), URI.create("http://example.org/app/resource"), "GET", SECURITY_CONTEXT, new MapPropertiesDelegate());
    r.header(HttpHeaders.IF_MODIFIED_SINCE, "Sat, 29 Oct 2011 19:43:31 GMT");
    SimpleDateFormat f = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
    Date date = f.parse("Sat, 29 Oct 2011 19:43:31 GMT");
    assertEquals(r.evaluatePreconditions(date).build().getStatus(), Response.Status.NOT_MODIFIED.getStatusCode());
    date = f.parse("Sat, 30 Oct 2011 19:43:31 GMT");
    assertNull(r.evaluatePreconditions(date));
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 14 with MapPropertiesDelegate

use of org.glassfish.jersey.internal.MapPropertiesDelegate in project jersey by jersey.

the class ContainerRequestTest method testAcceptableLanguages.

@Test
public void testAcceptableLanguages() throws URISyntaxException {
    ContainerRequest r = new ContainerRequest(URI.create("http://example.org/app"), URI.create("http://example.org/app/resource"), "GET", SECURITY_CONTEXT, new MapPropertiesDelegate());
    r.header(HttpHeaders.ACCEPT_LANGUAGE, "en-gb;q=0.8, en;q=0.7");
    r.header(HttpHeaders.ACCEPT_LANGUAGE, "de");
    assertEquals(r.getAcceptableLanguages().size(), 3);
    assertTrue(r.getAcceptableLanguages().contains(Locale.UK));
    assertTrue(r.getAcceptableLanguages().contains(Locale.ENGLISH));
    assertTrue(r.getAcceptableLanguages().contains(Locale.GERMAN));
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) Test(org.junit.Test)

Example 15 with MapPropertiesDelegate

use of org.glassfish.jersey.internal.MapPropertiesDelegate 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

MapPropertiesDelegate (org.glassfish.jersey.internal.MapPropertiesDelegate)16 Test (org.junit.Test)9 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)6 URI (java.net.URI)4 IOException (java.io.IOException)3 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)3 ContainerResponseWriter (org.glassfish.jersey.server.spi.ContainerResponseWriter)3 URISyntaxException (java.net.URISyntaxException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 EntityTag (javax.ws.rs.core.EntityTag)2 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)2 ContainerException (org.glassfish.jersey.server.ContainerException)2 HttpsExchange (com.sun.net.httpserver.HttpsExchange)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 Annotation (java.lang.annotation.Annotation)1 Principal (java.security.Principal)1 ServletException (javax.servlet.ServletException)1