use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class JettyConnector method apply.
@Override
public Future<?> apply(final ClientRequest jerseyRequest, final AsyncConnectorCallback callback) {
final Request jettyRequest = translateRequest(jerseyRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest);
final ContentProvider entity = getStreamProvider(jerseyRequest);
if (entity != null) {
jettyRequest.content(entity);
}
final AtomicBoolean callbackInvoked = new AtomicBoolean(false);
final Throwable failure;
try {
final CompletableFuture<ClientResponse> responseFuture = new CompletableFuture<ClientResponse>().whenComplete((clientResponse, throwable) -> {
if (throwable != null && throwable instanceof CancellationException) {
// take care of future cancellation
jettyRequest.abort(throwable);
}
});
final AtomicReference<ClientResponse> jerseyResponse = new AtomicReference<>();
final ByteBufferInputStream entityStream = new ByteBufferInputStream();
jettyRequest.send(new Response.Listener.Adapter() {
@Override
public void onHeaders(final Response jettyResponse) {
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(), JettyConnector.this.getClass().getName());
if (responseFuture.isDone()) {
if (!callbackInvoked.compareAndSet(false, true)) {
return;
}
}
final ClientResponse response = translateResponse(jerseyRequest, jettyResponse, entityStream);
jerseyResponse.set(response);
callback.response(response);
}
@Override
public void onContent(final Response jettyResponse, final ByteBuffer content) {
try {
entityStream.put(content);
} catch (final InterruptedException ex) {
final ProcessingException pe = new ProcessingException(ex);
entityStream.closeQueue(pe);
// try to complete the future with an exception
responseFuture.completeExceptionally(pe);
Thread.currentThread().interrupt();
}
}
@Override
public void onComplete(final Result result) {
entityStream.closeQueue();
// try to complete the future with the response only once truly done
responseFuture.complete(jerseyResponse.get());
}
@Override
public void onFailure(final Response response, final Throwable t) {
entityStream.closeQueue(t);
// try to complete the future with an exception
responseFuture.completeExceptionally(t);
if (callbackInvoked.compareAndSet(false, true)) {
callback.failure(t);
}
}
});
processContent(jerseyRequest, entity);
return responseFuture;
} catch (final Throwable t) {
failure = t;
}
if (callbackInvoked.compareAndSet(false, true)) {
callback.failure(failure);
}
CompletableFuture<Object> future = new CompletableFuture<>();
future.completeExceptionally(failure);
return future;
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class JettyConnector method apply.
@Override
public ClientResponse apply(final ClientRequest jerseyRequest) throws ProcessingException {
final Request jettyRequest = translateRequest(jerseyRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest);
final ContentProvider entity = getBytesProvider(jerseyRequest);
if (entity != null) {
jettyRequest.content(entity);
}
try {
final ContentResponse jettyResponse = jettyRequest.send();
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(), JettyConnector.this.getClass().getName());
final javax.ws.rs.core.Response.StatusType status = jettyResponse.getReason() == null ? Statuses.from(jettyResponse.getStatus()) : Statuses.from(jettyResponse.getStatus(), jettyResponse.getReason());
final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest);
processResponseHeaders(jettyResponse.getHeaders(), jerseyResponse);
try {
jerseyResponse.setEntityStream(new HttpClientResponseInputStream(jettyResponse));
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, null, e);
}
return jerseyResponse;
} catch (final Exception e) {
throw new ProcessingException(e);
}
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class InMemoryConnector method apply.
@Override
public Future<?> apply(final ClientRequest request, final AsyncConnectorCallback callback) {
CompletableFuture<ClientResponse> future = new CompletableFuture<>();
try {
ClientResponse response = apply(request);
callback.response(response);
future.complete(response);
} catch (ProcessingException ex) {
future.completeExceptionally(ex);
} catch (Throwable t) {
callback.failure(t);
future.completeExceptionally(t);
}
return future;
}
use of org.glassfish.jersey.client.ClientResponse in project dropwizard by dropwizard.
the class DropwizardApacheConnectorTest method multiple_headers_with_the_same_name_are_processed_successfully.
@Test
public void multiple_headers_with_the_same_name_are_processed_successfully() throws Exception {
final CloseableHttpClient client = mock(CloseableHttpClient.class);
final DropwizardApacheConnector dropwizardApacheConnector = new DropwizardApacheConnector(client, null, false);
final Header[] apacheHeaders = { new BasicHeader("Set-Cookie", "test1"), new BasicHeader("Set-Cookie", "test2") };
final CloseableHttpResponse apacheResponse = mock(CloseableHttpResponse.class);
when(apacheResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
when(apacheResponse.getAllHeaders()).thenReturn(apacheHeaders);
when(client.execute(Mockito.any())).thenReturn(apacheResponse);
final ClientRequest jerseyRequest = mock(ClientRequest.class);
when(jerseyRequest.getUri()).thenReturn(URI.create("http://localhost"));
when(jerseyRequest.getMethod()).thenReturn("GET");
when(jerseyRequest.getHeaders()).thenReturn(new MultivaluedHashMap<>());
final ClientResponse jerseyResponse = dropwizardApacheConnector.apply(jerseyRequest);
assertThat(jerseyResponse.getStatus()).isEqualTo(apacheResponse.getStatusLine().getStatusCode());
}
use of org.glassfish.jersey.client.ClientResponse in project dropwizard by dropwizard.
the class DropwizardApacheConnector method apply.
/**
* {@inheritDoc}
*/
@Override
public ClientResponse apply(ClientRequest jerseyRequest) {
try {
final HttpUriRequest apacheRequest = buildApacheRequest(jerseyRequest);
final CloseableHttpResponse apacheResponse = client.execute(apacheRequest);
final StatusLine statusLine = apacheResponse.getStatusLine();
final Response.StatusType status = Statuses.from(statusLine.getStatusCode(), firstNonNull(statusLine.getReasonPhrase(), ""));
final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest);
for (Header header : apacheResponse.getAllHeaders()) {
final List<String> headerValues = jerseyResponse.getHeaders().get(header.getName());
if (headerValues == null) {
jerseyResponse.getHeaders().put(header.getName(), Lists.newArrayList(header.getValue()));
} else {
headerValues.add(header.getValue());
}
}
final HttpEntity httpEntity = apacheResponse.getEntity();
jerseyResponse.setEntityStream(httpEntity != null ? httpEntity.getContent() : new ByteArrayInputStream(new byte[0]));
return jerseyResponse;
} catch (Exception e) {
throw new ProcessingException(e);
}
}
Aggregations