List of usage examples for org.apache.commons.httpclient.params HttpConnectionParams SO_TIMEOUT
String SO_TIMEOUT
To view the source code for org.apache.commons.httpclient.params HttpConnectionParams SO_TIMEOUT.
Click Source Link
From source file:colt.nicity.performance.agent.LatentHttpPump.java
private org.apache.commons.httpclient.HttpClient createApacheClient(String host, int port, int maxConnections, int socketTimeoutInMillis) { HttpConnectionManager connectionManager = createConnectionManager(maxConnections); org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient( connectionManager);/*from w w w . ja v a 2s. c o m*/ client.getParams().setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.RFC_2109); client.getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); client.getParams().setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true); client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, socketTimeoutInMillis > 0 ? socketTimeoutInMillis : 0); client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, socketTimeoutInMillis > 0 ? socketTimeoutInMillis : 0); HostConfiguration hostConfiguration = new HostConfiguration(); configureSsl(hostConfiguration, host, port); configureProxy(hostConfiguration); client.setHostConfiguration(hostConfiguration); return client; }
From source file:de.innovationgate.wgaservices.xfire.XFireClientFactoryService.java
private static void addDefaultServiceProperties(Map<String, Object> props) { if (!props.containsKey("mtom-enabled")) { props.put("mtom-enabled", "true"); }/*from w w w. j ava2s .c o m*/ if (!props.containsKey(HttpTransport.CHUNKING_ENABLED)) { props.put(HttpTransport.CHUNKING_ENABLED, "true"); } if (!props.containsKey(CommonsHttpMessageSender.HTTP_CLIENT_PARAMS)) { HttpClientParams params = new HttpClientParams(); params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 1000 * 5); params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 1000 * 60); props.put(CommonsHttpMessageSender.HTTP_CLIENT_PARAMS, params); } }
From source file:com.jivesoftware.os.jive.utils.http.client.HttpClientFactoryProvider.java
public HttpClientFactory createHttpClientFactory(final Collection<HttpClientConfiguration> configurations) { return new HttpClientFactory() { @Override/*from ww w. j a v a2s .co m*/ public HttpClient createClient(String host, int port) { ApacheHttpClient31BackedHttpClient httpClient = createApacheClient(); HostConfiguration hostConfiguration = new HostConfiguration(); configureSsl(hostConfiguration, host, port, httpClient); configureProxy(hostConfiguration, httpClient); httpClient.setHostConfiguration(hostConfiguration); configureOAuth(httpClient); return httpClient; } private ApacheHttpClient31BackedHttpClient createApacheClient() { HttpClientConfig httpClientConfig = locateConfig(HttpClientConfig.class, HttpClientConfig.newBuilder().build()); HttpConnectionManager connectionManager = createConnectionManager(httpClientConfig); org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient( connectionManager); client.getParams().setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.RFC_2109); client.getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); client.getParams().setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true); client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, httpClientConfig.getSocketTimeoutInMillis() > 0 ? httpClientConfig.getSocketTimeoutInMillis() : 0); client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, httpClientConfig.getSocketTimeoutInMillis() > 0 ? httpClientConfig.getSocketTimeoutInMillis() : 0); return new ApacheHttpClient31BackedHttpClient(client, httpClientConfig.getCopyOfHeadersForEveryRequest()); } @SuppressWarnings("unchecked") private <T> T locateConfig(Class<? extends T> _class, T defaultConfiguration) { for (HttpClientConfiguration configuration : configurations) { if (_class.isInstance(configuration)) { return (T) configuration; } } return defaultConfiguration; } private boolean hasValidProxyUsernameAndPasswordSettings(HttpClientProxyConfig httpClientProxyConfig) { return StringUtils.isNotBlank(httpClientProxyConfig.getProxyUsername()) && StringUtils.isNotBlank(httpClientProxyConfig.getProxyPassword()); } private HttpConnectionManager createConnectionManager(HttpClientConfig config) { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); if (config.getMaxConnectionsPerHost() > 0) { connectionManager.getParams() .setDefaultMaxConnectionsPerHost(config.getMaxConnectionsPerHost()); } else { connectionManager.getParams().setDefaultMaxConnectionsPerHost(Integer.MAX_VALUE); } if (config.getMaxConnections() > 0) { connectionManager.getParams().setMaxTotalConnections(config.getMaxConnections()); } return connectionManager; } private void configureOAuth(ApacheHttpClient31BackedHttpClient httpClient) { HttpClientOAuthConfig httpClientOAuthConfig = locateConfig(HttpClientOAuthConfig.class, null); if (httpClientOAuthConfig != null) { String serviceName = httpClientOAuthConfig.getServiceName(); HttpClientConsumerKeyAndSecretProvider consumerKeyAndSecretProvider = httpClientOAuthConfig .getConsumerKeyAndSecretProvider(); String consumerKey = consumerKeyAndSecretProvider.getConsumerKey(serviceName); if (StringUtils.isEmpty(consumerKey)) { throw new RuntimeException( "could create oauth client because consumerKey is null or empty for service:" + serviceName); } String consumerSecret = consumerKeyAndSecretProvider.getConsumerSecret(serviceName); if (StringUtils.isEmpty(consumerSecret)) { throw new RuntimeException( "could create oauth client because consumerSecret is null or empty for service:" + serviceName); } httpClient.setConsumerTokens(consumerKey, consumerSecret); } } private void configureProxy(HostConfiguration hostConfiguration, ApacheHttpClient31BackedHttpClient httpClient) { HttpClientProxyConfig httpClientProxyConfig = locateConfig(HttpClientProxyConfig.class, null); if (httpClientProxyConfig != null) { hostConfiguration.setProxy(httpClientProxyConfig.getProxyHost(), httpClientProxyConfig.getProxyPort()); if (hasValidProxyUsernameAndPasswordSettings(httpClientProxyConfig)) { HttpState state = new HttpState(); state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(httpClientProxyConfig.getProxyUsername(), httpClientProxyConfig.getProxyPassword())); httpClient.setState(state); } } } private void configureSsl(HostConfiguration hostConfiguration, String host, int port, ApacheHttpClient31BackedHttpClient httpClient) throws IllegalStateException { HttpClientSSLConfig httpClientSSLConfig = locateConfig(HttpClientSSLConfig.class, null); if (httpClientSSLConfig != null) { Protocol sslProtocol; if (httpClientSSLConfig.getCustomSSLSocketFactory() != null) { sslProtocol = new Protocol(HTTPS_PROTOCOL, new CustomSecureProtocolSocketFactory( httpClientSSLConfig.getCustomSSLSocketFactory()), SSL_PORT); } else { sslProtocol = Protocol.getProtocol(HTTPS_PROTOCOL); } hostConfiguration.setHost(host, port, sslProtocol); httpClient.setUsingSSL(); } else { hostConfiguration.setHost(host, port); } } }; }
From source file:com.cloudbees.api.BeesClientBase.java
public InputStream executeCometRequest(String url) throws Exception { HttpClient httpClient = HttpClientHelper.createClient(this.beesClientConfiguration); HttpParams params = httpClient.getParams(); params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 0); GetMethod getMethod = new GetMethod(url); httpClient.executeMethod(getMethod); return getMethod.getResponseBodyAsStream(); }
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 ava 2 s . co m if (proxyHost != null) { httpClient.getHostConfiguration().setProxyHost(proxyHost); } 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 va 2 s . c o 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 a2 s .com*/ 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 {//from www. j a v a 2 s .c o 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.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;/* www. j a v 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;/*from 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; }