use of org.apache.hc.client5.http.fluent.Request in project feign by OpenFeign.
the class AsyncApacheHttp5Client method toClassicHttpRequest.
SimpleHttpRequest toClassicHttpRequest(Request request, Request.Options options) {
final SimpleHttpRequest httpRequest = new SimpleHttpRequest(request.httpMethod().name(), request.url());
// request headers
boolean hasAcceptHeader = false;
for (final Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
final String headerName = headerEntry.getKey();
if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
hasAcceptHeader = true;
}
if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)) {
// doesn't like us to set it as well.
continue;
}
for (final String headerValue : headerEntry.getValue()) {
httpRequest.addHeader(headerName, headerValue);
}
}
// some servers choke on the default accept string, so we'll set it to anything
if (!hasAcceptHeader) {
httpRequest.addHeader(ACCEPT_HEADER_NAME, "*/*");
}
// request body
// final Body requestBody = request.requestBody();
final byte[] data = request.body();
if (data != null) {
httpRequest.setBody(data, getContentType(request));
}
return httpRequest;
}
use of org.apache.hc.client5.http.fluent.Request in project feign by OpenFeign.
the class AsyncApacheHttp5Client method configureTimeouts.
protected HttpClientContext configureTimeouts(Request.Options options, HttpClientContext context) {
// per request timeouts
final RequestConfig requestConfig = (client instanceof Configurable ? RequestConfig.copy(((Configurable) client).getConfig()) : RequestConfig.custom()).setConnectTimeout(options.connectTimeout(), options.connectTimeoutUnit()).setResponseTimeout(options.readTimeout(), options.readTimeoutUnit()).build();
context.setRequestConfig(requestConfig);
return context;
}
use of org.apache.hc.client5.http.fluent.Request in project feign by OpenFeign.
the class AsyncApacheHttp5Client method execute.
@Override
public CompletableFuture<Response> execute(Request request, Options options, Optional<HttpClientContext> requestContext) {
final SimpleHttpRequest httpUriRequest = toClassicHttpRequest(request, options);
final CompletableFuture<Response> result = new CompletableFuture<>();
final FutureCallback<SimpleHttpResponse> callback = new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(SimpleHttpResponse httpResponse) {
result.complete(toFeignResponse(httpResponse, request));
}
@Override
public void failed(Exception ex) {
result.completeExceptionally(ex);
}
@Override
public void cancelled() {
result.cancel(false);
}
};
client.execute(httpUriRequest, configureTimeouts(options, requestContext.orElseGet(HttpClientContext::new)), callback);
return result;
}
use of org.apache.hc.client5.http.fluent.Request in project spring-framework by spring-projects.
the class HttpComponentsClientHttpConnector method connect.
@Override
public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
HttpClientContext context = this.contextProvider.apply(method, uri);
if (context.getCookieStore() == null) {
context.setCookieStore(new BasicCookieStore());
}
HttpComponentsClientHttpRequest request = new HttpComponentsClientHttpRequest(method, uri, context, this.dataBufferFactory);
return requestCallback.apply(request).then(Mono.defer(() -> execute(request, context)));
}
Aggregations