Search in sources :

Example 1 with ClientResponseFilter

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

the class ResponseReadAndBufferEntityTest method testCloseUnreadResponseWithEntityStreamThatFailsToClose.

/**
     * 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-3 : Try to close the response - underlying exception should be reported.
     */
@Test
public void testCloseUnreadResponseWithEntityStreamThatFailsToClose() 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);
    try {
        response.close();
        fail("ProcessingException expected when closing the context and underlying stream throws an IOException.");
    } catch (ProcessingException pe) {
        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 2 with ClientResponseFilter

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

the class Jersey2878ITCase method configureClient.

@Override
protected void configureClient(ClientConfig config) {
    ClientResponseFilter trackInputStreams = new ClientResponseFilter() {

        @Override
        public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
            responseInputStreams.add(responseContext.getEntityStream());
        }
    };
    config.register(trackInputStreams);
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) ClientResponseContext(javax.ws.rs.client.ClientResponseContext)

Example 3 with ClientResponseFilter

use of javax.ws.rs.client.ClientResponseFilter 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 4 with ClientResponseFilter

use of javax.ws.rs.client.ClientResponseFilter 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 5 with ClientResponseFilter

use of javax.ws.rs.client.ClientResponseFilter 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)

Aggregations

ClientRequestContext (javax.ws.rs.client.ClientRequestContext)6 ClientResponseContext (javax.ws.rs.client.ClientResponseContext)6 ClientResponseFilter (javax.ws.rs.client.ClientResponseFilter)6 WebTarget (javax.ws.rs.client.WebTarget)5 Response (javax.ws.rs.core.Response)5 JerseyTest (org.glassfish.jersey.test.JerseyTest)5 Test (org.junit.Test)5 ProcessingException (javax.ws.rs.ProcessingException)2 Date (java.util.Date)1 ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)1 Invocation (javax.ws.rs.client.Invocation)1 NewCookie (javax.ws.rs.core.NewCookie)1