List of usage examples for java.net Proxy address
public SocketAddress address()
From source file:jp.go.nict.langrid.servicesupervisor.invocationprocessor.executor.intragrid.HttpClientUtil.java
/** * //w w w .j a v a 2s .c o m * */ public static HttpClient createHttpClientWithHostConfig(URL url) { HttpClient client = new HttpClient(); int port = url.getPort(); if (port == -1) { port = url.getDefaultPort(); if (port == -1) { port = 80; } } if ((url.getProtocol().equalsIgnoreCase("https")) && (sslSocketFactory != null)) { Protocol https = new Protocol("https", (ProtocolSocketFactory) new SSLSocketFactorySSLProtocolSocketFactory(sslSocketFactory), port); client.getHostConfiguration().setHost(url.getHost(), url.getPort(), https); } else { Protocol http = new Protocol("http", new SocketFactoryProtocolSocketFactory(SocketFactory.getDefault()), port); client.getHostConfiguration().setHost(url.getHost(), url.getPort(), http); } try { List<Proxy> proxies = ProxySelector.getDefault().select(url.toURI()); for (Proxy p : proxies) { if (p.equals(Proxy.NO_PROXY)) continue; if (!p.type().equals(Proxy.Type.HTTP)) continue; InetSocketAddress addr = (InetSocketAddress) p.address(); client.getHostConfiguration().setProxy(addr.getHostName(), addr.getPort()); client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", "")); break; } } catch (URISyntaxException e) { e.printStackTrace(); } return client; }
From source file:eu.esdihumboldt.util.http.ProxyUtil.java
/** * Set-up the given HTTP client to use the given proxy * //from w w w . j a v a2s. c o m * @param builder the HTTP client builder * @param proxy the proxy * @return the client builder adapted with the proxy settings */ public static HttpClientBuilder applyProxy(HttpClientBuilder builder, Proxy proxy) { init(); // check if proxy shall be used if (proxy != null && proxy.type() == Type.HTTP) { InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); // set the proxy HttpHost proxyHost = new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort()); builder = builder.setProxy(proxyHost); String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$ String password = System.getProperty("http.proxyPassword"); //$NON-NLS-1$ boolean useProxyAuth = user != null && !user.isEmpty(); if (useProxyAuth) { // set the proxy credentials CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(proxyAddress.getHostName(), proxyAddress.getPort()), new UsernamePasswordCredentials(user, password)); builder = builder.setDefaultCredentialsProvider(credsProvider) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); } _log.trace("Set proxy to " + proxyAddress.getHostName() + ":" + //$NON-NLS-1$ //$NON-NLS-2$ proxyAddress.getPort() + ((useProxyAuth) ? (" as user " + user) : (""))); //$NON-NLS-1$ //$NON-NLS-2$ } return builder; }
From source file:com.intellij.util.net.HttpConfigurable.java
public static List<KeyValue<String, String>> getJvmPropertiesList(final boolean withAutodetection, @Nullable final URI uri) { final HttpConfigurable me = getInstance(); if (!me.USE_HTTP_PROXY && !me.USE_PROXY_PAC) { return Collections.emptyList(); }/*from w ww . ja v a 2s .c o m*/ final List<KeyValue<String, String>> result = new ArrayList<KeyValue<String, String>>(); if (me.USE_HTTP_PROXY) { final boolean putCredentials = me.KEEP_PROXY_PASSWORD && StringUtil.isNotEmpty(me.PROXY_LOGIN); if (me.PROXY_TYPE_IS_SOCKS) { result.add(KeyValue.create(JavaProxyProperty.SOCKS_HOST, me.PROXY_HOST)); result.add(KeyValue.create(JavaProxyProperty.SOCKS_PORT, String.valueOf(me.PROXY_PORT))); if (putCredentials) { result.add(KeyValue.create(JavaProxyProperty.SOCKS_USERNAME, me.PROXY_LOGIN)); result.add(KeyValue.create(JavaProxyProperty.SOCKS_PASSWORD, me.getPlainProxyPassword())); } } else { result.add(KeyValue.create(JavaProxyProperty.HTTP_HOST, me.PROXY_HOST)); result.add(KeyValue.create(JavaProxyProperty.HTTP_PORT, String.valueOf(me.PROXY_PORT))); result.add(KeyValue.create(JavaProxyProperty.HTTPS_HOST, me.PROXY_HOST)); result.add(KeyValue.create(JavaProxyProperty.HTTPS_PORT, String.valueOf(me.PROXY_PORT))); if (putCredentials) { result.add(KeyValue.create(JavaProxyProperty.HTTP_USERNAME, me.PROXY_LOGIN)); result.add(KeyValue.create(JavaProxyProperty.HTTP_PASSWORD, me.getPlainProxyPassword())); } } } else if (me.USE_PROXY_PAC && withAutodetection && uri != null) { final List<Proxy> proxies = CommonProxy.getInstance().select(uri); // we will just take the first returned proxy, but we have an option to test connection through each of them, // for instance, by calling prepareUrl() if (proxies != null && !proxies.isEmpty()) { for (Proxy proxy : proxies) { if (isRealProxy(proxy)) { final SocketAddress address = proxy.address(); if (address instanceof InetSocketAddress) { final InetSocketAddress inetSocketAddress = (InetSocketAddress) address; if (Proxy.Type.SOCKS.equals(proxy.type())) { result.add(KeyValue.create(JavaProxyProperty.SOCKS_HOST, inetSocketAddress.getHostName())); result.add(KeyValue.create(JavaProxyProperty.SOCKS_PORT, String.valueOf(inetSocketAddress.getPort()))); } else { result.add(KeyValue.create(JavaProxyProperty.HTTP_HOST, inetSocketAddress.getHostName())); result.add(KeyValue.create(JavaProxyProperty.HTTP_PORT, String.valueOf(inetSocketAddress.getPort()))); result.add(KeyValue.create(JavaProxyProperty.HTTPS_HOST, inetSocketAddress.getHostName())); result.add(KeyValue.create(JavaProxyProperty.HTTPS_PORT, String.valueOf(inetSocketAddress.getPort()))); } } } } } } return result; }
From source file:org.eclipse.mylyn.commons.repositories.http.core.HttpUtil.java
public static void configureProxy(AbstractHttpClient client, RepositoryLocation location) { Assert.isNotNull(client);/*from w ww .j a v a 2 s . c o m*/ Assert.isNotNull(location); String url = location.getUrl(); Assert.isNotNull(url, "The location url must not be null"); //$NON-NLS-1$ String host = NetUtil.getHost(url); Proxy proxy; if (NetUtil.isUrlHttps(url)) { 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(); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(address.getHostName(), address.getPort())); if (proxy instanceof AuthenticatedProxy) { AuthenticatedProxy authProxy = (AuthenticatedProxy) proxy; Credentials credentials = getCredentials(authProxy.getUserName(), authProxy.getPassword(), address.getAddress(), false); if (credentials instanceof NTCredentials) { AuthScope proxyAuthScopeNTLM = new AuthScope(address.getHostName(), address.getPort(), AuthScope.ANY_REALM, AuthPolicy.NTLM); client.getCredentialsProvider().setCredentials(proxyAuthScopeNTLM, credentials); AuthScope proxyAuthScopeAny = new AuthScope(address.getHostName(), address.getPort(), AuthScope.ANY_REALM); Credentials usernamePasswordCredentials = getCredentials(authProxy.getUserName(), authProxy.getPassword(), address.getAddress(), true); client.getCredentialsProvider().setCredentials(proxyAuthScopeAny, usernamePasswordCredentials); } else { AuthScope proxyAuthScope = new AuthScope(address.getHostName(), address.getPort(), AuthScope.ANY_REALM); client.getCredentialsProvider().setCredentials(proxyAuthScope, credentials); } } } else { client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, null); } }
From source file:hudson.plugins.ec2.EC2Cloud.java
/*** * Connect to an EC2 instance.// w w w. j a va2 s . c om * @return {@link AmazonEC2} client */ public synchronized static AmazonEC2 connect(String accessId, Secret secretKey, URL endpoint) { awsCredentials = new BasicAWSCredentials(accessId, Secret.toString(secretKey)); ClientConfiguration config = new ClientConfiguration(); ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy; Proxy proxy = proxyConfig == null ? Proxy.NO_PROXY : proxyConfig.createProxy(endpoint.getHost()); if (!proxy.equals(Proxy.NO_PROXY) && proxy.address() instanceof InetSocketAddress) { InetSocketAddress address = (InetSocketAddress) proxy.address(); config.setProxyHost(address.getHostName()); config.setProxyPort(address.getPort()); if (null != proxyConfig.getUserName()) { config.setProxyUsername(proxyConfig.getUserName()); config.setProxyPassword(proxyConfig.getPassword()); } } AmazonEC2 client = new AmazonEC2Client(awsCredentials, config); client.setEndpoint(endpoint.toString()); return client; }
From source file:gov.nih.nci.nbia.StandaloneDMDispatcher.java
static String getProxyInfo() { StringBuffer sb = new StringBuffer("Get proxy info: "); try {//from w w w . ja v a 2 s . co m System.setProperty("java.net.useSystemProxies", "true"); List<Proxy> l = ProxySelector.getDefault().select(new URI("https://www.google.com/")); for (Iterator<Proxy> iter = l.iterator(); iter.hasNext();) { Proxy proxy = iter.next(); sb.append("proxy type : " + proxy.type() + "\n"); InetSocketAddress addr = (InetSocketAddress) proxy.address(); if (addr == null) { sb.append("No Proxy\n"); } else { sb.append("proxy hostname : " + addr.getHostName() + "\n"); sb.append("proxy port : " + addr.getPort() + "\n"); } } } catch (Exception e) { e.printStackTrace(); sb.append(e.getMessage()); } return sb.toString(); }
From source file:fr.cls.atoll.motu.processor.wps.TestServiceMetadata.java
public static void DetectProxy() throws Exception { System.setProperty("proxyHost", "proxy.cls.fr"); // adresse IP System.setProperty("proxyPort", "8080"); System.setProperty("socksProxyHost", "proxy.cls.fr"); // System.setProperty("http.proxyHost", "http-proxy.ece.fr"); // System.setProperty("http.proxyPort", "3128"); // System.setProperty("java.net.useSystemProxies", "true"); // List<Proxy> proxyList = ProxySelector.getDefault().select(new URI("http://schemas.opengis.net")); List<Proxy> proxyList = ProxySelector.getDefault().select(new URI("http://opendap.aviso.oceanobs.com")); for (Proxy proxy : proxyList) { System.out.println("Proxy type : " + proxy.type()); InetSocketAddress addr = (InetSocketAddress) proxy.address(); if (addr == null) { System.out.println("DIRECT CONXN"); } else {/*from w ww . j a v a2 s. com*/ System.out.println("Proxy hostname : " + addr.getHostName() + ":" + addr.getPort()); } } }
From source file:com.servoy.extensions.plugins.http.HttpProvider.java
public static void setHttpClientProxy(DefaultHttpClient client, String url, String proxyUser, String proxyPassword) {//from w ww .j ava 2s. c o m String proxyHost = null; int proxyPort = 8080; try { System.setProperty("java.net.useSystemProxies", "true"); URI uri = new URI(url); List<Proxy> proxies = ProxySelector.getDefault().select(uri); if (proxies != null && client != null) { for (Proxy proxy : proxies) { if (proxy.address() != null && proxy.address() instanceof InetSocketAddress) { InetSocketAddress address = (InetSocketAddress) proxy.address(); proxyHost = address.getHostName(); HttpHost host = new HttpHost(address.getHostName(), address.getPort()); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, host); break; } } } } catch (Exception ex) { Debug.log(ex); } if (proxyHost == null && System.getProperty("http.proxyHost") != null && !"".equals(System.getProperty("http.proxyHost"))) { proxyHost = System.getProperty("http.proxyHost"); try { proxyPort = Integer.parseInt(System.getProperty("http.proxyPort")); } catch (Exception ex) { //ignore } HttpHost host = new HttpHost(proxyHost, proxyPort); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, host); } if (proxyUser != null) { BasicCredentialsProvider bcp = new BasicCredentialsProvider(); bcp.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); client.setCredentialsProvider(bcp); } }
From source file:org.eclipse.mylyn.commons.http.HttpUtil.java
private static void configureHttpClientProxy(AbstractHttpClient client, HttpContext context, AbstractWebLocation location) {/*w w w . j a v a 2 s .co m*/ String host = getHost(location.getUrl()); Proxy proxy; if (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(); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(address.getHostName(), address.getPort())); if (proxy instanceof AuthenticatedProxy) { AuthenticatedProxy authProxy = (AuthenticatedProxy) proxy; Credentials credentials = getCredentials(authProxy.getUserName(), authProxy.getPassword(), address.getAddress()); if (credentials instanceof NTCredentials) { List<String> authpref = new ArrayList<String>(); authpref.add(AuthPolicy.NTLM); authpref.add(AuthPolicy.BASIC); authpref.add(AuthPolicy.DIGEST); client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref); } else { List<String> authpref = new ArrayList<String>(); authpref.add(AuthPolicy.BASIC); authpref.add(AuthPolicy.DIGEST); authpref.add(AuthPolicy.NTLM); client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref); } AuthScope proxyAuthScope = new AuthScope(address.getHostName(), address.getPort(), AuthScope.ANY_REALM); client.getCredentialsProvider().setCredentials(proxyAuthScope, credentials); } } else { client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, null); } }
From source file:com.urswolfer.intellij.plugin.gerrit.rest.ProxyHttpClientBuilderExtension.java
@Override public CredentialsProvider extendCredentialProvider(HttpClientBuilder httpClientBuilder, CredentialsProvider credentialsProvider, GerritAuthData authData) { HttpConfigurable proxySettings = HttpConfigurable.getInstance(); IdeaWideProxySelector ideaWideProxySelector = new IdeaWideProxySelector(proxySettings); // This will always return at least one proxy, which can be the "NO_PROXY" instance. List<Proxy> proxies = ideaWideProxySelector.select(URI.create(authData.getHost())); // Find the first real proxy with an address type we support. for (Proxy proxy : proxies) { SocketAddress socketAddress = proxy.address(); if (HttpConfigurable.isRealProxy(proxy) && socketAddress instanceof InetSocketAddress) { InetSocketAddress address = (InetSocketAddress) socketAddress; HttpHost proxyHttpHost = new HttpHost(address.getHostName(), address.getPort()); httpClientBuilder.setProxy(proxyHttpHost); // Here we use the single username/password that we got from IDEA's settings. It feels kinda strange // to use these credential but it's probably what the user expects. if (proxySettings.PROXY_AUTHENTICATION) { AuthScope authScope = new AuthScope(proxySettings.PROXY_HOST, proxySettings.PROXY_PORT); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials( proxySettings.PROXY_LOGIN, proxySettings.getPlainProxyPassword()); credentialsProvider.setCredentials(authScope, credentials); break; }/* ww w . j a v a 2s. c om*/ } } return credentialsProvider; }