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();
}
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();
}
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();
}
Aggregations