Example usage for org.apache.commons.httpclient HttpMethod isRequestSent

List of usage examples for org.apache.commons.httpclient HttpMethod isRequestSent

Introduction

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

Prototype

public abstract boolean isRequestSent();

Source Link

Usage

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

/**
 * @return A HttpMethodRetryHandler with given number of retries.
 *///from  w  w w  . j ava 2 s. co  m
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.cloud.storage.template.MetalinkTemplateDownloader.java

protected HttpMethodRetryHandler createRetryTwiceHandler() {
    return new HttpMethodRetryHandler() {
        @Override/*from ww  w .j  av a  2s.c  o m*/
        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:ac.elements.io.Signature.java

/**
 * Configure HttpClient with set of defaults as well as configuration from
 * AmazonEC2Config instance./*from  w ww.j  a va  2 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.cyberway.issue.crawler.fetcher.HeritrixHttpMethodRetryHandler.java

public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
    if (exception instanceof SocketTimeoutException) {
        // already waited for the configured amount of time with no reply; 
        // do not retry further until next go round
        return false;
    }/*  w w  w.  j  a v  a 2s. c om*/
    if (executionCount >= this.maxRetryCount) {
        // 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() && (!(method instanceof PostMethod))) {
        // 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;
            }/*from w  w  w . ja  v  a 2  s . com*/
            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.
 */// www.  j  av a2  s  . co  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:com.amazonaws.a2s.AmazonA2SClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration
 * from AmazonA2SConfig instance/*from  www.j  a v a 2s. com*/
 *
 */
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:com.amazonaws.elasticmapreduce.AmazonElasticMapReduceClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration
 * from AmazonElasticMapReduceConfig instance
 *
 *//*from w w  w .j  a v  a 2s.  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.yufei.dataget.utils.BaiheHtmlClientRetryHandler.java

@Override
public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
    String targetServer = method.getHostConfiguration().getHost();
    if (executionCount > maxRetryCount) {
        // Do not retry if over max retry count
        return false;
    }//from  w  ww . ja va2  s.  co m
    if (exception instanceof NoHttpResponseException) {
        // Retry if the server dropped connection on us
        mLog.info("Exception that can not connect to the target server " + targetServer
                + " for it too busy  to accept current connecton!");
        return true;
    }
    if (exception instanceof SocketTimeoutException) {
        // Retry if the client  unable to establish a connection with the target server or proxy server within the given period of time.
        mLog.info("Expection that can not get response from  target server " + targetServer
                + " within fixed amount time! ");
        return true;
    }
    //        if (exception instanceof ConnectionPoolTimeoutException) {
    //            // Retry if the client  fails to obtain a free connection from the connection pool within the given period of time.
    //            mLog.info("Exception that can not obtain a free connection from current connection pool, current host is: " + targetServer + ", please check the connection config for this host! ");
    //            return true;
    //        }
    if (exception instanceof ConnectTimeoutException) {
        // Retry if the client  unable to establish a connection with the target server or proxy server within the given period of time.
        mLog.info("Expection that can not establish a connection with the target server " + targetServer
                + " within fixed amount time! ");
        return true;
    }

    if (exception instanceof ProtocolException) {
        mLog.info("Exception that violation of the HTTP specification against the target server: "
                + targetServer + "!");
        return false;

    }
    if (exception instanceof UnknownHostException) {
        mLog.info("UnknowHost:" + targetServer + "");
        return false;

    }
    if (exception instanceof ConnectException) {
        mLog.info(targetServer + " maybe have shutdown!");
        return false;

    }

    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:org.apache.abdera.protocol.client.CommonsResponse.java

protected CommonsResponse(Abdera abdera, HttpMethod method) {
    super(abdera);
    if (method.isRequestSent())
        this.method = method;
    else/* w  w w.  j a va2  s.  co  m*/
        throw new IllegalStateException();
    parse_cc();
    getServerDate();
}