use of org.apache.hc.client5.http.async.methods.SimpleBody in project commercetools-jvm-sdk by commercetools.
the class ApacheHttpClientAdapterImpl method convertApacheToSphereResponse.
private HttpResponse convertApacheToSphereResponse(final SimpleHttpResponse apacheResponse, final HttpRequest httpRequest) {
final byte[] bodyNullable = Optional.ofNullable(apacheResponse.getBody()).map((SimpleBody entity) -> {
try {
final boolean gzipEncoded = Optional.ofNullable(apacheResponse.getFirstHeader(HttpHeaders.CONTENT_ENCODING)).map(Header::getValue).map(v -> v.equalsIgnoreCase("gzip")).orElse(false);
final InputStream body = new ByteArrayInputStream(entity.getBodyBytes());
final InputStream content = gzipEncoded ? new GZIPInputStream(body) : body;
final AutoCloseInputStream autoCloseInputStream = new AutoCloseInputStream(content);
final byte[] bytes = IOUtils.toByteArray(autoCloseInputStream);
return bytes;
} catch (final IOException e) {
throw new HttpException(e);
}
}).orElse(null);
final Integer statusCode = apacheResponse.getCode();
final Map<String, List<Header>> apacheHeaders = Arrays.stream(apacheResponse.getHeaders()).collect(Collectors.groupingBy(Header::getName));
final Function<Map.Entry<String, List<Header>>, String> keyMapper = e -> e.getKey();
final Map<String, List<String>> headers = apacheHeaders.entrySet().stream().collect(Collectors.toMap(keyMapper, e -> e.getValue().stream().map(Header::getValue).collect(Collectors.toList())));
return HttpResponse.of(statusCode, bodyNullable, httpRequest, HttpHeaders.of(headers));
}
use of org.apache.hc.client5.http.async.methods.SimpleBody in project httpcomponents-client by apache.
the class AbstractSimpleServerExchangeHandler method handle.
@Override
protected final void handle(final SimpleHttpRequest request, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws HttpException, IOException {
final SimpleHttpResponse response = handle(request, HttpCoreContext.adapt(context));
final SimpleBody body = response.getBody();
final AsyncEntityProducer entityProducer;
if (body != null) {
if (body.isText()) {
entityProducer = new StringAsyncEntityProducer(body.getBodyText(), body.getContentType());
} else {
entityProducer = new BasicAsyncEntityProducer(body.getBodyBytes(), body.getContentType());
}
} else {
entityProducer = null;
}
responseTrigger.submitResponse(new BasicResponseProducer(response, entityProducer), context);
}
use of org.apache.hc.client5.http.async.methods.SimpleBody in project httpcomponents-client by apache.
the class AsyncCachingExec method triggerResponse.
private void triggerResponse(final SimpleHttpResponse cacheResponse, final AsyncExecChain.Scope scope, final AsyncExecCallback asyncExecCallback) {
scope.clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, cacheResponse);
scope.execRuntime.releaseEndpoint();
final SimpleBody body = cacheResponse.getBody();
final byte[] content = body != null ? body.getBodyBytes() : null;
final ContentType contentType = body != null ? body.getContentType() : null;
try {
final AsyncDataConsumer dataConsumer = asyncExecCallback.handleResponse(cacheResponse, content != null ? new BasicEntityDetails(content.length, contentType) : null);
if (dataConsumer != null) {
if (content != null) {
dataConsumer.consume(ByteBuffer.wrap(content));
}
dataConsumer.streamEnd(null);
}
asyncExecCallback.completed();
} catch (final HttpException | IOException ex) {
asyncExecCallback.failed(ex);
}
}
use of org.apache.hc.client5.http.async.methods.SimpleBody in project httpcomponents-client by apache.
the class CachingExec method convert.
private static ClassicHttpResponse convert(final SimpleHttpResponse cacheResponse, final ExecChain.Scope scope) {
if (cacheResponse == null) {
return null;
}
final ClassicHttpResponse response = new BasicClassicHttpResponse(cacheResponse.getCode(), cacheResponse.getReasonPhrase());
for (final Iterator<Header> it = cacheResponse.headerIterator(); it.hasNext(); ) {
response.addHeader(it.next());
}
response.setVersion(cacheResponse.getVersion() != null ? cacheResponse.getVersion() : HttpVersion.DEFAULT);
final SimpleBody body = cacheResponse.getBody();
if (body != null) {
final ContentType contentType = body.getContentType();
final Header h = response.getFirstHeader(HttpHeaders.CONTENT_ENCODING);
final String contentEncoding = h != null ? h.getValue() : null;
if (body.isText()) {
response.setEntity(new StringEntity(body.getBodyText(), contentType, contentEncoding, false));
} else {
response.setEntity(new ByteArrayEntity(body.getBodyBytes(), contentType, contentEncoding, false));
}
}
scope.clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
return response;
}
use of org.apache.hc.client5.http.async.methods.SimpleBody in project commercetools-sdk-java-v2 by commercetools.
the class CtApacheHttpClient method toResponse.
private static ApiHttpResponse<byte[]> toResponse(final SimpleHttpResponse response) {
final Map<String, List<Header>> apacheHeaders = Arrays.stream(response.getHeaders()).collect(Collectors.groupingBy(Header::getName));
final ApiHttpHeaders apiHttpHeaders = new ApiHttpHeaders(apacheHeaders.entrySet().stream().flatMap(e -> e.getValue().stream().map(value -> ApiHttpHeaders.headerEntry(e.getKey(), value.getValue()))).collect(Collectors.toList()));
final byte[] bodyNullable = Optional.ofNullable(response.getBody()).map((SimpleBody entity) -> {
try {
final boolean gzipEncoded = Optional.ofNullable(response.getFirstHeader(HttpHeaders.CONTENT_ENCODING)).map(Header::getValue).map(v -> v.equalsIgnoreCase("gzip")).orElse(false);
final InputStream body = new ByteArrayInputStream(entity.getBodyBytes());
final InputStream content = gzipEncoded ? new GZIPInputStream(body) : body;
final AutoCloseInputStream autoCloseInputStream = new AutoCloseInputStream(content);
return IOUtils.toByteArray(autoCloseInputStream);
} catch (final IOException e) {
throw new HttpException(e);
}
}).orElse(null);
return new ApiHttpResponse<>(response.getCode(), apiHttpHeaders, bodyNullable, response.getReasonPhrase());
}
Aggregations