Example usage for org.apache.commons.httpclient HttpMethodRetryHandler HttpMethodRetryHandler

List of usage examples for org.apache.commons.httpclient HttpMethodRetryHandler HttpMethodRetryHandler

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodRetryHandler HttpMethodRetryHandler.

Prototype

HttpMethodRetryHandler

Source Link

Usage

From source file:com.cloud.utils.net.HTTPUtils.java

/**
 * @return A HttpMethodRetryHandler with given number of retries.
 *//*w ww.  j a va 2 s  . c  om*/
public static HttpMethodRetryHandler getHttpMethodRetryHandler(final int retryCount) {

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Initializing new HttpMethodRetryHandler with retry count " + retryCount);
    }

    return new HttpMethodRetryHandler() {
        @Override
        public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
            if (executionCount >= retryCount) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (!method.isRequestSent()) {
                // Retry if the request has not been sent fully or
                // if it's OK to retry methods that have been sent
                return true;
            }
            // otherwise do not retry
            return false;
        }
    };
}

From source file:com.predic8.membrane.integration.Http11Test.java

/**
 * Note that "Read timed out" indicates incorrect server behavior. The
 * socket timeout is set on the client to avoid fallback mentioned in
 * RFC2616 section 8.2.3 ("indefinite period").
 *//*  w  w w.ja  v a 2 s . c om*/
public static void initExpect100ContinueWithFastFail(HttpClient client) {
    client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    client.getParams().setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true);
    client.getParams().setParameter("http.method.retry-handler", new HttpMethodRetryHandler() {
        @Override
        public boolean retryMethod(HttpMethod arg0, IOException arg1, int arg2) {
            return false;
        }
    });
    client.getParams().setParameter("http.socket.timeout", 7000);
}

From source file:com.cloud.storage.template.MetalinkTemplateDownloader.java

protected HttpMethodRetryHandler createRetryTwiceHandler() {
    return new HttpMethodRetryHandler() {
        @Override/*from  w w w . j  a va  2  s  . c  om*/
        public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
            if (executionCount >= 2) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (!method.isRequestSent()) {
                // Retry if the request has not been sent fully or
                // if it's OK to retry methods that have been sent
                return true;
            }
            // otherwise do not retry
            return false;
        }
    };
}

From source file:com.cloud.storage.template.HttpTemplateDownloader.java

public HttpTemplateDownloader(StorageLayer storageLayer, String downloadUrl, String toDir,
        DownloadCompleteCallback callback, long maxTemplateSizeInBytes, String user, String password,
        Proxy proxy, ResourceType resourceType) {
    this._storage = storageLayer;
    this.downloadUrl = downloadUrl;
    this.setToDir(toDir);
    this.status = TemplateDownloader.Status.NOT_STARTED;
    this.resourceType = resourceType;
    this.MAX_TEMPLATE_SIZE_IN_BYTES = maxTemplateSizeInBytes;

    this.totalBytes = 0;
    this.client = new HttpClient(s_httpClientManager);

    myretryhandler = new HttpMethodRetryHandler() {
        public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
            if (executionCount >= 2) {
                // Do not retry if over max retry count
                return false;
            }/* w  ww. j  a  va  2s.  c  o  m*/
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (!method.isRequestSent()) {
                // Retry if the request has not been sent fully or
                // if it's OK to retry methods that have been sent
                return true;
            }
            // otherwise do not retry
            return false;
        }
    };

    try {
        this.request = new GetMethod(downloadUrl);
        this.request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
        this.completionCallback = callback;
        //this.request.setFollowRedirects(false);

        File f = File.createTempFile("dnld", "tmp_", new File(toDir));

        if (_storage != null) {
            _storage.setWorldReadableAndWriteable(f);
        }

        toFile = f.getAbsolutePath();
        Pair<String, Integer> hostAndPort = validateUrl(downloadUrl);

        if (proxy != null) {
            client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort());
            if (proxy.getUserName() != null) {
                Credentials proxyCreds = new UsernamePasswordCredentials(proxy.getUserName(),
                        proxy.getPassword());
                client.getState().setProxyCredentials(AuthScope.ANY, proxyCreds);
            }
        }
        if ((user != null) && (password != null)) {
            client.getParams().setAuthenticationPreemptive(true);
            Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
            client.getState().setCredentials(
                    new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM),
                    defaultcreds);
            s_logger.info("Added username=" + user + ", password=" + password + "for host "
                    + hostAndPort.first() + ":" + hostAndPort.second());
        } else {
            s_logger.info(
                    "No credentials configured for host=" + hostAndPort.first() + ":" + hostAndPort.second());
        }
    } catch (IllegalArgumentException iae) {
        errorString = iae.getMessage();
        status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
        inited = false;
    } catch (Exception ex) {
        errorString = "Unable to start download -- check url? ";
        status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
        s_logger.warn("Exception in constructor -- " + ex.toString());
    } catch (Throwable th) {
        s_logger.warn("throwable caught ", th);
    }
}

From source file:edu.utah.further.core.ws.HttpClientTemplate.java

/**
 * Private {@link HttpClient} initialization.
 *//*from   ww w . j  a v a  2s  .c o m*/
@PostConstruct
private final void afterPropertiesSet() {
    // Client is higher in the hierarchy than manager so set the parameters here
    final HttpClientParams clientParams = new HttpClientParams();
    clientParams.setConnectionManagerClass(connectionManager.getClass());
    clientParams.setConnectionManagerTimeout(connectionTimeout);
    clientParams.setSoTimeout(readTimeout);
    clientParams.setParameter("http.connection.timeout", new Integer(connectionTimeout));
    // A retry handler for when a connection fails
    clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {
        @Override
        public boolean retryMethod(final HttpMethod method, final IOException exception,
                final int executionCount) {
            if (executionCount >= retryCount) {
                // Do not retry if over max retry count
                return false;
            }
            if (instanceOf(exception, NoHttpResponseException.class)) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (instanceOf(exception, SocketException.class)) {
                // Retry if the server reset connection on us
                return true;
            }
            if (instanceOf(exception, SocketTimeoutException.class)) {
                // Retry if the read timed out
                return true;
            }
            if (!method.isRequestSent()) {
                // Retry if the request has not been sent fully or
                // if it's OK to retry methods that have been sent
                return true;
            }
            // otherwise do not retry
            return false;
        }
    });
    httpClient.setParams(clientParams);

    final HttpConnectionManagerParams connectionManagerParams = connectionManager.getParams();
    connectionManagerParams.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);
    connectionManager.setParams(connectionManagerParams);
}

From source file:ac.elements.io.Signature.java

/**
 * Configure HttpClient with set of defaults as well as configuration from
 * AmazonEC2Config instance.//  w  w w  .j a v  a2  s  .c o m
 * 
 * @return the http client
 */
private static HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
    httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > MAX_RETRY_ERROR) {
                log.warn("Maximum Number of Retry attempts " + "reached, will not retry");
                return false;
            }
            log.warn("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.warn("Retrying on NoHttpResponseException");
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                log.warn("Will not retry on InterruptedIOException", exception);
                return false;
            }
            if (exception instanceof UnknownHostException) {
                log.warn("Will not retry on UnknownHostException", exception);
                return false;
            }
            if (!method.isRequestSent()) {
                log.warn("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(MAX_CONNECTIONS);
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, MAX_CONNECTIONS);

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    // if (config.isSetProxyHost() && config.isSetProxyPort()) {
    // log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() +
    // "Proxy Port: " + config.getProxyPort() );
    // hostConfiguration.setProxy(config.getProxyHost(),
    // config.getProxyPort());
    // if (config.isSetProxyUsername() && config.isSetProxyPassword()) {
    // httpClient.getState().setProxyCredentials (new AuthScope(
    // config.getProxyHost(),
    // config.getProxyPort()),
    // new UsernamePasswordCredentials(
    // config.getProxyUsername(),
    // config.getProxyPassword()));
    //        
    // }
    // }
    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:com.amazonaws.elasticmapreduce.AmazonElasticMapReduceClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration
 * from AmazonElasticMapReduceConfig instance
 *
 *///from www .  j a  v  a2 s  .c o m
private HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, config.getUserAgent());
    httpClientParams.setParameter(HttpClientParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > 3) {
                log.debug("Maximum Number of Retry attempts reached, will not retry");
                return false;
            }
            log.debug("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.debug("Retrying on NoHttpResponseException");
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                log.debug("Will not retry on InterruptedIOException", exception);
                return false;
            }
            if (exception instanceof UnknownHostException) {
                log.debug("Will not retry on UnknownHostException", exception);
                return false;
            }
            if (!method.isRequestSent()) {
                log.debug("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(config.getMaxConnections());
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, config.getMaxConnections());

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    if (config.isSetProxyHost() && config.isSetProxyPort()) {
        log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: "
                + config.getProxyPort());
        hostConfiguration.setProxy(config.getProxyHost(), config.getProxyPort());
        if (config.isSetProxyUsername() && config.isSetProxyPassword()) {
            httpClient.getState().setProxyCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));

        }
    }

    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:com.amazonaws.a2s.AmazonA2SClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration
 * from AmazonA2SConfig instance//w w w .j  a  v  a2s . co  m
 *
 */
private HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, config.getUserAgent());
    httpClientParams.setParameter(HttpClientParams.RETRY_HANDLER, new HttpMethodRetryHandler() {
        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > 3) {
                log.debug("Maximum Number of Retry attempts reached, will not retry");
                return false;
            }
            log.debug("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.debug("Retrying on NoHttpResponseException");
                return true;
            }
            if (!method.isRequestSent()) {
                log.debug("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(3);
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, 3);

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    if (config.isSetProxyHost() && config.isSetProxyPort()) {
        log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: "
                + config.getProxyPort());
        hostConfiguration.setProxy(config.getProxyHost(), config.getProxyPort());

    }

    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:open.hyperion.nimblestorage.connection.NimbleStorageAPIFactory.java

/**
 * Initialize HTTP client// w  w w .ja  va2s.  c  o  m
 */
public void init() {
    _log.info("NimbleStorageDriver:NimbleStorageAPIFactory init enter");
    _clientMap = new ConcurrentHashMap<String, NimbleStorageAPI>();

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(_maxConnPerHost);
    params.setMaxTotalConnections(_maxConn);
    params.setTcpNoDelay(true);
    params.setConnectionTimeout(_connTimeout);
    params.setSoTimeout(_socketConnTimeout);

    _connectionManager = new MultiThreadedHttpConnectionManager();
    _connectionManager.setParams(params);
    _connectionManager.closeIdleConnections(0); // close idle connections immediately

    HttpClient client = new HttpClient(_connectionManager);
    client.getParams().setConnectionManagerTimeout(_connManagerTimeout);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {
        @Override
        public boolean retryMethod(HttpMethod httpMethod, IOException e, int i) {
            return false;
        }
    });

    Protocol.registerProtocol("https", new Protocol("https", new NonValidatingSocketFactory(), 8080));
}

From source file:open.hyperion.purestorage.connection.PureStorageAPIFactory.java

/**
 * Initialize HTTP client//from  w  ww  .  ja v  a  2  s.  c o  m
 */
public void init() {
    _log.info("PureStorageDriver:PureStorageAPIFactory init enter");
    _clientMap = new ConcurrentHashMap<String, PureStorageAPI>();

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(_maxConnPerHost);
    params.setMaxTotalConnections(_maxConn);
    params.setTcpNoDelay(true);
    params.setConnectionTimeout(_connTimeout);
    params.setSoTimeout(_socketConnTimeout);

    _connectionManager = new MultiThreadedHttpConnectionManager();
    _connectionManager.setParams(params);
    _connectionManager.closeIdleConnections(0); // close idle connections immediately

    HttpClient client = new HttpClient(_connectionManager);
    client.getParams().setConnectionManagerTimeout(_connManagerTimeout);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {
        @Override
        public boolean retryMethod(HttpMethod httpMethod, IOException e, int i) {
            return false;
        }
    });

    Protocol.registerProtocol("https", new Protocol("https", new NonValidatingSocketFactory(), 8080));
}