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));
}
}
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();
}
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
}
}
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");
}
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());
}
Aggregations