List of usage examples for java.net Proxy address
public SocketAddress address()
From source file:Main.java
public static void main(String[] args) throws Exception { System.setProperty("java.net.useSystemProxies", "true"); List l = ProxySelector.getDefault().select(new URI("http://www.yahoo.com/")); for (Iterator iter = l.iterator(); iter.hasNext();) { Proxy proxy = (Proxy) iter.next(); System.out.println("proxy hostname : " + proxy.type()); InetSocketAddress addr = (InetSocketAddress) proxy.address(); if (addr == null) { System.out.println("No Proxy"); } else {/*ww w . j ava 2 s .c o m*/ System.out.println("proxy hostname : " + addr.getHostName()); System.out.println("proxy port : " + addr.getPort()); } } }
From source file:com.facebook.presto.jdbc.QueryExecutor.java
@Nullable private static HttpHost getSystemSocksProxy() { URI uri = URI.create("socket://0.0.0.0:80"); for (Proxy proxy : ProxySelector.getDefault().select(uri)) { if (proxy.type() == Proxy.Type.SOCKS) { if (proxy.address() instanceof InetSocketAddress) { InetSocketAddress address = (InetSocketAddress) proxy.address(); return new HttpHost(address.getHostName(), address.getPort()); }// ww w . j av a 2 s.c om } } return null; }
From source file:eu.delving.sip.base.HttpClientFactory.java
private static void handleProxy(String serverUrl, HttpClientBuilder builder) { try {/*from ww w . j av a 2 s . co m*/ List<Proxy> proxies = ProxySelector.getDefault().select(new URI(serverUrl)); for (Proxy proxy : proxies) { if (proxy.type() != Proxy.Type.HTTP) continue; InetSocketAddress addr = (InetSocketAddress) proxy.address(); String host = addr.getHostName(); int port = addr.getPort(); builder.setProxy(new HttpHost(host, port)); } } catch (URISyntaxException e) { throw new RuntimeException("Bad address: " + serverUrl, e); } }
From source file:eu.esdihumboldt.util.http.client.fluent.FluentProxyUtil.java
/** * setup the given request object to go via proxy * // w ww . java 2 s . c o m * @param request A fluent request * @param proxy applying proxy to the fluent request * @return Executor, for a fluent request */ public static Executor setProxy(Request request, Proxy proxy) { ProxyUtil.init(); Executor executor = Executor.newInstance(); if (proxy != null && proxy.type() == Type.HTTP) { InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); // set the proxy HttpHost proxyHost = new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort()); request.viaProxy(proxyHost); String userName = System.getProperty("http.proxyUser"); String password = System.getProperty("http.proxyPassword"); boolean useProxyAuth = userName != null && !userName.isEmpty(); if (useProxyAuth) { Credentials cred = ClientProxyUtil.createCredentials(userName, password); executor.auth(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), cred); } } return executor; }
From source file:com.helger.httpclient.HttpClientHelper.java
@Nullable public static HttpHost createHttpHost(@Nullable final Proxy aProxy) { if (aProxy != null && aProxy.type() == Proxy.Type.HTTP) { if (aProxy.address() instanceof InetSocketAddress) { final InetSocketAddress aISA = (InetSocketAddress) aProxy.address(); return new HttpHost(aISA.getHostName(), aISA.getPort()); }//from w w w . j a v a 2 s. c o m } return null; }
From source file:com.zimbra.common.httpclient.HttpProxyConfig.java
public static ProxyHostConfiguration getProxyConfig(HostConfiguration hc, String uriStr) { if (!LC.client_use_system_proxy.booleanValue()) return null; URI uri = null;/*from w w w . j ava 2 s .com*/ try { uri = new URI(uriStr); } catch (URISyntaxException x) { ZimbraLog.net.info(uriStr, x); return null; } //no need to filter out localhost as the DefaultProxySelector will do that. List<Proxy> proxies = ProxySelectors.defaultProxySelector().select(uri); for (Proxy proxy : proxies) { switch (proxy.type()) { case DIRECT: return null; case HTTP: InetSocketAddress addr = (InetSocketAddress) proxy.address(); if (ZimbraLog.net.isDebugEnabled()) { ZimbraLog.net.debug("URI %s to use HTTP proxy %s", safePrint(uri), addr.toString()); } ProxyHostConfiguration nhc = new ProxyHostConfiguration(hc); nhc.setProxy(addr.getHostName(), addr.getPort()); if (proxy instanceof AuthProxy) { nhc.setUsername(((AuthProxy) proxy).getUsername()); nhc.setPassword(((AuthProxy) proxy).getPassword()); } return nhc; case SOCKS: //socks proxy can be handled at socket factory level default: continue; } } return null; }
From source file:com.blackducksoftware.integration.hub.jenkins.helper.BuildHelper.java
public static HubIntRestService getRestService(final IntLogger logger, final String serverUrl, final String username, final String password, final int hubTimeout) throws BDJenkinsHubPluginException, HubIntegrationException, URISyntaxException, MalformedURLException, BDRestException { final HubIntRestService service = new HubIntRestService(serverUrl); service.setLogger(logger);//from w w w. jav a 2s. com service.setTimeout(hubTimeout); final Jenkins jenkins = Jenkins.getInstance(); if (jenkins != null) { final ProxyConfiguration proxyConfig = jenkins.proxy; if (proxyConfig != null) { final URL actualUrl = new URL(serverUrl); final Proxy proxy = ProxyConfiguration.createProxy(actualUrl.getHost(), proxyConfig.name, proxyConfig.port, proxyConfig.noProxyHost); if (proxy.address() != null) { final InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); if (StringUtils.isNotBlank(proxyAddress.getHostName()) && proxyAddress.getPort() != 0) { if (StringUtils.isNotBlank(jenkins.proxy.getUserName()) && StringUtils.isNotBlank(jenkins.proxy.getPassword())) { service.setProxyProperties(proxyAddress.getHostName(), proxyAddress.getPort(), null, jenkins.proxy.getUserName(), jenkins.proxy.getPassword()); } else { service.setProxyProperties(proxyAddress.getHostName(), proxyAddress.getPort(), null, null, null); } if (logger != null) { logger.debug("Using proxy: '" + proxyAddress.getHostName() + "' at Port: '" + proxyAddress.getPort() + "'"); } } } } } if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) { service.setCookies(username, password); } return service; }
From source file:eu.esdihumboldt.util.http.client.ClientProxyUtil.java
/** * Set-up the given HTTP client to use the given proxy * // ww w . j ava 2s .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) { ProxyUtil.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()), createCredentials(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:es.eucm.eadventure.editor.plugin.vignette.ProxySetup.java
private static void init() { proxyConfig = null;/*from ww w. j a v a 2 s .com*/ log = Logger.getLogger("es.eucm.eadventure.tracking.prv.service.ProxyConfig"); handler = null; try { handler = new FileHandler("proxy.log"); log.addHandler(handler); } catch (SecurityException e) { handler = null; e.printStackTrace(); } catch (IOException e) { handler = null; e.printStackTrace(); } //Log log = LogFactory.getLog( "es.eucm.eadventure.tracking.prv.service.ProxyConfig" ); log("Setting prop java.net.useSystemProxies=true"); System.setProperty("java.net.useSystemProxies", "true"); Proxy proxy = getProxy(); if (proxy != null) { InetSocketAddress addr = (InetSocketAddress) proxy.address(); if (addr == null) { log("No proxy detected"); System.out.println("NO PROXY"); } else { String host = addr.getHostName(); int port = addr.getPort(); proxyConfig = new ProxyConfig(); proxyConfig.setHostName(host); proxyConfig.setPort("" + port); proxyConfig.setProtocol("http"); log("Proxy detected: host=" + host + " port=" + port); System.setProperty("java.net.useSystemProxies", "false"); System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", "" + port); log("Setting up system proxy host & port"); } } System.setProperty("java.net.useSystemProxies", "false"); log("Setting prop java.net.useSystemProxies=false"); init = true; }
From source file:jp.go.nict.langrid.management.logic.service.HttpClientUtil.java
/** * /* w w w .j a va 2 s . c om*/ * */ 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()); break; } } catch (URISyntaxException e) { e.printStackTrace(); } return client; }