List of usage examples for org.apache.http.impl.conn PoolingClientConnectionManager PoolingClientConnectionManager
public PoolingClientConnectionManager()
From source file:com.myjeeva.digitalocean.impl.DigitalOceanClient.java
private void initialize() { this.deserialize = new GsonBuilder().setDateFormat(DATE_FORMAT).create(); this.serialize = new GsonBuilder().setDateFormat(DATE_FORMAT) .registerTypeAdapter(Droplet.class, new DropletSerializer()).excludeFieldsWithoutExposeAnnotation() .create();//w ww . j av a2 s.c o m this.jsonParser = new JsonParser(); if (null == this.httpClient) { this.httpClient = new DefaultHttpClient(new PoolingClientConnectionManager()); } }
From source file:com.pennassurancesoftware.tutum.client.TutumClient.java
private void initialize() { this.deserialize = new GsonBuilder().setDateFormat(Constants.DATE_FORMAT).create(); this.serialize = new GsonBuilder().setDateFormat(Constants.DATE_FORMAT) .excludeFieldsWithoutExposeAnnotation().create(); this.jsonParser = new JsonParser(); if (null == this.httpClient) { this.httpClient = new DefaultHttpClient(new PoolingClientConnectionManager()); }// ww w.j a va 2s . c om }
From source file:org.apache.maven.plugin.javadoc.JavadocUtil.java
/** * Creates a new {@code HttpClient} instance. * * @param settings The settings to use for setting up the client or {@code null}. * @param url The {@code URL} to use for setting up the client or {@code null}. * * @return A new {@code HttpClient} instance. * * @see #DEFAULT_TIMEOUT/*from w w w .j a va2 s . com*/ * @since 2.8 */ private static HttpClient createHttpClient(Settings settings, URL url) { DefaultHttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager()); httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_TIMEOUT); httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_TIMEOUT); httpClient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); // Some web servers don't allow the default user-agent sent by httpClient httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); if (settings != null && settings.getActiveProxy() != null) { Proxy activeProxy = settings.getActiveProxy(); ProxyInfo proxyInfo = new ProxyInfo(); proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts()); if (StringUtils.isNotEmpty(activeProxy.getHost()) && (url == null || !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost()))) { HttpHost proxy = new HttpHost(activeProxy.getHost(), activeProxy.getPort()); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) { Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(), activeProxy.getPassword()); httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials); } } } return httpClient; }
From source file:com.amazonaws.mws.MarketplaceWebServiceClient.java
/** * Configure HttpClient with set of defaults as well as configuration * from MarketplaceWebServiceConfig instance * *//*from w ww. j a v a 2 s. c o m*/ private HttpClient configureHttpClient(String applicationName, String applicationVersion) { // respect a user-provided User-Agent header as-is, but if none is provided // then generate one satisfying the MWS User-Agent requirements if (config.getUserAgent() == null) { config.setUserAgent(quoteAppName(applicationName), quoteAppVersion(applicationVersion), quoteAttributeValue("Java/" + System.getProperty("java.version") + "/" + System.getProperty("java.class.version") + "/" + System.getProperty("java.vendor")), quoteAttributeName("Platform"), quoteAttributeValue("" + System.getProperty("os.name") + "/" + System.getProperty("os.arch") + "/" + System.getProperty("os.version")), quoteAttributeName("MWSClientVersion"), quoteAttributeValue(mwsClientLibraryVersion)); } defaultHeaders.add(new BasicHeader("X-Amazon-User-Agent", config.getUserAgent())); /* Set http client parameters */ BasicHttpParams httpParams = new BasicHttpParams(); httpParams.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent()); /* Set connection parameters */ HttpConnectionParams.setConnectionTimeout(httpParams, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpParams, config.getSoTimeout()); HttpConnectionParams.setStaleCheckingEnabled(httpParams, true); HttpConnectionParams.setTcpNoDelay(httpParams, true); /* Set connection manager */ PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); connectionManager.setMaxTotal(config.getMaxAsyncQueueSize()); connectionManager.setDefaultMaxPerRoute(config.getMaxAsyncQueueSize()); /* Set http client */ httpClient = new DefaultHttpClient(connectionManager, httpParams); httpContext = new BasicHttpContext(); /* Set proxy if configured */ if (config.isSetProxyHost() && config.isSetProxyPort()) { String proxyProtocol = null; if (config.isSetProxyProtocol()) { //User explicitly set how to talk to proxy proxyProtocol = config.getProxyProtocol().toString().toLowerCase(); } else { // assume that the mws endpoint url determines the protocol // for the proxy as well proxyProtocol = usesHttps(config.getServiceURL()) ? "https" : "http"; } log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + " Proxy Port: " + config.getProxyPort() + " Proxy protocol: " + proxyProtocol); final HttpHost hostConfiguration = new HttpHost(config.getProxyHost(), config.getProxyPort(), proxyProtocol); httpContext = new BasicHttpContext(); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, hostConfiguration); if (config.isSetProxyUsername() && config.isSetProxyPassword()) { credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword())); httpContext.setAttribute(ClientContext.CREDS_PROVIDER, credentialsProvider); } } return httpClient; }
From source file:API.amazon.mws.feeds.service.MarketplaceWebServiceClient.java
/** * Configure HttpClient with set of defaults as well as configuration * from MarketplaceWebServiceConfig instance * */// ww w . j a v a 2s.c o m private HttpClient configureHttpClient(String applicationName, String applicationVersion) { // respect a user-provided User-Agent header as-is, but if none is provided // then generate one satisfying the MWS User-Agent requirements if (config.getUserAgent() == null) { config.setUserAgent(quoteAppName(applicationName), quoteAppVersion(applicationVersion), quoteAttributeValue("Java/" + System.getProperty("java.version") + "/" + System.getProperty("java.class.version") + "/" + System.getProperty("java.vendor")), quoteAttributeName("Platform"), quoteAttributeValue("" + System.getProperty("os.name") + "/" + System.getProperty("os.arch") + "/" + System.getProperty("os.version")), quoteAttributeName("MWSClientVersion"), quoteAttributeValue(mwsClientLibraryVersion)); } defaultHeaders.add(new BasicHeader("X-Amazon-User-Agent", config.getUserAgent())); /* Set http client parameters */ BasicHttpParams httpParams = new BasicHttpParams(); httpParams.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent()); /* Set connection parameters */ HttpConnectionParams.setConnectionTimeout(httpParams, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpParams, config.getSoTimeout()); HttpConnectionParams.setStaleCheckingEnabled(httpParams, true); HttpConnectionParams.setTcpNoDelay(httpParams, true); /* Set connection manager */ PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); connectionManager.setMaxTotal(config.getMaxAsyncQueueSize()); connectionManager.setDefaultMaxPerRoute(config.getMaxAsyncQueueSize()); /* Set http client */ httpClient = new DefaultHttpClient(connectionManager, httpParams); httpContext = new BasicHttpContext(); /* Set proxy if configured */ if (config.isSetProxyHost() && config.isSetProxyPort()) { log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: " + config.getProxyPort()); final HttpHost hostConfiguration = new HttpHost(config.getProxyHost(), config.getProxyPort(), (usesHttps(config.getServiceURL()) ? "https" : "http")); httpContext = new BasicHttpContext(); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, hostConfiguration); if (config.isSetProxyUsername() && config.isSetProxyPassword()) { credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword())); httpContext.setAttribute(ClientContext.CREDS_PROVIDER, credentialsProvider); } } return httpClient; }
From source file:org.apache.tomcat.maven.common.deployer.TomcatManager.java
/** * Creates a Tomcat manager wrapper for the specified URL, username, password and URL encoding. * * @param url the full URL of the Tomcat manager instance to use * @param username the username to use when authenticating with Tomcat manager * @param password the password to use when authenticating with Tomcat manager * @param charset the URL encoding charset to use when communicating with Tomcat manager * @param verbose if the build is in verbose mode (quiet mode otherwise) * @since 2.2/*from w ww .j a v a 2s . c o m*/ */ public TomcatManager(URL url, String username, String password, String charset, boolean verbose) { this.url = url; this.username = username; this.password = password; this.charset = charset; this.verbose = verbose; PoolingClientConnectionManager poolingClientConnectionManager = new PoolingClientConnectionManager(); poolingClientConnectionManager.setMaxTotal(5); this.httpClient = new DefaultHttpClient(poolingClientConnectionManager); if (StringUtils.isNotEmpty(username)) { Credentials creds = new UsernamePasswordCredentials(username, password); String host = url.getHost(); int port = url.getPort() > -1 ? url.getPort() : AuthScope.ANY_PORT; httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port), creds); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); authCache.put(targetHost, basicAuth); localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.AUTH_CACHE, authCache); } }
From source file:org.eobjects.analyzer.cluster.http.HttpClusterManager.java
/** * Creates a new HTTP cluster manager// www. jav a 2s . c om * * @param slaveEndpoints * the endpoint URLs of the slaves */ public HttpClusterManager(List<String> slaveEndpoints) { this(new DefaultHttpClient(new PoolingClientConnectionManager()), slaveEndpoints); }
From source file:org.eobjects.datacleaner.user.UserPreferencesImpl.java
@Override public HttpClient createHttpClient() { final ClientConnectionManager connectionManager = new PoolingClientConnectionManager(); final DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager); if (isProxyEnabled()) { // set up HTTP proxy final String proxyHostname = getProxyHostname(); final int proxyPort = getProxyPort(); try {/* ww w.j a v a 2 s .c o m*/ final HttpHost proxy = new HttpHost(proxyHostname, proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if (isProxyAuthenticationEnabled()) { final AuthScope authScope = new AuthScope(proxyHostname, proxyPort); final String proxyUsername = getProxyUsername(); final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(proxyUsername, getProxyPassword()); httpClient.getCredentialsProvider().setCredentials(authScope, credentials); final int backslashIndex = proxyUsername.lastIndexOf('\\'); final String ntUsername; final String ntDomain; if (backslashIndex != -1) { ntUsername = proxyUsername.substring(backslashIndex + 1); ntDomain = proxyUsername.substring(0, backslashIndex); } else { ntUsername = proxyUsername; ntDomain = System.getProperty("datacleaner.proxy.domain"); } String workstation = System.getProperty("datacleaner.proxy.workstation"); if (Strings.isNullOrEmpty(workstation)) { String computername = InetAddress.getLocalHost().getHostName(); workstation = computername; } NTCredentials ntCredentials = new NTCredentials(ntUsername, getProxyPassword(), workstation, ntDomain); AuthScope ntAuthScope = new AuthScope(proxyHostname, proxyPort, AuthScope.ANY_REALM, "ntlm"); httpClient.getCredentialsProvider().setCredentials(ntAuthScope, ntCredentials); } } catch (Exception e) { // ignore proxy creation and return http client without it logger.error("Unexpected error occurred while initializing HTTP proxy", e); } } return httpClient; }
From source file:org.fcrepo.client.utils.HttpHelper.java
private static HttpClient buildClient(final String fedoraUsername, final String fedoraPassword, final String repositoryURL) { final PoolingClientConnectionManager connMann = new PoolingClientConnectionManager(); connMann.setMaxTotal(MAX_VALUE);// ww w. j a v a2 s .c o m connMann.setDefaultMaxPerRoute(MAX_VALUE); final DefaultHttpClient httpClient = new DefaultHttpClient(connMann); httpClient.setRedirectStrategy(new DefaultRedirectStrategy()); httpClient.setHttpRequestRetryHandler(new StandardHttpRequestRetryHandler(0, false)); // If the Fedora instance requires authentication, set it up here if (!isBlank(fedoraUsername) && !isBlank(fedoraPassword)) { LOGGER.debug("Adding BASIC credentials to client for repo requests."); final URI fedoraUri = URI.create(repositoryURL); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(fedoraUri.getHost(), fedoraUri.getPort()), new UsernamePasswordCredentials(fedoraUsername, fedoraPassword)); httpClient.setCredentialsProvider(credsProvider); } return httpClient; }