use of java.net.ProxySelector in project robovm by robovm.
the class FtpURLConnection method connect.
/**
* Establishes the connection to the resource specified by this
* <code>URL</code>
*
* @see #connected
* @see java.io.IOException
* @see URLStreamHandler
*/
@Override
public void connect() throws IOException {
// Use system-wide ProxySelect to select proxy list,
// then try to connect via elements in the proxy list.
List<Proxy> proxyList = null;
if (proxy != null) {
proxyList = new ArrayList<Proxy>(1);
proxyList.add(proxy);
} else {
ProxySelector selector = ProxySelector.getDefault();
if (selector != null) {
proxyList = selector.select(uri);
}
}
if (proxyList == null) {
currentProxy = null;
connectInternal();
} else {
ProxySelector selector = ProxySelector.getDefault();
Iterator<Proxy> iter = proxyList.iterator();
boolean connectOK = false;
String failureReason = "";
while (iter.hasNext() && !connectOK) {
currentProxy = iter.next();
try {
connectInternal();
connectOK = true;
} catch (IOException ioe) {
failureReason = ioe.getLocalizedMessage();
// should be invoked.
if (selector != null && Proxy.NO_PROXY != currentProxy) {
selector.connectFailed(uri, currentProxy.address(), ioe);
}
}
}
if (!connectOK) {
throw new IOException("Unable to connect to server: " + failureReason);
}
}
}
use of java.net.ProxySelector in project jdk8u_jdk by JetBrains.
the class EmptyInputStream method plainConnect0.
protected void plainConnect0() throws IOException {
// try to see if request can be served from local cache
if (cacheHandler != null && getUseCaches()) {
try {
URI uri = ParseUtil.toURI(url);
if (uri != null) {
cachedResponse = cacheHandler.get(uri, getRequestMethod(), getUserSetHeaders().getHeaders());
if ("https".equalsIgnoreCase(uri.getScheme()) && !(cachedResponse instanceof SecureCacheResponse)) {
cachedResponse = null;
}
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("Cache Request for " + uri + " / " + getRequestMethod());
logger.finest("From cache: " + (cachedResponse != null ? cachedResponse.toString() : "null"));
}
if (cachedResponse != null) {
cachedHeaders = mapToMessageHeader(cachedResponse.getHeaders());
cachedInputStream = cachedResponse.getBody();
}
}
} catch (IOException ioex) {
// ignore and commence normal connection
}
if (cachedHeaders != null && cachedInputStream != null) {
connected = true;
return;
} else {
cachedResponse = null;
}
}
try {
if (instProxy == null) {
// no instance Proxy is set
/**
* 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);
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("ProxySelector Request for " + uri);
}
Iterator<Proxy> it = sel.select(uri).iterator();
Proxy p;
while (it.hasNext()) {
p = it.next();
try {
if (!failedOnce) {
http = getNewHttpClient(url, p, connectTimeout);
http.setReadTimeout(readTimeout);
} else {
// make sure to construct new connection if first
// attempt failed
http = getNewHttpClient(url, p, connectTimeout, false);
http.setReadTimeout(readTimeout);
}
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
if (p != null) {
logger.finest("Proxy used: " + p.toString());
}
}
break;
} catch (IOException ioex) {
if (p != Proxy.NO_PROXY) {
sel.connectFailed(uri, p.address(), ioex);
if (!it.hasNext()) {
// fallback to direct connection
http = getNewHttpClient(url, null, connectTimeout, false);
http.setReadTimeout(readTimeout);
break;
}
} else {
throw ioex;
}
continue;
}
}
} else {
// No proxy selector, create http client with no proxy
if (!failedOnce) {
http = getNewHttpClient(url, null, connectTimeout);
http.setReadTimeout(readTimeout);
} else {
// make sure to construct new connection if first
// attempt failed
http = getNewHttpClient(url, null, connectTimeout, false);
http.setReadTimeout(readTimeout);
}
}
} else {
if (!failedOnce) {
http = getNewHttpClient(url, instProxy, connectTimeout);
http.setReadTimeout(readTimeout);
} else {
// make sure to construct new connection if first
// attempt failed
http = getNewHttpClient(url, instProxy, connectTimeout, false);
http.setReadTimeout(readTimeout);
}
}
ps = (PrintStream) http.getOutputStream();
} catch (IOException e) {
throw e;
}
// constructor to HTTP client calls openserver
connected = true;
}
use of java.net.ProxySelector in project android_frameworks_base by AOSPA.
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 jdk8u_jdk by JetBrains.
the class B6737819 method main.
public static void main(String[] args) throws Exception {
System.setProperty("http.proxyHost", "myproxy");
System.setProperty("http.proxyPort", "8080");
ProxySelector sel = ProxySelector.getDefault();
java.util.List<Proxy> l;
// from going through the HTTP proxy
for (String s : uris) {
l = sel.select(new URI(s));
if (l.size() == 1 && l.get(0).type() != Proxy.Type.DIRECT) {
throw new RuntimeException("ProxySelector returned the wrong proxy for " + s);
}
}
// Let's override the default nonProxyHosts and make sure we now get a
// HTTP proxy
System.setProperty("http.nonProxyHosts", "");
for (String s : uris) {
l = sel.select(new URI(s));
if (l.size() == 1 && l.get(0).type() != Proxy.Type.HTTP) {
throw new RuntimeException("ProxySelector returned the wrong proxy for " + s);
}
}
}
use of java.net.ProxySelector in project jdk8u_jdk by JetBrains.
the class MultiThreadedSystemProxies method main.
public static void main(String[] args) throws Exception {
System.setProperty("java.net.useSystemProxies", "true");
final ProxySelector ps = ProxySelector.getDefault();
final URI uri = new URI("http://ubuntu.com");
Thread[] threads = new Thread[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
threads[i] = new Thread(new Runnable() {
@Override
public void run() {
try {
ps.select(uri);
} catch (Exception x) {
throw new RuntimeException(x);
}
}
});
}
for (int i = 0; i < NUM_THREADS; i++) {
threads[i].start();
}
for (int i = 0; i < NUM_THREADS; i++) {
threads[i].join();
}
}
Aggregations