use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class ApacheConnector method apply.
@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
final HttpUriRequest request = getUriHttpRequest(clientRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request);
try {
final CloseableHttpResponse response;
final HttpClientContext context = HttpClientContext.create();
if (preemptiveBasicAuth) {
final AuthCache authCache = new BasicAuthCache();
final BasicScheme basicScheme = new BasicScheme();
authCache.put(getHost(request), basicScheme);
context.setAuthCache(authCache);
}
response = client.execute(getHost(request), request, context);
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());
final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null ? Statuses.from(response.getStatusLine().getStatusCode()) : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
final ClientResponse responseContext = new ClientResponse(status, clientRequest);
final List<URI> redirectLocations = context.getRedirectLocations();
if (redirectLocations != null && !redirectLocations.isEmpty()) {
responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1));
}
final Header[] respHeaders = response.getAllHeaders();
final MultivaluedMap<String, String> headers = responseContext.getHeaders();
for (final Header header : respHeaders) {
final String headerName = header.getName();
List<String> list = headers.get(headerName);
if (list == null) {
list = new ArrayList<>();
}
list.add(header.getValue());
headers.put(headerName, list);
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
if (headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
}
final Header contentEncoding = entity.getContentEncoding();
if (headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) {
headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue());
}
}
try {
responseContext.setEntityStream(new HttpClientResponseInputStream(getInputStream(response)));
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, null, e);
}
return responseContext;
} catch (final Exception e) {
throw new ProcessingException(e);
}
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class JdkConnector method send.
private void send(final ClientRequest request, final HttpRequest httpRequest, final AsyncConnectorCallback callback) {
translateHeaders(request, httpRequest);
final RedirectHandler redirectHandler = new RedirectHandler(httpConnectionPool, httpRequest, connectorConfiguration);
httpConnectionPool.send(httpRequest, new CompletionHandler<HttpResponse>() {
@Override
public void failed(Throwable throwable) {
callback.failure(throwable);
}
@Override
public void completed(HttpResponse result) {
redirectHandler.handleRedirects(result, new CompletionHandler<HttpResponse>() {
@Override
public void failed(Throwable throwable) {
callback.failure(throwable);
}
@Override
public void completed(HttpResponse result) {
ClientResponse response = translateResponse(request, result, redirectHandler.getLastRequestUri());
callback.response(response);
}
});
}
});
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class GrizzlyConnector method translate.
private ClientResponse translate(final ClientRequest requestContext, final HttpResponseStatus status, final HttpResponseHeaders headers, final NonBlockingInputStream entityStream) {
final ClientResponse responseContext = new ClientResponse(new Response.StatusType() {
@Override
public int getStatusCode() {
return status.getStatusCode();
}
@Override
public Response.Status.Family getFamily() {
return Response.Status.Family.familyOf(status.getStatusCode());
}
@Override
public String getReasonPhrase() {
return status.getStatusText();
}
}, requestContext);
for (Map.Entry<String, List<String>> entry : headers.getHeaders().entrySet()) {
for (String value : entry.getValue()) {
responseContext.getHeaders().add(entry.getKey(), value);
}
}
responseContext.setEntityStream(entityStream);
return responseContext;
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class GrizzlyConnector method apply.
/**
* Sends the {@link javax.ws.rs.core.Request} via Grizzly transport and returns the {@link javax.ws.rs.core.Response}.
*
* @param request Jersey client request to be sent.
* @return received response.
*/
@Override
public ClientResponse apply(final ClientRequest request) {
final Request connectorRequest = translate(request);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(request.getHeaders(), connectorRequest);
final CompletableFuture<ClientResponse> responseFuture = new CompletableFuture<>();
final ByteBufferInputStream entityStream = new ByteBufferInputStream();
final AtomicBoolean futureSet = new AtomicBoolean(false);
try {
grizzlyClient.executeRequest(connectorRequest, new AsyncHandler<Void>() {
private volatile HttpResponseStatus status = null;
@Override
public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
status = responseStatus;
return STATE.CONTINUE;
}
@Override
public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
if (!futureSet.compareAndSet(false, true)) {
return STATE.ABORT;
}
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, request.getHeaders(), GrizzlyConnector.this.getClass().getName());
responseFuture.complete(translate(request, this.status, headers, entityStream));
return STATE.CONTINUE;
}
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
entityStream.put(bodyPart.getBodyByteBuffer());
return STATE.CONTINUE;
}
@Override
public Void onCompleted() throws Exception {
entityStream.closeQueue();
return null;
}
@Override
public void onThrowable(Throwable t) {
entityStream.closeQueue(t);
if (futureSet.compareAndSet(false, true)) {
t = t instanceof IOException ? new ProcessingException(t.getMessage(), t) : t;
responseFuture.completeExceptionally(t);
}
}
});
return responseFuture.get();
} catch (ExecutionException ex) {
Throwable e = ex.getCause() == null ? ex : ex.getCause();
throw new ProcessingException(e.getMessage(), e);
} catch (InterruptedException ex) {
throw new ProcessingException(ex.getMessage(), ex);
}
}
use of org.glassfish.jersey.client.ClientResponse in project jersey by jersey.
the class JettyConnector method translateResponse.
private static ClientResponse translateResponse(final ClientRequest jerseyRequest, final org.eclipse.jetty.client.api.Response jettyResponse, final NonBlockingInputStream entityStream) {
final ClientResponse jerseyResponse = new ClientResponse(Statuses.from(jettyResponse.getStatus()), jerseyRequest);
processResponseHeaders(jettyResponse.getHeaders(), jerseyResponse);
jerseyResponse.setEntityStream(entityStream);
return jerseyResponse;
}
Aggregations