Search in sources :

Example 16 with Request

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;
}
Also used : SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest)

Example 17 with Request

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

Example 18 with Request

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;
}
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 19 with Request

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

Aggregations

IOException (java.io.IOException)8 HttpRequest (org.apache.hc.core5.http.HttpRequest)7 Test (org.junit.Test)7 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)5 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)5 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)4 ExecutionException (java.util.concurrent.ExecutionException)3 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)3 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 HttpExchange (com.sun.net.httpserver.HttpExchange)2 ClientProtocolException (org.apache.hc.client5.http.ClientProtocolException)2 Configurable (org.apache.hc.client5.http.config.Configurable)2 BasicCookieStore (org.apache.hc.client5.http.cookie.BasicCookieStore)2 PoolingAsyncClientConnectionManager (org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager)2 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)2 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)2 Timer (com.codahale.metrics.Timer)1 Gson (com.google.gson.Gson)1