Search in sources :

Example 31 with ProxySelector

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

Example 32 with ProxySelector

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

Example 33 with ProxySelector

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;
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) HttpHost(org.apache.http.HttpHost) InetSocketAddress(java.net.InetSocketAddress) HttpException(org.apache.http.HttpException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 34 with ProxySelector

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

Example 35 with ProxySelector

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) FtpProtocolException(sun.net.ftp.FtpProtocolException) URI(java.net.URI) ProxySelector(java.net.ProxySelector) FtpProtocolException(sun.net.ftp.FtpProtocolException) Proxy(java.net.Proxy) GetPropertyAction(sun.security.action.GetPropertyAction) HttpURLConnection(sun.net.www.protocol.http.HttpURLConnection)

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