Search in sources :

Example 21 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.

the class ResponseReadAndBufferEntityTest method testBufferEntityReadsOriginalStreamTest.

@Test
public void testBufferEntityReadsOriginalStreamTest() throws Exception {
    final WebTarget target = target("response/corrupted");
    final CorruptableInputStream cis = new CorruptableInputStream();
    target.register(new ClientResponseFilter() {

        @Override
        public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) {
            responseContext.setEntityStream(cis);
        }
    });
    final Response response = target.request().buildGet().invoke();
    try {
        cis.setCorruptRead(true);
        response.bufferEntity();
        fail("ProcessingException expected.");
    } catch (ProcessingException pe) {
        // expected
        assertThat(pe.getCause(), instanceOf(IOException.class));
    }
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) WebTarget(javax.ws.rs.client.WebTarget) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) ProcessingException(javax.ws.rs.ProcessingException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 22 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.

the class ResponseReadAndBufferEntityTest method testReadBufferedEntityMultipleTimesFromStreamThatFailsToClose.

/**
     * This method tests behavior of input stream operations in case the underlying input stream throws an exception when closed.
     * Reproducer for JRFCAF-1344.
     * <p>
     * UC-2 : Read buffered entity multiple times and then try to close the context
     */
@Test
public void testReadBufferedEntityMultipleTimesFromStreamThatFailsToClose() throws Exception {
    final CorruptableInputStream entityStream = new CorruptableInputStream();
    final WebTarget target = target("response/corrupted");
    target.register(new ClientResponseFilter() {

        @Override
        public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) {
            responseContext.setEntityStream(entityStream);
        }
    });
    final Response response = target.request().buildGet().invoke();
    entityStream.setCorruptClose(true);
    response.bufferEntity();
    assertEquals("Close not invoked on underlying input stream.", 1, entityStream.getCloseCount());
    String entity;
    entity = response.readEntity(String.class, null);
    assertThat("Unexpected response.", entity.toString(), equalTo(Resource.ENTITY));
    entity = response.readEntity(String.class, null);
    assertThat("Unexpected response.", entity.toString(), equalTo(Resource.ENTITY));
    // Close should not fail and should be idempotent
    response.close();
    response.close();
    response.close();
    assertEquals("Close invoked too many times on underlying input stream.", 1, entityStream.getCloseCount());
    try {
        // UC-2.1 : Try to read a buffered entity from a closed context
        response.readEntity(String.class, null);
        fail("IllegalStateException expected when reading from a closed buffered context.");
    // UC-2.1 : END
    } catch (IllegalStateException ise) {
    // expected
    }
    // UC-2 : END
    entityStream.reset();
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) WebTarget(javax.ws.rs.client.WebTarget) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 23 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.

the class ResponseReadAndBufferEntityTest method testReadUnbufferedEntityFromStreamThatFailsToClose.

/**
     * This method tests behavior of input stream operations in case the underlying input stream throws an exception when closed.
     * Reproducer for JRFCAF-1344.
     * <p>
     * UC-1 : Read unbuffered entity and then try to close the context
     */
@Test
public void testReadUnbufferedEntityFromStreamThatFailsToClose() throws Exception {
    final CorruptableInputStream entityStream = new CorruptableInputStream();
    final WebTarget target = target("response/corrupted");
    target.register(new ClientResponseFilter() {

        @Override
        public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) {
            responseContext.setEntityStream(entityStream);
        }
    });
    final Response response = target.request().buildGet().invoke();
    entityStream.setCorruptClose(true);
    // Read entity should not fail - we silently consume the underlying IOException from closed input stream.
    final String entity = response.readEntity(String.class, null);
    assertThat("Unexpected response.", entity.toString(), equalTo(Resource.ENTITY));
    assertEquals("Close not invoked on underlying input stream.", 1, entityStream.getCloseCount());
    // Close should not fail and should be idempotent
    response.close();
    response.close();
    response.close();
    assertEquals("Close invoked too many times on underlying input stream.", 1, entityStream.getCloseCount());
    try {
        // UC-1.1 : Try to read an unbuffered entity from a closed context
        response.readEntity(String.class, null);
        fail("IllegalStateException expected when reading from a closed context.");
    // UC-1.1 : END
    } catch (IllegalStateException ise) {
    // expected
    }
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) WebTarget(javax.ws.rs.client.WebTarget) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 24 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.

the class BasicClientTest method abortingTarget.

protected WebTarget abortingTarget() {
    Client client = ClientBuilder.newClient();
    client.register(new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext ctx) throws IOException {
            Response r = Response.ok("aborted").build();
            ctx.abortWith(r);
        }
    });
    return client.target("http://any.web:888");
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) IOException(java.io.IOException) Client(javax.ws.rs.client.Client)

Example 25 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.

the class AbortResponseClientTest method testRequestAbort.

@Test
public void testRequestAbort() {
    final Date date = getDate();
    ClientRequestFilter outFilter = new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext context) throws IOException {
            NewCookie cookie1 = new NewCookie("cookie1", "cookie1");
            NewCookie cookie2 = new NewCookie("cookie2", "cookie2");
            final Response response = Response.ok().cookie(cookie1).cookie(cookie2).header("head1", "head1").header(HttpHeaders.DATE, date).header(HttpHeaders.ETAG, "\"123465\"").header(HttpHeaders.CONTENT_LANGUAGE, "language").header(HttpHeaders.LAST_MODIFIED, date).header(HttpHeaders.CONTENT_LENGTH, 99).type(MediaType.TEXT_HTML_TYPE).location(URI.create("www.oracle.com")).build();
            // abort the request
            context.abortWith(response);
        }
    };
    ClientResponseFilter inFilter = new ClientResponseFilter() {

        @Override
        public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
            Map<String, NewCookie> map = responseContext.getCookies();
            assertEquals("cookie1", map.get("cookie1").getValue());
            assertEquals("cookie2", map.get("cookie2").getValue());
            final MultivaluedMap<String, String> headers = responseContext.getHeaders();
            assertEquals("head1", headers.get("head1").get(0));
            assertEquals(date.getTime(), responseContext.getDate().getTime());
        }
    };
    WebTarget target = target().path("test");
    target.register(outFilter).register(inFilter);
    Invocation i = target.request().buildGet();
    Response r = i.invoke();
    assertEquals("head1", r.getHeaderString("head1"));
    assertEquals("cookie1", r.getCookies().get("cookie1").getValue());
    assertEquals("cookie2", r.getCookies().get("cookie2").getValue());
    assertEquals(date.getTime(), r.getDate().getTime());
    assertEquals("123465", r.getEntityTag().getValue());
    assertEquals("language", r.getLanguage().toString());
    assertEquals(date.getTime(), r.getLastModified().getTime());
    // Assert.assertEquals("uri", r.getLink("link")); TODO: not supported yet
    assertEquals("www.oracle.com", r.getLocation().toString());
    assertEquals(MediaType.TEXT_HTML_TYPE, r.getMediaType());
    assertEquals(99, r.getLength());
    assertEquals(200, r.getStatus());
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) WebTarget(javax.ws.rs.client.WebTarget) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) Date(java.util.Date) NewCookie(javax.ws.rs.core.NewCookie) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Aggregations

ClientRequestContext (javax.ws.rs.client.ClientRequestContext)30 Test (org.junit.Test)15 ClientResponseContext (javax.ws.rs.client.ClientResponseContext)11 Response (javax.ws.rs.core.Response)10 IOException (java.io.IOException)9 ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)8 ClientResponseFilter (javax.ws.rs.client.ClientResponseFilter)7 WebTarget (javax.ws.rs.client.WebTarget)7 Test (org.junit.jupiter.api.Test)7 URI (java.net.URI)6 Client (javax.ws.rs.client.Client)4 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)4 Message (org.apache.cxf.message.Message)4 MessageImpl (org.apache.cxf.message.MessageImpl)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 ProcessingException (javax.ws.rs.ProcessingException)3 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3 CreateSignatureInterceptor (org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor)3 VerifySignatureFilter (org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureFilter)3 MockAlgorithmProvider (org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider)3