Search in sources :

Example 1 with AsyncRequestBuilder

use of org.apache.hc.core5.http.nio.support.AsyncRequestBuilder in project commercetools-jvm-sdk by commercetools.

the class ApacheHttpClientAdapterImpl method toApacheRequest.

private AsyncRequestProducer toApacheRequest(final HttpRequest httpRequest) {
    final String method = httpRequest.getHttpMethod().toString();
    final String uri = httpRequest.getUrl();
    final AsyncRequestBuilder builder = AsyncRequestBuilder.create(method);
    builder.setUri(uri);
    httpRequest.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
    if (httpRequest.getBody() != null) {
        final HttpRequestBody body = httpRequest.getBody();
        if (body instanceof StringHttpRequestBody) {
            builder.setEntity(((StringHttpRequestBody) body).getString(), ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8));
        } else if (body instanceof FileHttpRequestBody) {
            builder.setEntity(AsyncEntityProducers.create(((FileHttpRequestBody) body).getFile(), ContentType.DEFAULT_BINARY));
        } else if (body instanceof FormUrlEncodedHttpRequestBody) {
            builder.setEntity(urlEncodedOf((FormUrlEncodedHttpRequestBody) body), ContentType.APPLICATION_FORM_URLENCODED);
        } else {
            throw new HttpException("Cannot interpret request " + httpRequest);
        }
    }
    return builder.build();
}
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) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder)

Example 2 with AsyncRequestBuilder

use of org.apache.hc.core5.http.nio.support.AsyncRequestBuilder in project commercetools-sdk-java-v2 by commercetools.

the class CtApacheHttpClient method toApacheRequest.

private AsyncRequestProducer toApacheRequest(final ApiHttpRequest httpRequest) {
    final String method = httpRequest.getMethod().toString();
    final AsyncRequestBuilder builder = AsyncRequestBuilder.create(method);
    builder.setUri(httpRequest.getUri());
    httpRequest.getHeaders().getHeaders().forEach((entry) -> builder.addHeader(entry.getKey(), entry.getValue()));
    if (httpRequest.getBody() != null) {
        // default media type is JSON, if other media type is set as a header, use it
        ContentType mediaType = ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8);
        if (httpRequest.getHeaders().getHeaders().stream().anyMatch(s -> s.getKey().equalsIgnoreCase(ApiHttpHeaders.CONTENT_TYPE))) {
            mediaType = ContentType.parse(Objects.requireNonNull(httpRequest.getHeaders().getFirst(ApiHttpHeaders.CONTENT_TYPE)));
        }
        builder.setEntity(httpRequest.getBody(), mediaType);
    }
    return builder.build();
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder)

Example 3 with AsyncRequestBuilder

use of org.apache.hc.core5.http.nio.support.AsyncRequestBuilder in project commercetools-jvm-sdk by commercetools.

the class IntegrationTestHttpClient method toApacheRequest.

private AsyncRequestProducer toApacheRequest(final HttpRequest httpRequest) throws UnsupportedEncodingException {
    final String method = httpRequest.getHttpMethod().toString();
    final String uri = httpRequest.getUrl();
    final AsyncRequestBuilder builder = AsyncRequestBuilder.create(method);
    builder.setUri(uri);
    httpRequest.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
    if (httpRequest.getBody() != null) {
        final HttpRequestBody body = httpRequest.getBody();
        if (body instanceof StringHttpRequestBody) {
            builder.setEntity(((StringHttpRequestBody) body).getString(), ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8));
        } else if (body instanceof FileHttpRequestBody) {
            builder.setEntity(AsyncEntityProducers.create(((FileHttpRequestBody) body).getFile(), ContentType.DEFAULT_BINARY));
        } else if (body instanceof FormUrlEncodedHttpRequestBody) {
            builder.setEntity(urlEncodedOf((FormUrlEncodedHttpRequestBody) body), ContentType.APPLICATION_FORM_URLENCODED);
        } else {
            throw new HttpException("Cannot interpret request " + httpRequest);
        }
    }
    return builder.build();
}
Also used : AsyncEntityProducers(org.apache.hc.core5.http.nio.entity.AsyncEntityProducers) 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) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) Function(java.util.function.Function) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) SimpleResponseConsumer(org.apache.hc.client5.http.async.methods.SimpleResponseConsumer) Nullable(javax.annotation.Nullable) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) SimpleBody(org.apache.hc.client5.http.async.methods.SimpleBody) Header(org.apache.hc.core5.http.Header) io.sphere.sdk.http(io.sphere.sdk.http) IOReactorStatus(org.apache.hc.core5.reactor.IOReactorStatus) IOException(java.io.IOException) 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) 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) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InputStream(java.io.InputStream) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder)

Aggregations

ContentType (org.apache.hc.core5.http.ContentType)3 AsyncRequestBuilder (org.apache.hc.core5.http.nio.support.AsyncRequestBuilder)3 StandardCharsets (java.nio.charset.StandardCharsets)2 Arrays.asList (java.util.Arrays.asList)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 Nullable (javax.annotation.Nullable)2 IOUtils (org.apache.commons.io.IOUtils)2 AutoCloseInputStream (org.apache.commons.io.input.AutoCloseInputStream)2 CloseableHttpAsyncClient (org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)2 Header (org.apache.hc.core5.http.Header)2 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)2 AsyncRequestProducer (org.apache.hc.core5.http.nio.AsyncRequestProducer)2 AsyncEntityProducers (org.apache.hc.core5.http.nio.entity.AsyncEntityProducers)2