use of java.net.ProxySelector in project android_frameworks_base by ResurrectionRemix.
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 HttpURLConnectionTest method testUsingProxySelector.
/**
* Test checks if the proxy provided by proxy selector
* will be used for connection to the server
*/
public void testUsingProxySelector() throws Exception {
// Regression for HARMONY-570
MockServer server = new MockServer("server");
MockServer proxy = new MockServer("proxy");
URL url = new URL("http://localhost:" + server.port());
// keep default proxy selector
ProxySelector defPS = ProxySelector.getDefault();
// replace selector
ProxySelector.setDefault(new TestProxySelector(server.port(), proxy.port()));
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(2000);
connection.setReadTimeout(2000);
server.start();
synchronized (bound) {
if (!server.started)
bound.wait(5000);
}
proxy.start();
synchronized (bound) {
if (!proxy.started)
bound.wait(5000);
}
connection.connect();
// wait while server and proxy run
server.join();
proxy.join();
assertTrue("Connection does not use proxy", connection.usingProxy());
assertTrue("Proxy server was not used", proxy.accepted);
connection.disconnect();
assertTrue("usingProxy broken after disconnect", connection.usingProxy());
} finally {
// restore default proxy selector
ProxySelector.setDefault(defPS);
}
}
use of java.net.ProxySelector in project okhttp by square.
the class URLConnectionTest method redirectWithProxySelector.
@Test
public void redirectWithProxySelector() throws Exception {
final List<URI> proxySelectionRequests = new ArrayList<>();
urlFactory.setClient(urlFactory.client().newBuilder().proxySelector(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
proxySelectionRequests.add(uri);
MockWebServer proxyServer = (uri.getPort() == server.getPort()) ? server : server2;
return Arrays.asList(proxyServer.toProxyAddress());
}
@Override
public void connectFailed(URI uri, SocketAddress address, IOException failure) {
throw new AssertionError();
}
}).build());
server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.url("/b").url().toString()).setBody("This page has moved!"));
assertContent("This is the 2nd server!", urlFactory.open(server.url("/a").url()));
assertEquals(Arrays.asList(server.url("/").url().toURI(), server2.url("/").url().toURI()), proxySelectionRequests);
}
use of java.net.ProxySelector in project XobotOS by xamarin.
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 XobotOS by xamarin.
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;
}
Aggregations