List of usage examples for org.apache.commons.httpclient HostConfiguration setProxy
public void setProxy(String paramString, int paramInt)
From source file:com.amazonaws.elasticmapreduce.AmazonElasticMapReduceClient.java
/** * Configure HttpClient with set of defaults as well as configuration * from AmazonElasticMapReduceConfig instance * *///from w ww.java 2 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/*ww w.j a va2s .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 (!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.jaspersoft.ireport.jasperserver.ws.CommonsHTTPSender.java
/** * This method has been modified from the original one provided by Axis * in order to use the NetBeans proxy settings... * // w ww .j ava 2 s. co m * @param client * @param context * @param targetURL * @return */ protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL targetURL) { ProxySettings proxySettings = new ProxySettings(); TransportClientProperties tcp = TransportClientPropertiesFactory.create(targetURL.getProtocol()); // http or https int port = targetURL.getPort(); boolean hostInNonProxyList = proxySettings.isHostInNonProxyList(targetURL.getHost()); //isHostInNonProxyList(targetURL.getHost(), tcp.getNonProxyHosts()); HostConfiguration config = client.getHostConfiguration(); if (port == -1) { if (targetURL.getProtocol().equalsIgnoreCase("https")) { port = 443; // default port for https being 443 } else { // it must be http port = 80; // default port for http being 80 } } // System.out.println("Connecting to port: " + port); if (!hostInNonProxyList && proxySettings.getHttpHost().length() != 0 && proxySettings.getHttpPort() > 0) { if (proxySettings.getUsername() != null && proxySettings.getUsername().length() != 0) { Credentials proxyCred = new UsernamePasswordCredentials(proxySettings.getUsername(), proxySettings.getPassword()); // if the username is in the form "user\domain" // then use NTCredentials instead. int domainIndex = proxySettings.getUsername().indexOf("\\"); if (domainIndex > 0) { String domain = proxySettings.getUsername().substring(0, domainIndex); if (proxySettings.getUsername().length() > domainIndex + 1) { String user = proxySettings.getUsername().substring(domainIndex + 1); proxyCred = new NTCredentials(user, proxySettings.getPassword(), proxySettings.getHttpHost(), domain); } } client.getState().setProxyCredentials(AuthScope.ANY, proxyCred); } int proxyPort = proxySettings.getHttpPort(); config.setProxy(proxySettings.getHttpHost(), proxyPort); } else { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } /* if(hostInNonProxyList){ config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyHost().length() == 0 || tcp.getProxyPort().length() == 0) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyUser().length() != 0) { Credentials proxyCred = new UsernamePasswordCredentials(tcp.getProxyUser(), tcp.getProxyPassword()); // if the username is in the form "user\domain" // then use NTCredentials instead. int domainIndex = tcp.getProxyUser().indexOf("\\"); if (domainIndex > 0) { String domain = tcp.getProxyUser().substring(0, domainIndex); if (tcp.getProxyUser().length() > domainIndex + 1) { String user = tcp.getProxyUser().substring(domainIndex + 1); proxyCred = new NTCredentials(user, tcp.getProxyPassword(), tcp.getProxyHost(), domain); } } client.getState().setProxyCredentials(AuthScope.ANY, proxyCred); } int proxyPort = new Integer(tcp.getProxyPort()).intValue(); config.setProxy(tcp.getProxyHost(), proxyPort); } } * */ //System.out.println("Host Configuration:"); //System.out.println("Proxy:" + config.getProxyHost() + ":" + config.getProxyPort()); //System.out.flush(); return config; }
From source file:com.polarion.alm.ws.client.internal.connection.CommonsHTTPSender.java
protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL targetURL) { TransportClientProperties tcp = TransportClientPropertiesFactory.create(targetURL.getProtocol()); // http or https int port = targetURL.getPort(); boolean hostInNonProxyList = isHostInNonProxyList(targetURL.getHost(), tcp.getNonProxyHosts()); HostConfiguration config = new HostConfiguration(); if (port == -1) { if (targetURL.getProtocol().equalsIgnoreCase("https")) { port = 443; // default port for https being 443 } else { // it must be http port = 80; // default port for http being 80 }/*from w w w .j av a 2 s .co m*/ } if (hostInNonProxyList) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyHost().length() == 0 || tcp.getProxyPort().length() == 0) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyUser().length() != 0) { Credentials proxyCred = new UsernamePasswordCredentials(tcp.getProxyUser(), tcp.getProxyPassword()); // if the username is in the form "user\domain" // then use NTCredentials instead. int domainIndex = tcp.getProxyUser().indexOf("\\"); if (domainIndex > 0) { String domain = tcp.getProxyUser().substring(0, domainIndex); if (tcp.getProxyUser().length() > domainIndex + 1) { String user = tcp.getProxyUser().substring(domainIndex + 1); proxyCred = new NTCredentials(user, tcp.getProxyPassword(), tcp.getProxyHost(), domain); } } client.getState().setProxyCredentials(AuthScope.ANY, proxyCred); } int proxyPort = new Integer(tcp.getProxyPort()).intValue(); config.setProxy(tcp.getProxyHost(), proxyPort); } } return config; }
From source file:gov.va.med.imaging.proxy.ImageXChangeHttpCommonsSender.java
protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL targetURL) { TransportClientProperties tcp = TransportClientPropertiesFactory.create(targetURL.getProtocol()); // http // or/*from w ww . j av a 2 s . com*/ // https int port = targetURL.getPort(); boolean hostInNonProxyList = isHostInNonProxyList(targetURL.getHost(), tcp.getNonProxyHosts()); HostConfiguration config = new HostConfiguration(); if (port == -1) { if (targetURL.getProtocol().equalsIgnoreCase("https")) { port = 443; // default port for https being 443 } else { // it must be http port = 80; // default port for http being 80 } } if (hostInNonProxyList) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyHost().length() == 0 || tcp.getProxyPort().length() == 0) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyUser().length() != 0) { Credentials proxyCred = new UsernamePasswordCredentials(tcp.getProxyUser(), tcp.getProxyPassword()); // if the username is in the form "user\domain" // then use NTCredentials instead. int domainIndex = tcp.getProxyUser().indexOf("\\"); if (domainIndex > 0) { String domain = tcp.getProxyUser().substring(0, domainIndex); if (tcp.getProxyUser().length() > domainIndex + 1) { String user = tcp.getProxyUser().substring(domainIndex + 1); proxyCred = new NTCredentials(user, tcp.getProxyPassword(), tcp.getProxyHost(), domain); } } client.getState().setProxyCredentials(AuthScope.ANY, proxyCred); } int proxyPort = new Integer(tcp.getProxyPort()).intValue(); config.setProxy(tcp.getProxyHost(), proxyPort); } } return config; }
From source file:com.jivesoftware.os.jive.utils.http.client.HttpClientFactoryProvider.java
public HttpClientFactory createHttpClientFactory(final Collection<HttpClientConfiguration> configurations) { return new HttpClientFactory() { @Override/*from w w w. jav a 2s . c o 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:nz.net.kallisti.emusicj.network.http.proxy.HttpClientProvider.java
public HttpClient getHttpClient() { HttpState state = getState();// w w w . j a v a2 s . c om HttpClient client = new HttpClient(); client.setState(state); if (!prefs.getProxyHost().equals("") && prefs.usingProxy()) { HostConfiguration hostConf = new HostConfiguration(); hostConf.setProxy(prefs.getProxyHost(), prefs.getProxyPort()); client.setHostConfiguration(hostConf); client.getParams().setParameter(CredentialsProvider.PROVIDER, proxyCredsProvider); } return client; }
From source file:ome.formats.importer.util.HtmlMessenger.java
/** * Instantiate html messenger/* w w w.j a v a 2 s . co m*/ * * @param url * @param postList - variables list in post * @throws HtmlMessengerException */ public HtmlMessenger(String url, List<Part> postList) throws HtmlMessengerException { try { HostConfiguration cfg = new HostConfiguration(); cfg.setHost(url); String proxyHost = System.getProperty(PROXY_HOST); String proxyPort = System.getProperty(PROXY_PORT); if (proxyHost != null && proxyPort != null) { int port = Integer.parseInt(proxyPort); cfg.setProxy(proxyHost, port); } client = new HttpClient(); client.setHostConfiguration(cfg); HttpClientParams params = new HttpClientParams(); params.setConnectionManagerTimeout(CONN_TIMEOUT); params.setSoTimeout(CONN_TIMEOUT); client.setParams(params); method = new PostMethod(url); Part[] parts = new Part[postList.size()]; int i = 0; for (Part part : postList) { parts[i] = part; i++; } MultipartRequestEntity mpre = new MultipartRequestEntity(parts, method.getParams()); ProgressListener listener = new ProgressListener() { /* (non-Javadoc) * @see ome.formats.importer.util.FileUploadCounter.ProgressListener#update(long) */ public void update(long bytesRead) { } }; FileUploadCounter hfre = new FileUploadCounter(mpre, listener); method.setRequestEntity(hfre); } catch (Exception e) { e.printStackTrace(); throw new HtmlMessengerException("Error creating post parameters", e); } }
From source file:org.activebpel.rt.axis.bpel.handlers.AeHTTPSender.java
/** * @deprecated//from www . jav a2 s.c o m */ private HostConfiguration getHostConfiguration(HttpClient client, URL targetURL) { TransportClientProperties tcp = TransportClientPropertiesFactory.create(targetURL.getProtocol()); // http or https int port = targetURL.getPort(); boolean hostInNonProxyList = isHostInNonProxyList(targetURL.getHost(), tcp.getNonProxyHosts()); HostConfiguration config = new HostConfiguration(); if (port == -1) { port = 80; // even for https } if (hostInNonProxyList) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyHost().length() == 0 || tcp.getProxyPort().length() == 0) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyUser().length() != 0) { Credentials proxyCred = new UsernamePasswordCredentials(tcp.getProxyUser(), tcp.getProxyPassword()); client.getState().setProxyCredentials(null, null, proxyCred); } int proxyPort = new Integer(tcp.getProxyPort()).intValue(); config.setProxy(tcp.getProxyHost(), proxyPort); } } return config; }
From source file:org.apache.axis.transport.http.CommonsHTTPSender.java
protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL targetURL) { TransportClientProperties tcp = TransportClientPropertiesFactory.create(targetURL.getProtocol()); // http or https int port = targetURL.getPort(); boolean hostInNonProxyList = isHostInNonProxyList(targetURL.getHost(), tcp.getNonProxyHosts()); HostConfiguration config = new HostConfiguration(); if (port == -1) { if (targetURL.getProtocol().equalsIgnoreCase("https")) { port = 443; // default port for https being 443 } else { // it must be http port = 80; // default port for http being 80 }/*from ww w . ja v a2s. c o m*/ } if (hostInNonProxyList) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyHost().length() == 0 || tcp.getProxyPort().length() == 0) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyUser().length() != 0) { Credentials proxyCred = new UsernamePasswordCredentials(tcp.getProxyUser(), tcp.getProxyPassword()); // if the username is in the form "user\domain" // then use NTCredentials instead. int domainIndex = tcp.getProxyUser().indexOf("\\"); if (domainIndex > 0) { String domain = tcp.getProxyUser().substring(0, domainIndex); if (tcp.getProxyUser().length() > domainIndex + 1) { String user = tcp.getProxyUser().substring(domainIndex + 1); proxyCred = new NTCredentials(user, tcp.getProxyPassword(), tcp.getProxyHost(), domain); } } client.getState().setProxyCredentials(AuthScope.ANY, proxyCred); } int proxyPort = new Integer(tcp.getProxyPort()).intValue(); config.setProxy(tcp.getProxyHost(), proxyPort); } } return config; }