Search in sources :

Example 1 with CloseableHttpClient

use of org.apache.hc.client5.http.impl.classic.CloseableHttpClient in project weicoder by wdcode.

the class HttpClient5 method init.

/**
 * 初始化httpclient
 *
 * @return CloseableHttpClient
 */
private static CloseableHttpClient init() {
    // Http连接池
    PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
    pool.setDefaultMaxPerRoute(C.O.CPU_NUM);
    pool.setMaxTotal(HttpParams.HTTP_MAX);
    // 设置请求参数
    RequestConfig.Builder config = RequestConfig.custom();
    config.setConnectionRequestTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
    config.setConnectTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
    config.setCircularRedirectsAllowed(false);
    // HttpClientBuilder
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setDefaultRequestConfig(config.build());
    builder.setConnectionManager(pool);
    // builder.setMaxConnPerRoute(SystemConstants.CPU_NUM);
    // 设置 头
    List<BasicHeader> headers = Lists.newList();
    headers.add(new BasicHeader(C.H.USER_AGENT_KEY, C.H.USER_AGENT_VAL));
    headers.add(new BasicHeader(C.H.ACCEPT_KEY, C.H.ACCEPT_VAL));
    builder.setDefaultHeaders(headers);
    // 实例化客户端
    return builder.build();
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)

Example 2 with CloseableHttpClient

use of org.apache.hc.client5.http.impl.classic.CloseableHttpClient in project ksql by confluentinc.

the class DefaultConnectClient method buildHttpClient.

/**
 * Uses defaults from Request.execute(), except with an explicit SSLSocketFactory to pass
 * custom SSL configs. Link to default below:
 * https://github.com/apache/httpcomponents-client/blob/3734aaa038a58c17af638e9fa29019cacb22e82c/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java#L62-L72
 */
private static CloseableHttpClient buildHttpClient(final Optional<SSLContext> sslContext, final boolean verifySslHostname) {
    final PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
    sslContext.ifPresent(ctx -> {
        final SSLConnectionSocketFactory socketFactory = verifySslHostname ? new SSLConnectionSocketFactory(ctx) : new SSLConnectionSocketFactory(ctx, (hostname, session) -> true);
        connectionManagerBuilder.setSSLSocketFactory(socketFactory);
    });
    return HttpClientBuilder.create().setConnectionManager(connectionManagerBuilder.setMaxConnPerRoute(100).setMaxConnTotal(200).setValidateAfterInactivity(TimeValue.ofSeconds(10L)).build()).useSystemProperties().evictExpiredConnections().evictIdleConnections(TimeValue.ofMinutes(1L)).build();
}
Also used : HttpClientResponseHandler(org.apache.hc.core5.http.io.HttpClientResponseHandler) SSLContext(javax.net.ssl.SSLContext) RetryerBuilder(com.github.rholder.retry.RetryerBuilder) RetryException(com.github.rholder.retry.RetryException) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Callable(java.util.concurrent.Callable) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Request(org.apache.hc.client5.http.fluent.Request) ConnectorPluginInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorPluginInfo) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) PoolingHttpClientConnectionManagerBuilder(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) URI(java.net.URI) TypeReference(com.fasterxml.jackson.core.type.TypeReference) EntityUtils(org.apache.hc.core5.http.io.entity.EntityUtils) WaitStrategies(com.github.rholder.retry.WaitStrategies) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) TimeValue(org.apache.hc.core5.util.TimeValue) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Header(org.apache.hc.core5.http.Header) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpHeaders(io.vertx.core.http.HttpHeaders) Collectors(java.util.stream.Collectors) Timeout(org.apache.hc.core5.util.Timeout) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) KsqlServerException(io.confluent.ksql.util.KsqlServerException) List(java.util.List) StopStrategies(com.github.rholder.retry.StopStrategies) Paths(java.nio.file.Paths) ConfigInfos(org.apache.kafka.connect.runtime.rest.entities.ConfigInfos) ContentType(org.apache.hc.core5.http.ContentType) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HttpEntity(org.apache.hc.core5.http.HttpEntity) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpStatus(org.apache.hc.core5.http.HttpStatus) Collections(java.util.Collections) PoolingHttpClientConnectionManagerBuilder(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory)

Example 3 with CloseableHttpClient

use of org.apache.hc.client5.http.impl.classic.CloseableHttpClient 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 4 with CloseableHttpClient

use of org.apache.hc.client5.http.impl.classic.CloseableHttpClient in project pact-jvm by DiUS.

the class HttpClient method insecureHttpClient.

static CloseableHttpClient insecureHttpClient() {
    SSLContext sslContext = null;
    try {
        sslContext = SSLContexts.custom().loadTrustMaterial(new TrustSelfSignedStrategy()).build();
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        throw new RuntimeException(e);
    }
    SSLConnectionSocketFactory socketFactory = SSLConnectionSocketFactoryBuilder.create().setSslContext(sslContext).build();
    CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", socketFactory).build())).build();
    return httpClient;
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) ConnectionSocketFactory(org.apache.hc.client5.http.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.hc.client5.http.socket.PlainConnectionSocketFactory) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.hc.client5.http.ssl.TrustSelfSignedStrategy) KeyManagementException(java.security.KeyManagementException) BasicHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager)

Example 5 with CloseableHttpClient

use of org.apache.hc.client5.http.impl.classic.CloseableHttpClient in project weicoder by wdcode.

the class HttpClient method init.

/**
 * 初始化httpclient
 *
 * @return CloseableHttpClient
 */
private static CloseableHttpClient init() {
    // Http连接池
    PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
    pool.setDefaultMaxPerRoute(SystemConstants.CPU_NUM);
    pool.setMaxTotal(HttpParams.HTTP_MAX);
    // 设置请求参数
    RequestConfig.Builder config = RequestConfig.custom();
    config.setConnectionRequestTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
    config.setConnectTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
    config.setCircularRedirectsAllowed(false);
    // HttpClientBuilder
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setDefaultRequestConfig(config.build());
    builder.setConnectionManager(pool);
    // builder.setMaxConnPerRoute(SystemConstants.CPU_NUM);
    // 设置 头
    List<BasicHeader> headers = Lists.newList();
    headers.add(new BasicHeader(HttpConstants.USER_AGENT_KEY, HttpConstants.USER_AGENT_VAL));
    headers.add(new BasicHeader(HttpConstants.ACCEPT_KEY, HttpConstants.ACCEPT_VAL));
    builder.setDefaultHeaders(headers);
    // 实例化客户端
    return builder.build();
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)

Aggregations

RequestConfig (org.apache.hc.client5.http.config.RequestConfig)3 HttpClientBuilder (org.apache.hc.client5.http.impl.classic.HttpClientBuilder)3 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)3 SSLContext (javax.net.ssl.SSLContext)2 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)2 PoolingHttpClientConnectionManager (org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)2 SSLConnectionSocketFactory (org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 RetryException (com.github.rholder.retry.RetryException)1 RetryerBuilder (com.github.rholder.retry.RetryerBuilder)1 StopStrategies (com.github.rholder.retry.StopStrategies)1 WaitStrategies (com.github.rholder.retry.WaitStrategies)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 KsqlException (io.confluent.ksql.util.KsqlException)1 KsqlServerException (io.confluent.ksql.util.KsqlServerException)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1 IOException (java.io.IOException)1