List of usage examples for org.apache.commons.httpclient HostConfiguration setProxy
public void setProxy(String paramString, int paramInt)
From source file:org.attribyte.api.http.impl.commons.Commons3Client.java
/** * Creates a client with specified options. * @param options The options./*from w ww . j a v a 2 s . co m*/ */ public Commons3Client(final ClientOptions options) { connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.setParams(fromOptions(options)); httpClient = new HttpClient(connectionManager); HostConfiguration hostConfig = new HostConfiguration(); if (options.proxyHost != null) { hostConfig.setProxy(options.proxyHost, options.proxyPort); } httpClient.setHostConfiguration(hostConfig); userAgent = options.userAgent; }
From source file:org.attribyte.api.http.impl.commons.Commons3Client.java
@Override /**//from w ww . j a v a 2 s. co m * Initializes the client from properties. * <p> * The following properties are available. <b>Bold</b> properties are required. * * <h2>HTTP Client</h2> * <dl> * <dt><b>User-Agent</b></dt> * <dd>The default User-Agent string sent with requests. Added only if * request has no <code>User-Agent</code> header.</dd> * <dt><b>connectionTimeoutMillis</b></dt> * <dd>The HTTP connection timeout in milliseconds.</dd> * <dt><b>socketTimeoutMillis</b></dt> * <dd>The HTTP client socket timeout in milliseconds.</dd> * <dt>proxyHost</dt> * <dd>The HTTP proxy host. If specified, all client requests will use this proxy.</dd> * <dt>proxyPort</dt> * <dd>The HTTP proxy port. Required when <code>proxyHost</code> is specified</dd> * </dl> * </p> * @param prefix The prefix for all properties (e.g. 'client.'). * @param props The properties. * @param logger The logger. If unspecified, messages are logged to the console. * @throws InitializationException on initialization error. */ public synchronized void init(String prefix, Properties props, Logger logger) throws InitializationException { if (isInit.compareAndSet(false, true)) { ClientOptions options = new ClientOptions(prefix, props); connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.setParams(fromOptions(options)); httpClient = new HttpClient(connectionManager); HostConfiguration hostConfig = new HostConfiguration(); if (options.proxyHost != null) { hostConfig.setProxy(options.proxyHost, options.proxyPort); } httpClient.setHostConfiguration(hostConfig); userAgent = options.userAgent; } }
From source file:org.deegree.commons.utils.net.HttpUtils.java
private static void handleProxies(String protocol, HttpClient client, String host) { TreeSet<String> nops = new TreeSet<String>(); String proxyHost = getProperty((protocol == null ? "" : protocol + ".") + "proxyHost"); String proxyUser = getProperty((protocol == null ? "" : protocol + ".") + "proxyUser"); String proxyPass = getProperty((protocol == null ? "" : protocol + ".") + "proxyPassword"); if (proxyHost != null) { String nop = getProperty((protocol == null ? "" : protocol + ".") + "noProxyHosts"); if (nop != null && !nop.equals("")) { nops.addAll(asList(nop.split("\\|"))); }/*from w ww . j a va 2s. c o m*/ nop = getProperty((protocol == null ? "" : protocol + ".") + "nonProxyHosts"); if (nop != null && !nop.equals("")) { nops.addAll(asList(nop.split("\\|"))); } int proxyPort = parseInt(getProperty((protocol == null ? "" : protocol + ".") + "proxyPort")); HostConfiguration hc = client.getHostConfiguration(); if (LOG.isDebugEnabled()) { LOG.debug("Found the following no- and nonProxyHosts: {}", nops); } if (proxyUser != null) { Credentials creds = new UsernamePasswordCredentials(proxyUser, proxyPass); client.getState().setProxyCredentials(AuthScope.ANY, creds); client.getParams().setAuthenticationPreemptive(true); } if (!nops.contains(host)) { if (LOG.isDebugEnabled()) { LOG.debug("Using proxy {}:{}", proxyHost, proxyPort); if (protocol == null) { LOG.debug("This overrides the protocol specific settings, if there were any."); } } hc.setProxy(proxyHost, proxyPort); client.setHostConfiguration(hc); } else { if (LOG.isDebugEnabled()) { LOG.debug("Proxy was set, but {} was contained in the no-/nonProxyList!", host); if (protocol == null) { LOG.debug("If a protocol specific proxy has been set, it will be used anyway!"); } } } } if (protocol != null) { handleProxies(null, client, host); } }
From source file:org.deegree.enterprise.WebUtils.java
private static void handleProxies(String protocol, HttpClient client, String host) { TreeSet<String> nops = new TreeSet<String>(); String proxyHost = getProperty((protocol == null ? "" : protocol + ".") + "proxyHost"); String proxyUser = getProperty((protocol == null ? "" : protocol + ".") + "proxyUser"); String proxyPass = getProperty((protocol == null ? "" : protocol + ".") + "proxyPassword"); if (proxyHost != null) { String nop = getProperty((protocol == null ? "" : protocol + ".") + "noProxyHosts"); if (nop != null && !nop.equals("")) { nops.addAll(asList(nop.split("\\|"))); }//from w w w . j a v a 2s . c o m nop = getProperty((protocol == null ? "" : protocol + ".") + "nonProxyHosts"); if (nop != null && !nop.equals("")) { nops.addAll(asList(nop.split("\\|"))); } int proxyPort = parseInt(getProperty((protocol == null ? "" : protocol + ".") + "proxyPort")); HostConfiguration hc = client.getHostConfiguration(); if (LOG.isDebug()) { LOG.logDebug("Found the following no- and nonProxyHosts", nops); } if (proxyUser != null) { Credentials creds = new UsernamePasswordCredentials(proxyUser, proxyPass); client.getState().setProxyCredentials(AuthScope.ANY, creds); client.getParams().setAuthenticationPreemptive(true); } if (!nops.contains(host)) { if (LOG.isDebug()) { LOG.logDebug("Using proxy " + proxyHost + ":" + proxyPort); if (protocol == null) { LOG.logDebug("This overrides the protocol specific settings, if there were any."); } } hc.setProxy(proxyHost, proxyPort); client.setHostConfiguration(hc); } else { if (LOG.isDebug()) { LOG.logDebug("Proxy was set, but " + host + " was contained in the no-/nonProxyList!"); if (protocol == null) { LOG.logDebug("If a protocol specific proxy has been set, it will be used anyway!"); } } } } if (protocol != null) { handleProxies(null, client, host); } }
From source file:org.eclipse.mylyn.commons.net.WebUtil.java
private static void configureHttpClientProxy(HttpClient client, HostConfiguration hostConfiguration, AbstractWebLocation location) {/* w w w. jav a2s. co m*/ String host = WebUtil.getHost(location.getUrl()); Proxy proxy; if (WebUtil.isRepositoryHttps(location.getUrl())) { proxy = location.getProxyForHost(host, IProxyData.HTTPS_PROXY_TYPE); } else { proxy = location.getProxyForHost(host, IProxyData.HTTP_PROXY_TYPE); } if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) { InetSocketAddress address = (InetSocketAddress) proxy.address(); hostConfiguration.setProxy(address.getHostName(), address.getPort()); if (proxy instanceof AuthenticatedProxy) { AuthenticatedProxy authProxy = (AuthenticatedProxy) proxy; Credentials credentials = getCredentials(authProxy.getUserName(), authProxy.getPassword(), address.getAddress()); AuthScope proxyAuthScope = new AuthScope(address.getHostName(), address.getPort(), AuthScope.ANY_REALM); client.getState().setProxyCredentials(proxyAuthScope, credentials); } } else { hostConfiguration.setProxyHost(null); } }
From source file:org.eclipse.mylyn.internal.provisional.commons.soap.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")) { //$NON-NLS-1$ 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 a v a 2 s. c om*/ } 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("\\"); //$NON-NLS-1$ 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:org.eclipse.osee.framework.core.util.HttpProcessor.java
private static void configureProxyData(URI uri, HostConfiguration config) { boolean proxyBypass = OseeProperties.getOseeProxyBypassEnabled(); if (!proxyBypass) { if (proxyService == null) { BundleContext context = Activator.getBundleContext(); if (context != null) { ServiceReference<IProxyService> reference = context.getServiceReference(IProxyService.class); proxyService = context.getService(reference); }/*w ww . ja v a2s .co m*/ if (proxyService != null) { proxyService.addProxyChangeListener(new IProxyChangeListener() { @Override public void proxyInfoChanged(IProxyChangeEvent event) { proxiedData.clear(); } }); } } String key = String.format("%s_%s", uri.getScheme(), uri.getHost()); IProxyData[] datas = proxiedData.get(key); if (datas == null && proxyService != null) { datas = proxyService.select(uri); proxiedData.put(key, datas); } if (datas != null) { for (IProxyData data : datas) { config.setProxy(data.getHost(), data.getPort()); } } } OseeLog.logf(Activator.class, Level.INFO, "Http-Request: [%s] [%s]", requests++, uri.toASCIIString()); }
From source file:org.eclipse.smila.connectivity.framework.crawler.web.http.Http.java
/** * Loads HTTP client configuration for this web site. *///from www. j a v a2 s . co m private void configureClient() { final HttpConnectionManagerParams params = s_connectionManager.getParams(); if (_timeout != 0) { params.setConnectionTimeout(_timeout); params.setSoTimeout(_timeout); } else { params.setConnectionTimeout(_connectTimeout); params.setSoTimeout(_readTimeout); } params.setSendBufferSize(BUFFER_SIZE); params.setReceiveBufferSize(BUFFER_SIZE); final HostConfiguration hostConf = s_client.getHostConfiguration(); final List<Header> headers = new ArrayList<Header>(); // prefer English headers.add(new Header("Accept-Language", "en-us,en-gb,en;q=0.7,*;q=0.3")); // prefer UTF-8 headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7")); // prefer understandable formats headers.add(new Header("Accept", "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8")); // accept GZIP content headers.add(new Header("Accept-Encoding", "x-gzip, gzip")); final String[] webSiteHeaders = getConf().get(HttpProperties.HEADERS).split(SEMICOLON); for (String header : webSiteHeaders) { final String[] headerInformation = header.split(COLON); if (headerInformation.length > 2) { headers.add(new Header(headerInformation[0].trim(), headerInformation[1].trim())); } } hostConf.getParams().setParameter("http.default-headers", headers); if (_useProxy) { hostConf.setProxy(_proxyHost, _proxyPort); if (_proxyLogin.length() > 0) { final Credentials proxyCreds = new UsernamePasswordCredentials(_proxyLogin, _proxyPassword); s_client.getState().setProxyCredentials(new AuthScope(AuthScope.ANY), proxyCreds); } } final List<Rfc2617Authentication> httpAuthentications = _authentication.getRfc2617Authentications(); for (Rfc2617Authentication auth : httpAuthentications) { s_client.getState().setCredentials( new AuthScope(auth.getHost(), Integer.valueOf(auth.getPort()), auth.getRealm()), new UsernamePasswordCredentials(auth.getLogin(), auth.getPassword())); } final SslCertificateAuthentication sslAuth = _authentication.getSslCertificateAuthentication(); if (sslAuth != null) { try { final URL truststoreURL = new File(sslAuth.getTruststoreUrl()).toURL(); final URL keystoreURL = new File(sslAuth.getKeystoreUrl()).toURL(); final ProtocolSocketFactory sslFactory = new AuthSSLProtocolSocketFactory(keystoreURL, sslAuth.getKeystorePassword(), truststoreURL, sslAuth.getTruststorePassword()); _https = new Protocol(sslAuth.getProtocolName(), sslFactory, Integer.valueOf(sslAuth.getPort())); Protocol.registerProtocol(sslAuth.getProtocolName(), _https); } catch (MalformedURLException exception) { LOG.error("unable to bind https protocol" + exception.toString()); } } }
From source file:org.eclipse.virgo.ide.runtime.internal.core.utils.WebDownloadUtils.java
private static void configureHttpClientProxy(String url, HttpClient client, HostConfiguration hostConfiguration) { ServiceReference services = ServerCorePlugin.getDefault().getBundle().getBundleContext() .getServiceReference(IProxyService.class.getName()); if (services != null) { IProxyService proxyService = (IProxyService) ServerCorePlugin.getDefault().getBundle() .getBundleContext().getService(services); IProxyData proxyData = proxyService.getProxyDataForHost(getHost(url), IProxyData.HTTP_PROXY_TYPE); if (proxyService.isProxiesEnabled() && proxyData != null) { hostConfiguration.setProxy(proxyData.getHost(), proxyData.getPort()); }/*from www .ja v a2s . co m*/ if (proxyData != null && proxyData.isRequiresAuthentication()) { client.getState().setProxyCredentials(AuthScope.ANY, getCredentials(proxyData, getHost(url))); } } }
From source file:org.elasticsearch.hadoop.rest.commonshttp.CommonsHttpTransport.java
private Object[] setupHttpProxy(Settings settings, HostConfiguration hostConfig) { // return HostConfiguration + HttpState Object[] results = new Object[2]; results[0] = hostConfig;//from www. j av a 2 s.c o m // set proxy settings String proxyHost = null; int proxyPort = -1; if (settings.getNetworkHttpUseSystemProperties()) { proxyHost = System.getProperty("http.proxyHost"); proxyPort = Integer.getInteger("http.proxyPort", -1); } if (StringUtils.hasText(settings.getNetworkProxyHttpHost())) { proxyHost = settings.getNetworkProxyHttpHost(); } if (settings.getNetworkProxyHttpPort() > 0) { proxyPort = settings.getNetworkProxyHttpPort(); } if (StringUtils.hasText(proxyHost)) { hostConfig.setProxy(proxyHost, proxyPort); proxyInfo = proxyInfo.concat(String.format("[HTTP proxy %s:%s]", proxyHost, proxyPort)); // client is not yet initialized so postpone state if (StringUtils.hasText(settings.getNetworkProxyHttpUser())) { if (!StringUtils.hasText(settings.getNetworkProxyHttpPass())) { log.warn(String.format( "HTTP proxy user specified but no/empty password defined - double check the [%s] property", ConfigurationOptions.ES_NET_PROXY_HTTP_PASS)); } HttpState state = new HttpState(); state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials( settings.getNetworkProxyHttpUser(), settings.getNetworkProxyHttpPass())); // client is not yet initialized so simply save the object for later results[1] = state; } if (log.isDebugEnabled()) { if (StringUtils.hasText(settings.getNetworkProxyHttpUser())) { log.debug(String.format("Using authenticated HTTP proxy [%s:%s]", proxyHost, proxyPort)); } else { log.debug(String.format("Using HTTP proxy [%s:%s]", proxyHost, proxyPort)); } } } return results; }