Search in sources :

Example 66 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project build-info by JFrogDev.

the class PreemptiveHttpClient method createHttpClientBuilder.

private HttpClientBuilder createHttpClientBuilder(String userName, String password, int timeout, int connectionRetries) {
    this.connectionRetries = connectionRetries;
    int timeoutMilliSeconds = timeout * 1000;
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeoutMilliSeconds).setConnectTimeout(timeoutMilliSeconds).setCircularRedirectsAllowed(true).build();
    HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig);
    if (StringUtils.isEmpty(userName)) {
        userName = "anonymous";
        password = "";
    }
    basicCredentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(userName, password));
    localContext.setCredentialsProvider(basicCredentialsProvider);
    // Add as the first request interceptor
    builder.addInterceptorFirst(new PreemptiveAuth());
    int retryCount = connectionRetries < 0 ? ArtifactoryHttpClient.DEFAULT_CONNECTION_RETRY : connectionRetries;
    builder.setRetryHandler(new PreemptiveRetryHandler(retryCount));
    builder.setServiceUnavailableRetryStrategy(new PreemptiveRetryStrategy());
    builder.setRedirectStrategy(new PreemptiveRedirectStrategy());
    // set the following user agent with each request
    String userAgent = "ArtifactoryBuildClient/" + CLIENT_VERSION;
    builder.setUserAgent(userAgent);
    return builder;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) AuthScope(org.apache.http.auth.AuthScope) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 67 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project epp.mpc by eclipse.

the class HttpClientFactory method setClientTimeouts.

private static void setClientTimeouts(HttpClientBuilder builder) {
    @SuppressWarnings("restriction") int connectTimeoutUssDefault = StorageProperties.getProperty(StorageProperties.CONNECT_TIMEOUT, HttpClientTransport.DEFAULT_CONNECT_TIMEOUT);
    @SuppressWarnings("restriction") int readTimeoutUssDefault = StorageProperties.getProperty(StorageProperties.SOCKET_TIMEOUT, HttpClientTransport.DEFAULT_READ_TIMEOUT);
    int connectTimeout = getTimeoutValue(HttpClientTransport.CONNECT_TIMEOUT_PROPERTY, connectTimeoutUssDefault);
    int readTimeout = getTimeoutValue(HttpClientTransport.READ_TIMEOUT_PROPERTY, readTimeoutUssDefault);
    int connectionRequestTimeout = getTimeoutValue(HttpClientTransport.CONNECTION_REQUEST_TIMEOUT_PROPERTY, HttpClientTransport.DEFAULT_CONNECTION_REQUEST_TIMEOUT);
    SocketConfig defaultSocketConfig = SocketConfig.copy(SocketConfig.DEFAULT).setSoTimeout(readTimeout).setTcpNoDelay(// Disable Nagle - see https://en.wikipedia.org/wiki/Nagle%27s_algorithm#Negative_effect_on_larger_writes
    true).build();
    RequestConfig defaultRequestConfig = RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(readTimeout).setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).build();
    builder.setDefaultSocketConfig(defaultSocketConfig);
    builder.setDefaultRequestConfig(defaultRequestConfig);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) SocketConfig(org.apache.http.config.SocketConfig)

Example 68 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project gateway-dubbox by zhuzhong.

the class OpenApiHttpClientServiceImpl method initHttpClient.

private void initHttpClient() {
    // 注册访问协议相关的socket工厂
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build();
    // httpclient 工厂
    HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE);
    // dns解析器
    DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE;
    // 创建池化连接管理器
    manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connFactory, dnsResolver);
    // 默认socket配置
    SocketConfig defaultSocketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
    manager.setDefaultSocketConfig(defaultSocketConfig);
    // 设置整个连接池的最大连接数
    manager.setMaxTotal(this.maxTotal);
    // 每个路由最大连接数
    manager.setDefaultMaxPerRoute(this.defaultMaxPerRoute);
    manager.setValidateAfterInactivity(this.validateAfterInactivity);
    RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(this.connectionTimeout).setSocketTimeout(this.socketTimeout).setConnectionRequestTimeout(this.connectionRequestTimeout).build();
    httpClient = HttpClients.custom().setConnectionManager(manager).setConnectionManagerShared(false).evictIdleConnections(60, TimeUnit.SECONDS).evictExpiredConnections().setConnectionTimeToLive(60, TimeUnit.SECONDS).setDefaultRequestConfig(defaultRequestConfig).setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE).setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE).setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)).build();
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : DnsResolver(org.apache.http.conn.DnsResolver) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) RequestConfig(org.apache.http.client.config.RequestConfig) SocketConfig(org.apache.http.config.SocketConfig) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpRoute(org.apache.http.conn.routing.HttpRoute) ManagedHttpClientConnection(org.apache.http.conn.ManagedHttpClientConnection) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) ManagedHttpClientConnectionFactory(org.apache.http.impl.conn.ManagedHttpClientConnectionFactory)

Example 69 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project vespa by vespa-engine.

the class ConfigServerRestExecutorImpl method proxyCall.

private Optional<ProxyResponse> proxyCall(URI uri, ProxyRequest proxyRequest, StringBuilder errorBuilder) throws ProxyException {
    String fullUri = uri.toString() + removeFirstSlashIfAny(proxyRequest.getConfigServerRequest());
    final HttpRequestBase requestBase = createHttpBaseRequest(proxyRequest.getMethod(), fullUri, proxyRequest.getData());
    // Empty list of headers to copy for now, add headers when needed, or rewrite logic.
    copyHeaders(proxyRequest.getHeaders(), requestBase);
    RequestConfig config = RequestConfig.custom().setConnectTimeout((int) PROXY_REQUEST_TIMEOUT.toMillis()).setConnectionRequestTimeout((int) PROXY_REQUEST_TIMEOUT.toMillis()).setSocketTimeout((int) PROXY_REQUEST_TIMEOUT.toMillis()).build();
    try (CloseableHttpClient client = createHttpClient(config, sslContextProvider, zoneRegistry, proxyRequest);
        CloseableHttpResponse response = client.execute(requestBase)) {
        String content = getContent(response);
        int status = response.getStatusLine().getStatusCode();
        if (status / 100 == 5) {
            errorBuilder.append("Talking to server ").append(uri.getHost());
            errorBuilder.append(", got ").append(status).append(" ").append(content).append("\n");
            log.log(LogLevel.DEBUG, () -> String.format("Got response from %s with status code %d and content:\n %s", uri.getHost(), status, content));
            return Optional.empty();
        }
        final Header contentHeader = response.getLastHeader("Content-Type");
        final String contentType;
        if (contentHeader != null && contentHeader.getValue() != null && !contentHeader.getValue().isEmpty()) {
            contentType = contentHeader.getValue().replace("; charset=UTF-8", "");
        } else {
            contentType = "application/json";
        }
        // Send response back
        return Optional.of(new ProxyResponse(proxyRequest, content, status, Optional.of(uri), contentType));
    } catch (Exception e) {
        errorBuilder.append("Talking to server ").append(uri.getHost());
        errorBuilder.append(" got exception ").append(e.getMessage());
        log.log(LogLevel.DEBUG, e, () -> "Got exception while sending request to " + uri.getHost());
        return Optional.empty();
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SSLException(javax.net.ssl.SSLException)

Example 70 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project vespa by vespa-engine.

the class ConfigServerRestExecutorImpl method queueFirstServerIfDown.

/**
 * During upgrade, one server can be down, this is normal. Therefor we do a quick ping on the first server,
 * if it is not responding, we try the other servers first. False positive/negatives are not critical,
 * but will increase latency to some extent.
 */
private boolean queueFirstServerIfDown(List<URI> allServers, ProxyRequest proxyRequest) {
    if (allServers.size() < 2) {
        return false;
    }
    URI uri = allServers.get(0);
    HttpGet httpget = new HttpGet(uri);
    int timeout = 500;
    RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
    try (CloseableHttpClient client = createHttpClient(config, sslContextProvider, zoneRegistry, proxyRequest);
        CloseableHttpResponse response = client.execute(httpget)) {
        if (response.getStatusLine().getStatusCode() == 200) {
            return false;
        }
    } catch (IOException e) {
    // We ignore this, if server is restarting this might happen.
    }
    // Some error happened, move this server to the back. The other servers should be running.
    allServers.remove(0);
    allServers.add(uri);
    return true;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URI(java.net.URI)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)344 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)146 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)97 HttpGet (org.apache.http.client.methods.HttpGet)94 IOException (java.io.IOException)78 HttpEntity (org.apache.http.HttpEntity)67 HttpPost (org.apache.http.client.methods.HttpPost)65 HttpResponse (org.apache.http.HttpResponse)60 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)55 URI (java.net.URI)46 StringEntity (org.apache.http.entity.StringEntity)43 Map (java.util.Map)41 Test (org.junit.Test)41 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)33 HttpHost (org.apache.http.HttpHost)32 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)32 HttpClient (org.apache.http.client.HttpClient)31 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)27 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)24 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)24