Example usage for org.apache.commons.httpclient.params HttpConnectionParams CONNECTION_TIMEOUT

List of usage examples for org.apache.commons.httpclient.params HttpConnectionParams CONNECTION_TIMEOUT

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpConnectionParams CONNECTION_TIMEOUT.

Prototype

String CONNECTION_TIMEOUT

To view the source code for org.apache.commons.httpclient.params HttpConnectionParams CONNECTION_TIMEOUT.

Click Source Link

Usage

From source file:com.bigdata.rdf.sail.remoting.GraphRepositoryClient.java

private HttpClient getHttpClient() {
    if (httpClient == null) {
        httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());

        httpClient.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(300000));
        // httpClient.getParams().setParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT,
        // new Integer(300000));
        httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new NeverRetryHandler());

        httpClient.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(300000));

        httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, new Integer(300000));
        httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, new Integer(300000));
    }/*ww w .  j  a v  a2 s.  c  o m*/
    if (proxyHost != null) {
        httpClient.getHostConfiguration().setProxyHost(proxyHost);
    }
    return httpClient;
}

From source file:com.legstar.http.client.CicsHttp.java

/**
 * Create a reusable HTTP Client with a set of parameters that match
 * the remote CICS Server characteristics. At this stage, the HTTPClient is not
 * associated with a state and a method yet.
 * @param cicsHttpEndpoint the connection configuration
 * @return the new HTTP Client/* w  ww.  j a va 2 s  .c  o m*/
 */
protected HttpClient createHttpClient(final CicsHttpEndpoint cicsHttpEndpoint) {

    if (_log.isDebugEnabled()) {
        _log.debug("enter createHttpClient()");
    }
    HttpClientParams params = new HttpClientParams();

    /* Consider that if server is failing to respond, we should never retry.
     * A system such as CICS should always be responsive unless something
     * bad is happening in which case, retry makes things worse. */
    DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(0, false);
    params.setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler);

    /* Preemptive authentication forces the first HTTP payload to hold
     * credentials. This bypasses the inefficient default mechanism that
     * expects a 401 reply on the first request and then re-issues the same
     * request again with credentials.*/
    params.setAuthenticationPreemptive(true);

    /* Set the receive time out. */
    params.setSoTimeout(cicsHttpEndpoint.getReceiveTimeout());

    /* Tell the connection manager how long we are prepared to wait
     * for a connection. */
    params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, cicsHttpEndpoint.getConnectTimeout());

    /* Disable Nagle algorithm */
    params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true);

    HttpClient httpClient = new HttpClient(params);

    httpClient.setHostConfiguration(createHostConfiguration(cicsHttpEndpoint));

    return httpClient;
}

From source file:com.liferay.portal.util.HttpImpl.java

public HostConfiguration getHostConfiguration(String location) throws IOException {

    if (_log.isDebugEnabled()) {
        _log.debug("Location is " + location);
    }/* w w w .  j a  va2 s  . co m*/

    HostConfiguration hostConfiguration = new HostConfiguration();

    hostConfiguration.setHost(new URI(location, false));

    if (isProxyHost(hostConfiguration.getHost())) {
        hostConfiguration.setProxy(_PROXY_HOST, _PROXY_PORT);
    }

    HttpConnectionManager httpConnectionManager = _httpClient.getHttpConnectionManager();

    HttpConnectionManagerParams httpConnectionManagerParams = httpConnectionManager.getParams();

    int defaultMaxConnectionsPerHost = httpConnectionManagerParams.getMaxConnectionsPerHost(hostConfiguration);

    int maxConnectionsPerHost = GetterUtil.getInteger(PropsUtil.get(
            HttpImpl.class.getName() + ".max.connections.per.host", new Filter(hostConfiguration.getHost())));

    if ((maxConnectionsPerHost > 0) && (maxConnectionsPerHost != defaultMaxConnectionsPerHost)) {

        httpConnectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, maxConnectionsPerHost);
    }

    int timeout = GetterUtil.getInteger(
            PropsUtil.get(HttpImpl.class.getName() + ".timeout", new Filter(hostConfiguration.getHost())));

    if (timeout > 0) {
        HostParams hostParams = hostConfiguration.getParams();

        hostParams.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, timeout);
        hostParams.setIntParameter(HttpConnectionParams.SO_TIMEOUT, timeout);
    }

    return hostConfiguration;
}

From source file:com.alfaariss.oa.authentication.remote.AbstractRemoteMethod.java

private void readHTTPConfig(Element eConfig) throws OAException {
    //connection_timeout
    //The timeout until a connection is established. 
    //A value of zero means the timeout is not used.
    String sConnectionTimeout = _configurationManager.getParam(eConfig, "connection_timeout");
    if (sConnectionTimeout == null) {
        _logger.info("No 'connection_timeout' parameter found in configuration, using default");
    } else {/*from  w w  w  .  jav  a  2 s . co  m*/
        try {
            int iConnectionTimeout = Integer.parseInt(sConnectionTimeout);

            _httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT,
                    new Integer(iConnectionTimeout));
        } catch (NumberFormatException e) {
            _logger.error("Invalid 'connection_timeout' parameter found in configuration, not a number: "
                    + sConnectionTimeout, e);
            throw new OAException(SystemErrors.ERROR_INIT);
        }
    }

    //socket_timeout
    //The parameters below are optional parameters used by the apache <code>HttpClient</code>.
    //Whenever a parameter is left undefined (no value is explicitly set anywhere in the 
    //parameter hierarchy) <code>HttpClient</code> will use its best judgment to pick up a value. 
    //This default behavior is likely to provide the best compatibility with widely used HTTP servers. 
    //The default socket timeout (SO_TIMEOUT) in milliseconds which is 
    //the timeout for waiting for data. A timeout value of zero is interpreted
    //as an infinite timeout. This value is used when no socket timeout is 
    //set in the HTTP method parameters.  
    String sSocketTimeout = _configurationManager.getParam(eConfig, "socket_timeout");
    if (sSocketTimeout == null) {
        _logger.info("No 'socket_timeout' parameter found in configuration, using an infinite timeout");
    } else {
        try {
            int iSocketTimeout = Integer.parseInt(sSocketTimeout);

            _httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, new Integer(iSocketTimeout));
        } catch (NumberFormatException e) {
            _logger.error("Invalid 'socket_timeout' parameter found in configuration, not a number: "
                    + sSocketTimeout, e);
            throw new OAException(SystemErrors.ERROR_INIT);
        }
    }
}

From source file:com.alfaariss.oa.authentication.remote.aselect.logout.LogoutManager.java

private void readHTTPConfig(IConfigurationManager configurationManager, Element config) throws OAException {
    //connection_timeout
    //The timeout until a connection is established. 
    //A value of zero means the timeout is not used.
    String sConnectionTimeout = configurationManager.getParam(config, "connection_timeout");
    if (sConnectionTimeout == null) {
        _logger.info("No 'connection_timeout' parameter found in configuration, using default");
    } else {/*w  ww . j  ava  2s. co m*/
        try {
            int iConnectionTimeout = Integer.parseInt(sConnectionTimeout);

            _httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT,
                    new Integer(iConnectionTimeout));
        } catch (NumberFormatException e) {
            _logger.error("Invalid 'connection_timeout' parameter found in configuration, not a number: "
                    + sConnectionTimeout, e);
            throw new OAException(SystemErrors.ERROR_INIT);
        }
    }

    //socket_timeout
    //The parameters below are optional parameters used by the apache <code>HttpClient</code>.
    //Whenever a parameter is left undefined (no value is explicitly set anywhere in the 
    //parameter hierarchy) <code>HttpClient</code> will use its best judgment to pick up a value. 
    //This default behavior is likely to provide the best compatibility with widely used HTTP servers. 
    //The default socket timeout (SO_TIMEOUT) in milliseconds which is 
    //the timeout for waiting for data. A timeout value of zero is interpreted
    //as an infinite timeout. This value is used when no socket timeout is 
    //set in the HTTP method parameters.  
    String sSocketTimeout = configurationManager.getParam(config, "socket_timeout");
    if (sSocketTimeout == null) {
        _logger.info("No 'socket_timeout' parameter found in configuration, using an infinite timeout");
    } else {
        try {
            int iSocketTimeout = Integer.parseInt(sSocketTimeout);

            _httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, new Integer(iSocketTimeout));
        } catch (NumberFormatException e) {
            _logger.error("Invalid 'socket_timeout' parameter found in configuration, not a number: "
                    + sSocketTimeout, e);
            throw new OAException(SystemErrors.ERROR_INIT);
        }
    }
}

From source file:com.legstar.http.client.CicsHttp.java

/** (non-Javadoc).
 * @see com.legstar.messaging.LegStarConnection#getConnectTimeout()
 * {@inheritDoc}//from   w  ww  .  j a  v  a  2s. c o m
 */
public long getConnectTimeout() {
    return getHttpClient().getParams().getIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, -1);
}

From source file:com.eucalyptus.imaging.backend.ImagingTaskStateManager.java

private boolean doesManifestExist(final String manifestUrl) throws Exception {
    // validate urls per EUCA-9144
    final UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isEucalyptusUrl(manifestUrl))
        throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + manifestUrl);
    final HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
    client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000);
    client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 30000);
    GetMethod method = new GetMethod(manifestUrl);
    String manifest = null;//ww  w  . j av a 2  s .c  o m
    try {
        // avoid TCP's CLOSE_WAIT  
        method.setRequestHeader("Connection", "close");
        client.executeMethod(method);
        manifest = method.getResponseBodyAsString(ImageConfiguration.getInstance().getMaxManifestSizeBytes());
        if (manifest == null) {
            return false;
        } else if (manifest.contains("<Code>NoSuchKey</Code>")
                || manifest.contains("The specified key does not exist")) {
            return false;
        }
    } catch (final Exception ex) {
        return false;
    } finally {
        method.releaseConnection();
    }
    final List<String> partsUrls = getPartsHeadUrl(manifest);
    for (final String url : partsUrls) {
        if (!urlValidator.isEucalyptusUrl(url))
            throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + url);
        HeadMethod partCheck = new HeadMethod(url);
        int res = client.executeMethod(partCheck);
        if (res != HttpStatus.SC_OK) {
            return false;
        }
    }
    return true;
}

From source file:com.eucalyptus.imaging.ImagingTaskStateManager.java

private boolean doesManifestExist(final String manifestUrl) throws Exception {
    // validate urls per EUCA-9144
    final UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isEucalyptusUrl(manifestUrl))
        throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + manifestUrl);
    final HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
    client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000);
    client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 30000);
    GetMethod method = new GetMethod(manifestUrl);
    String manifest = null;/*  w  w w  . j a v a2 s  . c om*/
    try {
        // avoid TCP's CLOSE_WAIT  
        method.setRequestHeader("Connection", "close");
        client.executeMethod(method);
        manifest = method.getResponseBodyAsString();
        if (manifest == null) {
            return false;
        } else if (manifest.contains("<Code>NoSuchKey</Code>")
                || manifest.contains("The specified key does not exist")) {
            return false;
        }
    } catch (final Exception ex) {
        return false;
    } finally {
        method.releaseConnection();
    }
    final List<String> partsUrls = getPartsHeadUrl(manifest);
    for (final String url : partsUrls) {
        if (!urlValidator.isEucalyptusUrl(url))
            throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + url);
        HeadMethod partCheck = new HeadMethod(url);
        int res = client.executeMethod(partCheck);
        if (res != HttpStatus.SC_OK) {
            return false;
        }
    }
    return true;
}

From source file:com.twinsoft.convertigo.beans.connectors.HttpConnector.java

protected int doExecuteMethod(final HttpMethod method, Context context)
        throws ConnectionException, URIException, MalformedURLException {
    int statuscode = -1;

    // Tells the method to automatically handle authentication.
    method.setDoAuthentication(true);//from w w w .j  a  v  a 2s. c o m

    // Tells the method to automatically handle redirection.
    method.setFollowRedirects(false);

    HttpPool httpPool = ((AbstractHttpTransaction) context.transaction).getHttpPool();
    HttpClient httpClient = context.getHttpClient3(httpPool);

    try {
        // Display the cookies
        if (handleCookie) {
            Cookie[] cookies = httpState.getCookies();
            if (Engine.logBeans.isTraceEnabled())
                Engine.logBeans.trace(
                        "(HttpConnector) HttpClient request cookies:" + Arrays.asList(cookies).toString());
        }

        forwardHeader(new HeaderForwarder() {
            public void add(String name, String value, String forwardPolicy) {
                if (HttpConnector.HTTP_HEADER_FORWARD_POLICY_IGNORE.equals(forwardPolicy)) {
                    Header exHeader = method.getRequestHeader(name);
                    if (exHeader != null) {
                        // Nothing to do
                        Engine.logEngine.debug("(WebViewer) Forwarding header '" + name
                                + "' has been ignored due to forward policy");
                    } else {
                        method.setRequestHeader(name, value);
                        Engine.logEngine.debug("(WebViewer) Header forwarded and added: " + name + "=" + value);
                    }
                } else if (HttpConnector.HTTP_HEADER_FORWARD_POLICY_REPLACE.equals(forwardPolicy)) {
                    method.setRequestHeader(name, value);
                    Engine.logEngine.debug("(WebViewer) Header forwarded and replaced: " + name + "=" + value);
                } else if (HttpConnector.HTTP_HEADER_FORWARD_POLICY_MERGE.equals(forwardPolicy)) {
                    Header exHeader = method.getRequestHeader(name);
                    if (exHeader != null)
                        value = exHeader.getValue() + ", " + value;

                    method.setRequestHeader(name, value);
                    Engine.logEngine.debug("(WebViewer) Header forwarded and merged: " + name + "=" + value);
                }
            }
        });

        // handle oAuthSignatures if any
        if (oAuthKey != null && oAuthSecret != null && oAuthToken != null && oAuthTokenSecret != null) {
            oAuthConsumer = new HttpOAuthConsumer(oAuthKey, oAuthSecret, hostConfiguration);
            oAuthConsumer.setTokenWithSecret(oAuthToken, oAuthTokenSecret);
            oAuthConsumer.sign(method);

            oAuthConsumer = null;
        }

        HttpUtils.logCurrentHttpConnection(httpClient, hostConfiguration, httpPool);

        hostConfiguration.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,
                (int) context.requestedObject.getResponseTimeout() * 1000);
        hostConfiguration.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT,
                (int) context.requestedObject.getResponseTimeout() * 1000);

        Engine.logBeans.debug("(HttpConnector) HttpClient: executing method...");
        statuscode = httpClient.executeMethod(hostConfiguration, method, httpState);
        Engine.logBeans.debug("(HttpConnector) HttpClient: end of method successfull");

        // Display the cookies
        if (handleCookie) {
            Cookie[] cookies = httpState.getCookies();
            if (Engine.logBeans.isTraceEnabled())
                Engine.logBeans.trace(
                        "(HttpConnector) HttpClient response cookies:" + Arrays.asList(cookies).toString());
        }
    } catch (SocketTimeoutException e) {
        throw new ConnectionException(
                "Timeout reached (" + context.requestedObject.getResponseTimeout() + " sec)");
    } catch (IOException e) {
        if (!context.requestedObject.runningThread.bContinue)
            return statuscode;

        try {
            HttpUtils.logCurrentHttpConnection(httpClient, hostConfiguration, httpPool);
            Engine.logBeans.warn("(HttpConnector) HttpClient: connection error to " + sUrl + ": "
                    + e.getMessage() + "; retrying method");
            statuscode = httpClient.executeMethod(hostConfiguration, method, httpState);
            Engine.logBeans.debug("(HttpConnector) HttpClient: end of method successfull");
        } catch (IOException ee) {
            throw new ConnectionException("Connection error to " + sUrl, ee);
        }
    } catch (OAuthException eee) {
        throw new ConnectionException("OAuth Connection error to " + sUrl, eee);
    }
    return statuscode;
}

From source file:org.apache.abdera.protocol.client.AbderaClient.java

/**
 * Sets the timeout until a connection is etablished. A value of zero means the timeout is not used. The default
 * value is zero./*from   w ww  .j  a va  2  s. c o  m*/
 */
public AbderaClient setConnectionTimeout(int timeout) {
    client.getHttpConnectionManager().getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT,
            timeout);
    return this;
}