Search in sources :

Example 1 with SimpleBody

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));
}
Also used : AsyncEntityProducers(org.apache.hc.core5.http.nio.entity.AsyncEntityProducers) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) WWWFormCodec(org.apache.hc.core5.net.WWWFormCodec) Function(java.util.function.Function) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Nullable(javax.annotation.Nullable) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) Header(org.apache.hc.core5.http.Header) IOReactorStatus(org.apache.hc.core5.reactor.IOReactorStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) org.apache.hc.client5.http.async.methods(org.apache.hc.client5.http.async.methods) java.io(java.io) ContentType(org.apache.hc.core5.http.ContentType) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) Optional(java.util.Optional) GZIPInputStream(java.util.zip.GZIPInputStream) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.apache.hc.core5.http.Header) Arrays.asList(java.util.Arrays.asList) List(java.util.List) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream)

Example 2 with SimpleBody

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);
}
Also used : SimpleBody(org.apache.hc.client5.http.async.methods.SimpleBody) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer)

Example 3 with SimpleBody

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);
    }
}
Also used : SimpleBody(org.apache.hc.client5.http.async.methods.SimpleBody) ContentType(org.apache.hc.core5.http.ContentType) AsyncDataConsumer(org.apache.hc.core5.http.nio.AsyncDataConsumer) BasicEntityDetails(org.apache.hc.core5.http.impl.BasicEntityDetails) HttpException(org.apache.hc.core5.http.HttpException) ResourceIOException(org.apache.hc.client5.http.cache.ResourceIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 4 with SimpleBody

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;
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) SimpleBody(org.apache.hc.client5.http.async.methods.SimpleBody) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) Header(org.apache.hc.core5.http.Header) ContentType(org.apache.hc.core5.http.ContentType) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Example 5 with SimpleBody

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());
}
Also used : java.util(java.util) GZIPInputStream(java.util.zip.GZIPInputStream) AsyncClientConnectionManager(org.apache.hc.client5.http.nio.AsyncClientConnectionManager) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) HttpVersionPolicy(org.apache.hc.core5.http2.HttpVersionPolicy) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleResponseConsumer(org.apache.hc.client5.http.async.methods.SimpleResponseConsumer) TlsDetails(org.apache.hc.core5.reactor.ssl.TlsDetails) ClientTlsStrategyBuilder(org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder) PoolingAsyncClientConnectionManagerBuilder(org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) HttpAsyncClientBuilder(org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder) SimpleBody(org.apache.hc.client5.http.async.methods.SimpleBody) Header(org.apache.hc.core5.http.Header) IOException(java.io.IOException) io.vrap.rmf.base.client(io.vrap.rmf.base.client) IOReactorStatus(org.apache.hc.core5.reactor.IOReactorStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) HttpHeaders(org.apache.hc.core5.http.HttpHeaders) ContentType(org.apache.hc.core5.http.ContentType) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) InputStream(java.io.InputStream) SimpleBody(org.apache.hc.client5.http.async.methods.SimpleBody) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream)

Aggregations

SimpleBody (org.apache.hc.client5.http.async.methods.SimpleBody)5 ContentType (org.apache.hc.core5.http.ContentType)5 Header (org.apache.hc.core5.http.Header)4 IOException (java.io.IOException)3 StandardCharsets (java.nio.charset.StandardCharsets)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Collectors (java.util.stream.Collectors)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 IOUtils (org.apache.commons.io.IOUtils)3 AutoCloseInputStream (org.apache.commons.io.input.AutoCloseInputStream)3 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)3 CloseableHttpAsyncClient (org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)3 AsyncRequestProducer (org.apache.hc.core5.http.nio.AsyncRequestProducer)3 AsyncRequestBuilder (org.apache.hc.core5.http.nio.support.AsyncRequestBuilder)3 IOReactorStatus (org.apache.hc.core5.reactor.IOReactorStatus)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Arrays.asList (java.util.Arrays.asList)2 List (java.util.List)2 Map (java.util.Map)2