Search in sources :

Example 1 with HttpClientContext

use of org.apache.hc.client5.http.protocol.HttpClientContext in project feign by OpenFeign.

the class ApacheHttp5Client method execute.

@Override
public Response execute(Request request, Request.Options options) throws IOException {
    ClassicHttpRequest httpUriRequest;
    try {
        httpUriRequest = toClassicHttpRequest(request, options);
    } catch (final URISyntaxException e) {
        throw new IOException("URL '" + request.url() + "' couldn't be parsed into a URI", e);
    }
    final HttpHost target = HttpHost.create(URI.create(request.url()));
    final HttpClientContext context = configureTimeouts(options);
    final ClassicHttpResponse httpResponse = (ClassicHttpResponse) client.execute(target, httpUriRequest, context);
    return toFeignResponse(httpResponse, request);
}
Also used : HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) URISyntaxException(java.net.URISyntaxException)

Example 2 with HttpClientContext

use of org.apache.hc.client5.http.protocol.HttpClientContext in project feign by OpenFeign.

the class ApacheHttp5Client method configureTimeouts.

protected HttpClientContext configureTimeouts(Request.Options options) {
    final HttpClientContext context = new HttpClientContext();
    // 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;
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) Configurable(org.apache.hc.client5.http.config.Configurable)

Example 3 with HttpClientContext

use of org.apache.hc.client5.http.protocol.HttpClientContext 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;
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) Configurable(org.apache.hc.client5.http.config.Configurable)

Example 4 with HttpClientContext

use of org.apache.hc.client5.http.protocol.HttpClientContext 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;
}
Also used : SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback)

Example 5 with HttpClientContext

use of org.apache.hc.client5.http.protocol.HttpClientContext 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)));
}
Also used : BasicCookieStore(org.apache.hc.client5.http.cookie.BasicCookieStore) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext)

Aggregations

HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)4 Configurable (org.apache.hc.client5.http.config.Configurable)2 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)2 URISyntaxException (java.net.URISyntaxException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)1 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)1 BasicCookieStore (org.apache.hc.client5.http.cookie.BasicCookieStore)1 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)1