Example usage for org.apache.http.impl.client AutoRetryHttpClient AutoRetryHttpClient

List of usage examples for org.apache.http.impl.client AutoRetryHttpClient AutoRetryHttpClient

Introduction

In this page you can find the example usage for org.apache.http.impl.client AutoRetryHttpClient AutoRetryHttpClient.

Prototype

public AutoRetryHttpClient(final HttpClient client, final ServiceUnavailableRetryStrategy retryStrategy) 

Source Link

Usage

From source file:com.centurylink.cloud.sdk.core.client.SdkClientBuilder.java

protected ClientHttpEngine initDefaultEngine() {
    HttpClient httpClient;//from  w w  w. j  a  v  a 2 s. co m

    X509HostnameVerifier verifier = initHostnameVerifier();
    try {
        org.apache.http.conn.ssl.SSLSocketFactory sslsf;
        SSLContext theContext = sslContext;
        if (disableTrustManager) {
            theContext = SSLContext.getInstance("SSL");
            theContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, new SecureRandom());
            verifier = new AllowAllHostnameVerifier();
            sslsf = new org.apache.http.conn.ssl.SSLSocketFactory(theContext, verifier);
        } else if (theContext != null) {
            sslsf = new org.apache.http.conn.ssl.SSLSocketFactory(theContext, verifier);
        } else if (clientKeyStore != null || truststore != null) {
            sslsf = new org.apache.http.conn.ssl.SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory.TLS,
                    clientKeyStore, clientPrivateKeyPassword, truststore, null, verifier);
        } else {
            final SSLContext tlsContext = SSLContext.getInstance(org.apache.http.conn.ssl.SSLSocketFactory.TLS);
            tlsContext.init(null, null, null);
            sslsf = new org.apache.http.conn.ssl.SSLSocketFactory(tlsContext, verifier);
        }

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        Scheme httpsScheme = new Scheme("https", 443, sslsf);
        registry.register(httpsScheme);

        httpClient = new AutoRetryHttpClient(
                new DefaultHttpClient(initClientConnectionManager(registry), initHttpParams()) {
                    {
                        if (proxyCredentials != null) {
                            setCredentialsProvider(new BasicCredentialsProvider() {
                                {
                                    setCredentials(
                                            new AuthScope(defaultProxy.getHostName(), defaultProxy.getPort()),
                                            proxyCredentials);
                                }
                            });
                        }
                    }
                }, new ClcRetryStrategy(3, 1000));

        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient, true);
        engine.setResponseBufferSize(responseBufferSize);
        engine.setHostnameVerifier(verifier);
        // this may be null.  We can't really support this with Apache Client.
        engine.setSslContext(theContext);
        engine.setDefaultProxy(defaultProxy);
        return engine;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}