Search in sources :

Example 1 with RequestConfig

use of org.apache.hc.client5.http.config.RequestConfig in project ksql by confluentinc.

the class WebClient method send.

/**
 * Sends a POST request to a web server
 * This method requires a pre-configured http client instance
 *
 * @param customerId customer Id on behalf of which the request is sent
 * @param bytes request payload
 * @param httpPost A POST request structure
 * @param proxy a http (passive) proxy
 * @param httpClient http client instance configured by caller
 * @return an HTTP Status code
 * @see #send(String, byte[], HttpPost, ResponseHandler)
 */
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:FinalParameters" })
protected static int send(final String customerId, final byte[] bytes, final HttpPost httpPost, final HttpHost proxy, CloseableHttpClient httpClient, final ResponseHandler responseHandler) {
    int statusCode = DEFAULT_STATUS_CODE;
    if (bytes != null && bytes.length > 0 && httpPost != null && customerId != null) {
        // add the body to the request
        final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.LEGACY);
        builder.addTextBody("cid", customerId);
        builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, "filename");
        httpPost.setEntity(builder.build());
        httpPost.addHeader("api-version", "phone-home-v1");
        // set the HTTP config
        RequestConfig config = RequestConfig.custom().setConnectTimeout(Timeout.ofMilliseconds(REQUEST_TIMEOUT_MS)).setConnectionRequestTimeout(Timeout.ofMilliseconds(REQUEST_TIMEOUT_MS)).setResponseTimeout(Timeout.ofMilliseconds(REQUEST_TIMEOUT_MS)).build();
        CloseableHttpResponse response = null;
        try {
            if (proxy != null) {
                log.debug("setting proxy to {}", proxy);
                config = RequestConfig.copy(config).setProxy(proxy).build();
                httpPost.setConfig(config);
                final DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
                if (httpClient == null) {
                    httpClient = HttpClientBuilder.create().setRoutePlanner(routePlanner).setDefaultRequestConfig(config).build();
                }
            } else {
                if (httpClient == null) {
                    httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
                }
            }
            response = httpClient.execute(httpPost);
            if (responseHandler != null) {
                responseHandler.handle(response);
            }
            // send request
            log.debug("POST request returned {}", new StatusLine(response).toString());
            statusCode = response.getCode();
        } catch (IOException e) {
            log.error("Could not submit metrics to Confluent: {}", e.getMessage());
        } finally {
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    log.warn("could not close http client", e);
                }
            }
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    log.warn("could not close http response", e);
                }
            }
        }
    } else {
        statusCode = HttpStatus.SC_BAD_REQUEST;
    }
    return statusCode;
}
Also used : StatusLine(org.apache.hc.core5.http.message.StatusLine) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) MultipartEntityBuilder(org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) DefaultProxyRoutePlanner(org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner) IOException(java.io.IOException)

Example 2 with RequestConfig

use of org.apache.hc.client5.http.config.RequestConfig 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 RequestConfig

use of org.apache.hc.client5.http.config.RequestConfig 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)

Aggregations

RequestConfig (org.apache.hc.client5.http.config.RequestConfig)3 Configurable (org.apache.hc.client5.http.config.Configurable)2 IOException (java.io.IOException)1 MultipartEntityBuilder (org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder)1 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)1 DefaultProxyRoutePlanner (org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner)1 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)1 StatusLine (org.apache.hc.core5.http.message.StatusLine)1