Search in sources :

Example 6 with ProxySelector

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);
        }
    }
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 7 with ProxySelector

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;
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) SecureCacheResponse(java.net.SecureCacheResponse) URI(java.net.URI)

Example 8 with ProxySelector

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;
}
Also used : ProxySelector(java.net.ProxySelector) URI(java.net.URI)

Example 9 with ProxySelector

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);
        }
    }
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) URI(java.net.URI)

Example 10 with ProxySelector

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();
    }
}
Also used : ProxySelector(java.net.ProxySelector) URI(java.net.URI)

Aggregations

ProxySelector (java.net.ProxySelector)53 URI (java.net.URI)33 Proxy (java.net.Proxy)32 InetSocketAddress (java.net.InetSocketAddress)21 IOException (java.io.IOException)19 SocketAddress (java.net.SocketAddress)13 List (java.util.List)7 InetAddress (java.net.InetAddress)6 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 HttpURLConnection (java.net.HttpURLConnection)3 URISyntaxException (java.net.URISyntaxException)3 HttpHost (org.apache.http.HttpHost)3 Test (org.junit.Test)3 ManualProxySelector (org.kse.utilities.net.ManualProxySelector)3 NoProxySelector (org.kse.utilities.net.NoProxySelector)3 PacProxySelector (org.kse.utilities.net.PacProxySelector)3 ProxyAddress (org.kse.utilities.net.ProxyAddress)3 SystemProxySelector (org.kse.utilities.net.SystemProxySelector)3 InterruptedIOException (java.io.InterruptedIOException)2