Search in sources :

Example 6 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project sling by apache.

the class TopologyConnectorClient method createHttpClient.

private CloseableHttpClient createHttpClient() {
    final HttpClientBuilder builder = HttpClientBuilder.create();
    // setting the SoTimeout (which is configured in seconds)
    builder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(1000 * config.getSoTimeout()).build());
    builder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    return builder.build();
}
Also used : DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder)

Example 7 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project docker-maven-plugin by fabric8io.

the class HttpPingChecker method ping.

private boolean ping() throws IOException {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_PING_TIMEOUT).setConnectTimeout(HTTP_PING_TIMEOUT).setConnectionRequestTimeout(HTTP_PING_TIMEOUT).setRedirectsEnabled(false).build();
    CloseableHttpClient httpClient;
    if (allowAllHosts) {
        SSLContextBuilder builder = new SSLContextBuilder();
        try {
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
            httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_RETRIES, false)).setSSLSocketFactory(socketFactory).build();
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
            throw new IOException("Unable to set self signed strategy on http wait: " + e, e);
        }
    } else {
        httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_RETRIES, false)).build();
    }
    try {
        CloseableHttpResponse response = httpClient.execute(RequestBuilder.create(method.toUpperCase()).setUri(url).build());
        try {
            int responseCode = response.getStatusLine().getStatusCode();
            if (responseCode == 501) {
                throw new IllegalArgumentException("Invalid or not supported HTTP method '" + method.toUpperCase() + "' for checking " + url);
            }
            return responseCode >= statusMin && responseCode <= statusMax;
        } finally {
            response.close();
        }
    } finally {
        httpClient.close();
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Example 8 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project cuba by cuba-platform.

the class IdpServiceLogoutCallbackInvoker method performLogoutOnServiceProviders.

public void performLogoutOnServiceProviders(String idpSessionId) {
    List<String> serviceProviderLogoutUrls = idpConfig.getServiceProviderLogoutUrls();
    asyncServiceLogoutExecutor.submit(() -> {
        HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
        HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)).build();
        try {
            for (String serviceProviderLogoutUrl : serviceProviderLogoutUrls) {
                callLoggedOutServiceUrl(client, serviceProviderLogoutUrl, idpSessionId);
            }
        } catch (Throwable ex) {
            log.error("Unable to perform logout on IDP services for session {}", idpSessionId, ex);
        } finally {
            connectionManager.shutdown();
        }
    });
}
Also used : HttpClient(org.apache.http.client.HttpClient) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 9 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project java by wavefrontHQ.

the class AbstractAgent method createAgentService.

/**
 * Create RESTeasy proxies for remote calls via HTTP.
 */
protected WavefrontAPI createAgentService() {
    ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
    factory.registerProvider(JsonNodeWriter.class);
    if (!factory.getClasses().contains(ResteasyJackson2Provider.class)) {
        factory.registerProvider(ResteasyJackson2Provider.class);
    }
    if (httpUserAgent == null) {
        httpUserAgent = "Wavefront-Proxy/" + props.getString("build.version");
    }
    ClientHttpEngine httpEngine;
    if (javaNetConnection) {
        httpEngine = new JavaNetConnectionEngine() {

            @Override
            protected HttpURLConnection createConnection(ClientInvocation request) throws IOException {
                HttpURLConnection connection = (HttpURLConnection) request.getUri().toURL().openConnection();
                connection.setRequestProperty("User-Agent", httpUserAgent);
                connection.setRequestMethod(request.getMethod());
                // 5s
                connection.setConnectTimeout(httpConnectTimeout);
                // 60s
                connection.setReadTimeout(httpRequestTimeout);
                if (connection instanceof HttpsURLConnection) {
                    HttpsURLConnection secureConnection = (HttpsURLConnection) connection;
                    secureConnection.setSSLSocketFactory(new SSLSocketFactoryImpl(HttpsURLConnection.getDefaultSSLSocketFactory(), httpRequestTimeout));
                }
                return connection;
            }
        };
    } else {
        HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().setUserAgent(httpUserAgent).setMaxConnTotal(httpMaxConnTotal).setMaxConnPerRoute(httpMaxConnPerRoute).setConnectionTimeToLive(1, TimeUnit.MINUTES).setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(httpRequestTimeout).build()).setSSLSocketFactory(new SSLConnectionSocketFactoryImpl(SSLConnectionSocketFactory.getSystemSocketFactory(), httpRequestTimeout)).setRetryHandler(new DefaultHttpRequestRetryHandler(httpAutoRetries, true)).setDefaultRequestConfig(RequestConfig.custom().setContentCompressionEnabled(true).setRedirectsEnabled(true).setConnectTimeout(httpConnectTimeout).setConnectionRequestTimeout(httpConnectTimeout).setSocketTimeout(httpRequestTimeout).build()).build();
        final ApacheHttpClient4Engine apacheHttpClient4Engine = new ApacheHttpClient4Engine(httpClient, true);
        // avoid using disk at all
        apacheHttpClient4Engine.setFileUploadInMemoryThresholdLimit(100);
        apacheHttpClient4Engine.setFileUploadMemoryUnit(ApacheHttpClient4Engine.MemoryUnit.MB);
        httpEngine = apacheHttpClient4Engine;
    }
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(factory).register(GZIPDecodingInterceptor.class).register(gzipCompression ? GZIPEncodingInterceptor.class : DisableGZIPEncodingInterceptor.class).register(AcceptEncodingGZIPFilter.class).build();
    ResteasyWebTarget target = client.target(server);
    return target.proxy(WavefrontAPI.class);
}
Also used : ResteasyJackson2Provider(org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) GZIPDecodingInterceptor(org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) ClientInvocation(org.jboss.resteasy.client.jaxrs.internal.ClientInvocation) IOException(java.io.IOException) AcceptEncodingGZIPFilter(org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPFilter) ClientHttpEngine(org.jboss.resteasy.client.jaxrs.ClientHttpEngine) HttpURLConnection(java.net.HttpURLConnection) GZIPEncodingInterceptor(org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor) HttpClient(org.apache.http.client.HttpClient) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 10 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project jenkin-qtest-plugin by QASymphony.

the class HttpClientUtils method getHttpClient.

public static HttpClient getHttpClient(String hostUrl) throws Exception {
    int timeout;
    try {
        timeout = Integer.parseInt(System.getenv("SOCKET_TIMEOUT"));
    } catch (Exception e) {
        timeout = DEFAULT_SOCKET_TIMEOUT;
    }
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().useSystemProperties();
    setHttpProxy(httpClientBuilder, hostUrl);
    SSLConnectionSocketFactory sslSocketFactory = getSslSocketFactory();
    httpClientBuilder.setSSLSocketFactory(sslSocketFactory).setConnectionReuseStrategy(new NoConnectionReuseStrategy());
    timeout = timeout * 1000;
    httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).build());
    httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(RETRY_MAX_COUNT, RETRY_REQUEST_SEND_RETRY_ENABLED) {

        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount > this.getRetryCount())
                return false;
            if (exception instanceof HttpHostConnectException)
                return true;
            return super.retryRequest(exception, executionCount, context);
        }
    });
    return httpClientBuilder.build();
}
Also used : NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) IOException(java.io.IOException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyStoreException(java.security.KeyStoreException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

DefaultHttpRequestRetryHandler (org.apache.http.impl.client.DefaultHttpRequestRetryHandler)16 IOException (java.io.IOException)6 HttpContext (org.apache.http.protocol.HttpContext)5 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)4 HttpParams (org.apache.http.params.HttpParams)4 RequestConfig (org.apache.http.client.config.RequestConfig)3 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)3 KeyManagementException (java.security.KeyManagementException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ConnectionReuseStrategy (org.apache.http.ConnectionReuseStrategy)2 HttpHost (org.apache.http.HttpHost)2 HttpResponse (org.apache.http.HttpResponse)2 AuthScope (org.apache.http.auth.AuthScope)2 NTCredentials (org.apache.http.auth.NTCredentials)2 HttpClient (org.apache.http.client.HttpClient)2 SocketConfig (org.apache.http.config.SocketConfig)2 NoConnectionReuseStrategy (org.apache.http.impl.NoConnectionReuseStrategy)2 AbstractHttpClient (org.apache.http.impl.client.AbstractHttpClient)2 DefaultConnectionKeepAliveStrategy (org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy)2