use of java.net.ProxySelector in project XobotOS by xamarin.
the class HttpConnection method connect.
public static HttpConnection connect(URI uri, Proxy proxy, boolean requiresTunnel, int connectTimeout) throws IOException {
/*
* Try an explicitly-specified proxy.
*/
if (proxy != null) {
Address address = (proxy.type() == Proxy.Type.DIRECT) ? new Address(uri) : new Address(uri, proxy, requiresTunnel);
return HttpConnectionPool.INSTANCE.get(address, connectTimeout);
}
/*
* Try connecting to each of the proxies provided by the ProxySelector
* until a connection succeeds.
*/
ProxySelector selector = ProxySelector.getDefault();
List<Proxy> proxyList = selector.select(uri);
if (proxyList != null) {
for (Proxy selectedProxy : proxyList) {
if (selectedProxy.type() == Proxy.Type.DIRECT) {
// TODO: if the selector recommends a direct connection, attempt that?
continue;
}
try {
Address address = new Address(uri, selectedProxy, requiresTunnel);
return HttpConnectionPool.INSTANCE.get(address, connectTimeout);
} catch (IOException e) {
// failed to connect, tell it to the selector
selector.connectFailed(uri, selectedProxy.address(), e);
}
}
}
/*
* Try a direct connection. If this fails, this method will throw.
*/
return HttpConnectionPool.INSTANCE.get(new Address(uri), connectTimeout);
}
use of java.net.ProxySelector in project android_frameworks_base by DirtyUnicorns.
the class Proxy method getProxy.
/**
* Return the proxy object to be used for the URL given as parameter.
* @param ctx A Context used to get the settings for the proxy host.
* @param url A URL to be accessed. Used to evaluate exclusion list.
* @return Proxy (java.net) object containing the host name. If the
* user did not set a hostname it returns the default host.
* A null value means that no host is to be used.
* {@hide}
*/
public static final java.net.Proxy getProxy(Context ctx, String url) {
String host = "";
if ((url != null) && !isLocalHost(host)) {
URI uri = URI.create(url);
ProxySelector proxySelector = ProxySelector.getDefault();
List<java.net.Proxy> proxyList = proxySelector.select(uri);
if (proxyList.size() > 0) {
return proxyList.get(0);
}
}
return java.net.Proxy.NO_PROXY;
}
use of java.net.ProxySelector in project robovm by robovm.
the class ProxySelectorRoutePlanner method determineProxy.
/**
* Determines a proxy for the given target.
*
* @param target the planned target, never <code>null</code>
* @param request the request to be sent, never <code>null</code>
* @param context the context, or <code>null</code>
*
* @return the proxy to use, or <code>null</code> for a direct route
*
* @throws HttpException
* in case of system proxy settings that cannot be handled
*/
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
// the proxy selector can be 'unset', so we better deal with null here
ProxySelector psel = this.proxySelector;
if (psel == null)
psel = ProxySelector.getDefault();
if (psel == null)
return null;
URI targetURI = null;
try {
targetURI = new URI(target.toURI());
} catch (URISyntaxException usx) {
throw new HttpException("Cannot convert host to URI: " + target, usx);
}
List<Proxy> proxies = psel.select(targetURI);
Proxy p = chooseProxy(proxies, target, request, context);
HttpHost result = null;
if (p.type() == Proxy.Type.HTTP) {
// convert the socket address to an HttpHost
if (!(p.address() instanceof InetSocketAddress)) {
throw new HttpException("Unable to handle non-Inet proxy address: " + p.address());
}
final InetSocketAddress isa = (InetSocketAddress) p.address();
// assume default scheme (http)
result = new HttpHost(getHost(isa), isa.getPort());
}
return result;
}
use of java.net.ProxySelector in project gerrit by GerritCodeReview.
the class LibraryDownloader method openHttpStream.
private static InputStream openHttpStream(String urlStr) throws IOException {
ProxySelector proxySelector = ProxySelector.getDefault();
URL url = new URL(urlStr);
Proxy proxy = HttpSupport.proxyFor(proxySelector, url);
HttpURLConnection c = (HttpURLConnection) url.openConnection(proxy);
switch(HttpSupport.response(c)) {
case HttpURLConnection.HTTP_OK:
return c.getInputStream();
case HttpURLConnection.HTTP_NOT_FOUND:
throw new FileNotFoundException(url.toString());
default:
throw new IOException(url.toString() + ": " + HttpSupport.response(c) + " " + c.getResponseMessage());
}
}
use of java.net.ProxySelector in project jdk8u_jdk by JetBrains.
the class FtpURLConnection method connect.
/**
* Connects to the FTP server and logs in.
*
* @throws FtpLoginException if the login is unsuccessful
* @throws FtpProtocolException if an error occurs
* @throws UnknownHostException if trying to connect to an unknown host
*/
public synchronized void connect() throws IOException {
if (connected) {
return;
}
Proxy p = null;
if (instProxy == null) {
// no per connection proxy specified
/**
* Do we have to use a proxy?
*/
ProxySelector sel = java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<ProxySelector>() {
public ProxySelector run() {
return ProxySelector.getDefault();
}
});
if (sel != null) {
URI uri = sun.net.www.ParseUtil.toURI(url);
Iterator<Proxy> it = sel.select(uri).iterator();
while (it.hasNext()) {
p = it.next();
if (p == null || p == Proxy.NO_PROXY || p.type() == Proxy.Type.SOCKS) {
break;
}
if (p.type() != Proxy.Type.HTTP || !(p.address() instanceof InetSocketAddress)) {
sel.connectFailed(uri, p.address(), new IOException("Wrong proxy type"));
continue;
}
// OK, we have an http proxy
InetSocketAddress paddr = (InetSocketAddress) p.address();
try {
http = new HttpURLConnection(url, p);
http.setDoInput(getDoInput());
http.setDoOutput(getDoOutput());
if (connectTimeout >= 0) {
http.setConnectTimeout(connectTimeout);
}
if (readTimeout >= 0) {
http.setReadTimeout(readTimeout);
}
http.connect();
connected = true;
return;
} catch (IOException ioe) {
sel.connectFailed(uri, paddr, ioe);
http = null;
}
}
}
} else {
// per connection proxy specified
p = instProxy;
if (p.type() == Proxy.Type.HTTP) {
http = new HttpURLConnection(url, instProxy);
http.setDoInput(getDoInput());
http.setDoOutput(getDoOutput());
if (connectTimeout >= 0) {
http.setConnectTimeout(connectTimeout);
}
if (readTimeout >= 0) {
http.setReadTimeout(readTimeout);
}
http.connect();
connected = true;
return;
}
}
if (user == null) {
user = "anonymous";
String vers = java.security.AccessController.doPrivileged(new GetPropertyAction("java.version"));
password = java.security.AccessController.doPrivileged(new GetPropertyAction("ftp.protocol.user", "Java" + vers + "@"));
}
try {
ftp = FtpClient.create();
if (p != null) {
ftp.setProxy(p);
}
setTimeouts();
if (port != -1) {
ftp.connect(new InetSocketAddress(host, port));
} else {
ftp.connect(new InetSocketAddress(host, FtpClient.defaultPort()));
}
} catch (UnknownHostException e) {
// Just keep throwing for now.
throw e;
} catch (FtpProtocolException fe) {
throw new IOException(fe);
}
try {
ftp.login(user, password == null ? null : password.toCharArray());
} catch (sun.net.ftp.FtpProtocolException e) {
ftp.close();
// Backward compatibility
throw new sun.net.ftp.FtpLoginException("Invalid username/password");
}
connected = true;
}
Aggregations