// Result host and port are returned in a string in the format host:port public static String[] getProxyForURL(String target) { String[] proxyInfo = new String[0]; List<String> proxies = new ArrayList<String>(); URI uri = null; try { ProxySelector defaultProxySelector = ProxySelector.getDefault(); uri = new URI(target); List<Proxy> proxyList = defaultProxySelector.select(uri); Proxy proxy = proxyList.get(0); if (proxy.equals(Proxy.NO_PROXY)) { System.out.println("DalvikProxySelector.getProxyForURL(): No proxy found"); return null; } SocketAddress address = proxy.address(); InetSocketAddress inetSocketAddress = (InetSocketAddress) address; String host = inetSocketAddress.getHostName(); int port = inetSocketAddress.getPort(); if (host == null) { System.out.println("DalvikProxySelector.getProxyForURL(): No proxy found"); return null; } proxies.add(host); // even index, host proxies.add(Integer.toString(port)); // odd index, port System.out.println("DalvikProxySelector.getProxyForURL(): host=" + host + " port=" + port); return proxies.toArray(new String[0]); } catch (Exception e) { System.out.println( "DalvikProxySelector.getProxyForURL(): exception(ignored): " + e.toString()); return null; } }
/** Get or create the http client to be used to fetch all the map data. */ public HttpClient getHttpClient(URI uri) { MultiThreadedHttpConnectionManager connectionManager = getConnectionManager(); HttpClient httpClient = new HttpClient(connectionManager); // httpclient is a bit pesky about loading everything in memory... // disabling the warnings. Logger.getLogger(HttpMethodBase.class).setLevel(Level.ERROR); // configure proxies for URI ProxySelector selector = ProxySelector.getDefault(); List<Proxy> proxyList = selector.select(uri); Proxy proxy = proxyList.get(0); if (!proxy.equals(Proxy.NO_PROXY)) { InetSocketAddress socketAddress = (InetSocketAddress) proxy.address(); String hostName = socketAddress.getHostName(); int port = socketAddress.getPort(); httpClient.getHostConfiguration().setProxy(hostName, port); } for (SecurityStrategy sec : security) if (sec.matches(uri)) { sec.configure(uri, httpClient); break; } return httpClient; }
/** * * Connect to an EC2 instance. * * @return {@link AmazonEC2} client */ public static synchronized AmazonEC2 connect( AWSCredentialsProvider credentialsProvider, URL endpoint) { awsCredentialsProvider = credentialsProvider; ClientConfiguration config = new ClientConfiguration(); config.setMaxErrorRetry(16); // Default retry limit (3) is low and often // cause problems. Raise it a bit. // See: https://issues.jenkins-ci.org/browse/JENKINS-26800 config.setSignerOverride("QueryStringSignerType"); 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(credentialsProvider, config); client.setEndpoint(endpoint.toString()); return client; }