Example usage for org.apache.http.client HttpRequestRetryHandler HttpRequestRetryHandler

List of usage examples for org.apache.http.client HttpRequestRetryHandler HttpRequestRetryHandler

Introduction

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

Prototype

HttpRequestRetryHandler

Source Link

Usage

From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java

/**
 * ??<br>// w w  w  . j ava 2 s.  c o m
 * ?????
 * 
 * @throws Exception
 */
@Test
public void testHttpRetry() throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    // ?Handler
    HttpRequestRetryHandler mRequestRetryHandler = new HttpRequestRetryHandler() {

        // true? ?
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (executionCount > 5) {
                // ???
                System.out.println(".....");
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // ??
                return true;
            }
            if (exception instanceof SSLHandshakeException) {
                // ???SSL?
                return false;
            }

            System.out.println("executionCount ==>" + executionCount);

            HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                // ?
                return true;
            }

            return false;
        }
    };

    // Http?
    httpClient.setHttpRequestRetryHandler(mRequestRetryHandler);

    HttpGet httpGet = new HttpGet(new URI(ERR_URL1));

    HttpResponse httpResponse = httpClient.execute(httpGet);

    String content = parseEntity(httpResponse.getEntity());

    System.out.println(content);

}

From source file:com.wudaosoft.net.httpclient.Request.java

protected void init() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException,
        CertificateException, IOException {

    Args.notNull(hostConfig, "Host config");

    SSLConnectionSocketFactory sslConnectionSocketFactory = null;

    if (sslcontext == null) {

        if (hostConfig.getCA() != null) {
            // Trust root CA and all self-signed certs
            SSLContext sslcontext1 = SSLContexts.custom().loadTrustMaterial(hostConfig.getCA(),
                    hostConfig.getCAPassword(), TrustSelfSignedStrategy.INSTANCE).build();

            // Allow TLSv1 protocol only
            sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext1, new String[] { "TLSv1" },
                    null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        } else {/*w  w w.  j av  a 2s  . c o m*/

            if (isTrustAll) {

                SSLContext sslcontext1 = SSLContext.getInstance("TLS");

                TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }

                    @Override
                    public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                            throws CertificateException {

                    }

                    @Override
                    public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                            throws CertificateException {
                    }

                } };

                sslcontext1.init(null, trustAllCerts, null);

                sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext1,
                        NoopHostnameVerifier.INSTANCE);
            } else {
                sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
            }
        }
    } else {

        sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    }

    if (keepAliveStrategy == null) {
        keepAliveStrategy = new ConnectionKeepAliveStrategy() {

            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                // Honor 'keep-alive' header
                HeaderElementIterator it = new BasicHeaderElementIterator(
                        response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                while (it.hasNext()) {
                    HeaderElement he = it.nextElement();
                    String param = he.getName();
                    String value = he.getValue();
                    if (value != null && param.equalsIgnoreCase("timeout")) {
                        try {
                            return Long.parseLong(value) * 1000;
                        } catch (NumberFormatException ignore) {
                        }
                    }
                }
                // HttpHost target = (HttpHost)
                // context.getAttribute(HttpClientContext.HTTP_TARGET_HOST);
                // if
                // ("xxxxx".equalsIgnoreCase(target.getHostName()))
                // {
                // // Keep alive for 5 seconds only
                // return 3 * 1000;
                // } else {
                // // otherwise keep alive for 30 seconds
                // return 30 * 1000;
                // }

                return 30 * 1000;
            }

        };
    }

    if (retryHandler == null) {
        retryHandler = new HttpRequestRetryHandler() {

            public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                if (executionCount >= 3) {
                    // Do not retry if over max retry count
                    return false;
                }
                if (exception instanceof InterruptedIOException) {
                    // Timeout
                    return false;
                }
                if (exception instanceof UnknownHostException) {
                    // Unknown host
                    return false;
                }
                if (exception instanceof ConnectTimeoutException) {
                    // Connection refused
                    return false;
                }
                if (exception instanceof SSLException) {
                    // SSL handshake exception
                    return false;
                }
                HttpClientContext clientContext = HttpClientContext.adapt(context);
                HttpRequest request = clientContext.getRequest();
                boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
                if (idempotent) {
                    // Retry if the request is considered idempotent
                    return true;
                }
                return false;
            }
        };
    }

    connManager = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslConnectionSocketFactory).build());

    if (hostConfig.getHost() != null) {

        connManager.setMaxTotal(hostConfig.getPoolSize() + 60);

        connManager.setMaxPerRoute(
                new HttpRoute(hostConfig.getHost(), null,
                        !HttpHost.DEFAULT_SCHEME_NAME.equals(hostConfig.getHost().getSchemeName())),
                hostConfig.getPoolSize());

        connManager.setDefaultMaxPerRoute(20);
    } else {
        connManager.setMaxTotal(hostConfig.getPoolSize());
        int hostCount = hostConfig.getHostCount() == 0 ? 10 : hostConfig.getHostCount();
        connManager.setDefaultMaxPerRoute(hostConfig.getPoolSize() / hostCount);
    }

    // connManager.setValidateAfterInactivity(2000);

    // Create socket configuration
    SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(isKeepAlive).build();
    connManager.setDefaultSocketConfig(socketConfig);

    // Create connection configuration
    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .setCharset(hostConfig.getCharset() == null ? Consts.UTF_8 : hostConfig.getCharset()).build();
    connManager.setDefaultConnectionConfig(connectionConfig);

    new IdleConnectionMonitorThread(connManager).start();

    if (requestInterceptor == null) {
        requestInterceptor = new SortHeadersInterceptor(hostConfig);
    }

    if (!hostConfig.isMulticlient()) {
        defaultHttpContext = HttpClientContext.create();
        httpClient = create();
    }
}

From source file:org.apache.hive.jdbc.HiveConnection.java

private CloseableHttpClient getHttpClient(Boolean useSsl) throws SQLException {
    boolean isCookieEnabled = sessConfMap.get(JdbcConnectionParams.COOKIE_AUTH) == null
            || (!JdbcConnectionParams.COOKIE_AUTH_FALSE
                    .equalsIgnoreCase(sessConfMap.get(JdbcConnectionParams.COOKIE_AUTH)));
    String cookieName = sessConfMap.get(JdbcConnectionParams.COOKIE_NAME) == null
            ? JdbcConnectionParams.DEFAULT_COOKIE_NAMES_HS2
            : sessConfMap.get(JdbcConnectionParams.COOKIE_NAME);
    CookieStore cookieStore = isCookieEnabled ? new BasicCookieStore() : null;
    HttpClientBuilder httpClientBuilder;
    // Request interceptor for any request pre-processing logic
    HttpRequestInterceptor requestInterceptor;
    Map<String, String> additionalHttpHeaders = new HashMap<String, String>();

    // Retrieve the additional HttpHeaders
    for (Map.Entry<String, String> entry : sessConfMap.entrySet()) {
        String key = entry.getKey();

        if (key.startsWith(JdbcConnectionParams.HTTP_HEADER_PREFIX)) {
            additionalHttpHeaders.put(key.substring(JdbcConnectionParams.HTTP_HEADER_PREFIX.length()),
                    entry.getValue());/*from w w  w .  ja  va2 s .  co m*/
        }
    }
    // Configure http client for kerberos/password based authentication
    if (isKerberosAuthMode()) {
        /**
         * Add an interceptor which sets the appropriate header in the request.
         * It does the kerberos authentication and get the final service ticket,
         * for sending to the server before every request.
         * In https mode, the entire information is encrypted
         */
        requestInterceptor = new HttpKerberosRequestInterceptor(
                sessConfMap.get(JdbcConnectionParams.AUTH_PRINCIPAL), host, getServerHttpUrl(useSsl),
                assumeSubject, cookieStore, cookieName, useSsl, additionalHttpHeaders);
    } else {
        // Check for delegation token, if present add it in the header
        String tokenStr = getClientDelegationToken(sessConfMap);
        if (tokenStr != null) {
            requestInterceptor = new HttpTokenAuthInterceptor(tokenStr, cookieStore, cookieName, useSsl,
                    additionalHttpHeaders);
        } else {
            /**
             * Add an interceptor to pass username/password in the header.
             * In https mode, the entire information is encrypted
             */
            requestInterceptor = new HttpBasicAuthInterceptor(getUserName(), getPassword(), cookieStore,
                    cookieName, useSsl, additionalHttpHeaders);
        }
    }
    // Configure http client for cookie based authentication
    if (isCookieEnabled) {
        // Create a http client with a retry mechanism when the server returns a status code of 401.
        httpClientBuilder = HttpClients.custom()
                .setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {
                    @Override
                    public boolean retryRequest(final HttpResponse response, final int executionCount,
                            final HttpContext context) {
                        int statusCode = response.getStatusLine().getStatusCode();
                        boolean ret = statusCode == 401 && executionCount <= 1;

                        // Set the context attribute to true which will be interpreted by the request
                        // interceptor
                        if (ret) {
                            context.setAttribute(Utils.HIVE_SERVER2_RETRY_KEY, Utils.HIVE_SERVER2_RETRY_TRUE);
                        }
                        return ret;
                    }

                    @Override
                    public long getRetryInterval() {
                        // Immediate retry
                        return 0;
                    }
                });
    } else {
        httpClientBuilder = HttpClientBuilder.create();
    }
    // In case the server's idletimeout is set to a lower value, it might close it's side of
    // connection. However we retry one more time on NoHttpResponseException
    httpClientBuilder.setRetryHandler(new HttpRequestRetryHandler() {
        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount > 1) {
                LOG.info("Retry attempts to connect to server exceeded.");
                return false;
            }
            if (exception instanceof org.apache.http.NoHttpResponseException) {
                LOG.info("Could not connect to the server. Retrying one more time.");
                return true;
            }
            return false;
        }
    });

    // Add the request interceptor to the client builder
    httpClientBuilder.addInterceptorFirst(requestInterceptor);

    // Add an interceptor to add in an XSRF header
    httpClientBuilder.addInterceptorLast(new XsrfHttpRequestInterceptor());

    // Configure http client for SSL
    if (useSsl) {
        String useTwoWaySSL = sessConfMap.get(JdbcConnectionParams.USE_TWO_WAY_SSL);
        String sslTrustStorePath = sessConfMap.get(JdbcConnectionParams.SSL_TRUST_STORE);
        String sslTrustStorePassword = sessConfMap.get(JdbcConnectionParams.SSL_TRUST_STORE_PASSWORD);
        KeyStore sslTrustStore;
        SSLConnectionSocketFactory socketFactory;
        SSLContext sslContext;
        /**
         * The code within the try block throws: SSLInitializationException, KeyStoreException,
         * IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException &
         * UnrecoverableKeyException. We don't want the client to retry on any of these,
         * hence we catch all and throw a SQLException.
         */
        try {
            if (useTwoWaySSL != null && useTwoWaySSL.equalsIgnoreCase(JdbcConnectionParams.TRUE)) {
                socketFactory = getTwoWaySSLSocketFactory();
            } else if (sslTrustStorePath == null || sslTrustStorePath.isEmpty()) {
                // Create a default socket factory based on standard JSSE trust material
                socketFactory = SSLConnectionSocketFactory.getSocketFactory();
            } else {
                // Pick trust store config from the given path
                sslTrustStore = KeyStore.getInstance(JdbcConnectionParams.SSL_TRUST_STORE_TYPE);
                try (FileInputStream fis = new FileInputStream(sslTrustStorePath)) {
                    sslTrustStore.load(fis, sslTrustStorePassword.toCharArray());
                }
                sslContext = SSLContexts.custom().loadTrustMaterial(sslTrustStore, null).build();
                socketFactory = new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier(null));
            }
            final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("https", socketFactory).build();
            httpClientBuilder.setConnectionManager(new BasicHttpClientConnectionManager(registry));
        } catch (Exception e) {
            String msg = "Could not create an https connection to " + jdbcUriString + ". " + e.getMessage();
            throw new SQLException(msg, " 08S01", e);
        }
    }
    return httpClientBuilder.build();
}

From source file:org.mule.modules.freshbooks.api.DefaultFreshBooksClient.java

/**
 * Constructor for Authentication Token mechanism
 * //from w w  w  .  j  ava  2 s .  c om
 * @param apiUrl
 *            url for API
 * @param authenticationToken
 *            authentication token value
 * @param maxTotalConnection
 *            max total connections for client
 * @param defaultMaxConnectionPerRoute
 *            default max connection per route for client
 */
public DefaultFreshBooksClient(String apiUrl, String authenticationToken, int maxTotalConnection,
        int defaultMaxConnectionPerRoute) {
    Validate.notEmpty(apiUrl);
    Validate.notEmpty(authenticationToken);
    try {
        this.apiUrl = new URL(apiUrl);
    } catch (MalformedURLException e) {
        throw new FreshBooksException(e.getMessage());
    }

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    // max total connections
    cm.setMaxTotal(maxTotalConnection);
    // default max connection per route
    cm.setDefaultMaxPerRoute(defaultMaxConnectionPerRoute);

    client = new DefaultHttpClient(cm);
    this.client.getCredentialsProvider().setCredentials(
            new AuthScope(this.apiUrl.getHost(), 443, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(authenticationToken, ""));

    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {

        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount > 3) {
                logger.warn("Maximum tries reached for client http pool ");
                return false;
            }
            if (exception instanceof org.apache.http.NoHttpResponseException) {
                logger.warn("No response from server on " + executionCount + " call");
                return true;
            }
            return false;
        }
    });
}

From source file:org.mule.modules.freshbooks.api.DefaultFreshBooksClient.java

/**
 * Constructor for OAuth1.0a mechanism/*from   ww w  . ja  va2s .  c o m*/
 * 
 * @param apiUrl
 *            url for API
 * @param consumerKey
 *            consumerKey value
 * @param consumerSecret
 *            consumerSecret value
 * @param maxTotalConnection
 *            max total connections for client
 * @param defaultMaxConnectionPerRoute
 *            default max connection per route for client
 */
public DefaultFreshBooksClient(String apiUrl, String consumerKey, String consumerSecret, int maxTotalConnection,
        int defaultMaxConnectionPerRoute) {
    Validate.notEmpty(apiUrl);

    try {
        this.apiUrl = new URL(apiUrl);
    } catch (MalformedURLException e) {
        throw new FreshBooksException(e.getMessage());
    }

    this.consumerKey = consumerKey;
    this.consumerSecret = consumerSecret;

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    // max total connections
    cm.setMaxTotal(maxTotalConnection);
    // default max connection per route
    cm.setDefaultMaxPerRoute(defaultMaxConnectionPerRoute);

    client = new DefaultHttpClient(cm);
    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {

        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount > 3) {
                logger.warn("Maximum tries reached for client http pool ");
                return false;
            }
            if (exception instanceof org.apache.http.NoHttpResponseException) {
                logger.warn("No response from server on " + executionCount + " call");
                return true;
            }
            return false;
        }
    });
}

From source file:org.mule.modules.quickbooks.api.AbstractQuickBooksClientOAuth.java

public static DefaultHttpClient getThreadSafeClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams params = client.getParams();

    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);
    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        @Override/*ww w.  ja  v  a 2 s. c  o m*/
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount > 3) {
                LOGGER.warn("Maximum tries reached for client http pool ");
                return false;
            }
            if (exception instanceof org.apache.http.NoHttpResponseException) {
                LOGGER.warn("No response from server on " + executionCount + " call");
                return true;
            }
            return false;
        }
    });

    return client;
}