FileDocCategorySizeDatePackage
ProxySelectorImpl.javaAPI DocAndroid 1.5 API12013Wed May 06 22:41:04 BST 2009java.net

ProxySelectorImpl

public class ProxySelectorImpl extends ProxySelector
Default implementation for {@code ProxySelector}.
since
Android 1.0

Fields Summary
private static final int
HTTP_PROXY_PORT
private static final int
HTTPS_PROXY_PORT
private static final int
FTP_PROXY_PORT
private static final int
SOCKS_PROXY_PORT
private static Properties
netProps
Constructors Summary
public ProxySelectorImpl()


    // read net.properties file
     
        AccessController.doPrivileged(new java.security.PrivilegedAction() {
            public Object run() {
                File f = new File(System.getProperty("java.home") //$NON-NLS-1$
                        + File.separator + "lib" + File.separator //$NON-NLS-1$
                        + "net.properties"); //$NON-NLS-1$

                if (f.exists()) {
                    try {
                        FileInputStream fis = new FileInputStream(f);
                        // BEGIN android-modified
                        InputStream is = new BufferedInputStream(fis, 8192);
                        // END android-modified
                        netProps = new Properties();
                        netProps.load(is);
                        is.close();
                    } catch (IOException e) {
                    }
                }
                return null;
            }
        });
    
        super();
    
Methods Summary
public voidconnectFailed(java.net.URI uri, java.net.SocketAddress sa, java.io.IOException ioe)

        if (null == uri || null == sa || null == ioe) {
            // "KA001=Argument must not be null"
            throw new IllegalArgumentException(Msg.getString("KA001")); //$NON-NLS-1$
        }
    
private java.net.ProxycreateProxy(java.net.Proxy$Type type, java.lang.String host, java.lang.String port, int defaultPort)

        Proxy proxy;
        if (type == Proxy.Type.DIRECT) {
            proxy = Proxy.NO_PROXY;
        } else {
            int iPort;
            try {
                // BEGIN android-changed
                iPort = Integer.parseInt(port);
                // END android-changed
            } catch (NumberFormatException e) {
                iPort = defaultPort;
            }
            proxy = new Proxy(type, InetSocketAddress.createUnresolved(host,
                    iPort));
        }
        return proxy;
    
private java.lang.StringgetSystemProperty(java.lang.String property)

        return getSystemProperty(property, null);
    
private java.lang.StringgetSystemProperty(java.lang.String property, java.lang.String defaultValue)

        String value = AccessController.doPrivileged(new PriviAction<String>(
                property));
        if (null == value || "".equals(value)) { //$NON-NLS-1$
            value = (netProps != null)
                    ? netProps.getProperty(property, defaultValue)
                    : defaultValue;
        }
        return value;
    
private java.lang.StringgetSystemPropertyOrAlternative(java.lang.String key, java.lang.String alternativeKey, java.lang.String defaultValue)

        String value = getSystemProperty(key);
        if (value == null) {
            value = getSystemProperty(alternativeKey);
            if (null == value) {
                value = defaultValue;
            }
        }
        return value;
    
private booleanisNonProxyHost(java.lang.String host, java.lang.String nonProxyHosts)

        // nonProxyHosts is not set
        if (null == nonProxyHosts) {
            return false;
        }
        // Construct regex expression of nonProxyHosts
        int length = nonProxyHosts.length();
        char ch;
        StringBuilder buf = new StringBuilder(length);
        for (int i = 0; i < nonProxyHosts.length(); i++) {
            ch = nonProxyHosts.charAt(i);
            switch (ch) {
                case '.":
                    buf.append("\\."); //$NON-NLS-1$
                    break;
                case '*":
                    buf.append(".*"); //$NON-NLS-1$
                    break;
                default:
                    buf.append(ch);
            }
        }
        String nonProxyHostsReg = buf.toString();
        // check whether the host is the nonProxyHosts.
        return host.matches(nonProxyHostsReg);
    
public java.util.Listselect(java.net.URI uri)

        // argument check
        if (null == uri) {
            // KA001=Argument must not be null
            throw new IllegalArgumentException(Msg.getString("KA001")); //$NON-NLS-1$
        }
        // check scheme
        String scheme = uri.getScheme();
        if (null == scheme) {
            throw new IllegalArgumentException();
        }

        String host = uri.getHost();
        Proxy proxy = Proxy.NO_PROXY;

        if ("http".equals(scheme)) { //$NON-NLS-1$
            proxy = selectHttpProxy(host);
        } else if ("https".equals(scheme)) { //$NON-NLS-1$
            proxy = selectHttpsProxy();
        } else if ("ftp".equals(scheme)) { //$NON-NLS-1$
            proxy = selectFtpProxy(host);
        } else if ("socket".equals(scheme)) { //$NON-NLS-1$
            proxy = selectSocksProxy();
        }
        List<Proxy> proxyList = new ArrayList<Proxy>(1);
        proxyList.add(proxy);
        return proxyList;
    
private java.net.ProxyselectFtpProxy(java.lang.String uriHost)

        String host;
        String port = null;
        Proxy.Type type = Proxy.Type.DIRECT;
        String nonProxyHosts = getSystemProperty("ftp.nonProxyHosts"); //$NON-NLS-1$
        // if host is in non proxy host list, returns Proxy.NO_PROXY
        if (isNonProxyHost(uriHost, nonProxyHosts)) {
            return Proxy.NO_PROXY;
        }

        host = getSystemProperty("ftp.proxyHost"); //$NON-NLS-1$
        if (null != host) {
            // case 1: use exact ftp proxy
            type = Proxy.Type.HTTP;
            port = getSystemProperty(
                    "ftp.proxyPort", String.valueOf(FTP_PROXY_PORT)); //$NON-NLS-1$
        } else {
            host = getSystemProperty("socksProxyHost"); //$NON-NLS-1$
            if (null != host) {
                // case 2: use socks proxy instead
                type = Proxy.Type.SOCKS;
                port = getSystemProperty(
                        "socksProxyPort", String.valueOf(SOCKS_PROXY_PORT)); //$NON-NLS-1$
            }
        }
        int defaultPort = (type == Proxy.Type.SOCKS) ? SOCKS_PROXY_PORT
                : FTP_PROXY_PORT;
        return createProxy(type, host, port, defaultPort);
    
private java.net.ProxyselectHttpProxy(java.lang.String uriHost)

        String host;
        String port = null;
        Proxy.Type type = Proxy.Type.DIRECT;

        String nonProxyHosts = getSystemProperty("http.nonProxyHosts"); //$NON-NLS-1$
        // if host is in non proxy host list, returns Proxy.NO_PROXY
        if (isNonProxyHost(uriHost, nonProxyHosts)) {
            return Proxy.NO_PROXY;
        }

        host = getSystemProperty("http.proxyHost"); //$NON-NLS-1$
        if (null != host) {
            // case 1: http.proxyHost is set, use exact http proxy
            type = Proxy.Type.HTTP;
            port = getSystemPropertyOrAlternative("http.proxyPort", //$NON-NLS-1$
                    "proxyPort", String.valueOf(HTTP_PROXY_PORT)); //$NON-NLS-1$
        } else if ((host = getSystemProperty("proxyHost", null)) != null) { //$NON-NLS-1$
            // case 2: proxyHost is set, use exact http proxy
            type = Proxy.Type.HTTP;
            port = getSystemPropertyOrAlternative("proxyPort", //$NON-NLS-1$
                    "http.proxyPort", String.valueOf(HTTP_PROXY_PORT)); //$NON-NLS-1$

        } else if ((host = getSystemProperty("socksProxyHost")) != null) { //$NON-NLS-1$
            // case 3: use socks proxy instead
            type = Proxy.Type.SOCKS;
            port = getSystemProperty(
                    "socksProxyPort", String.valueOf(SOCKS_PROXY_PORT)); //$NON-NLS-1$
        }
        int defaultPort = (type == Proxy.Type.SOCKS) ? SOCKS_PROXY_PORT
                : HTTP_PROXY_PORT;
        return createProxy(type, host, port, defaultPort);
    
private java.net.ProxyselectHttpsProxy()

        String host;
        String port = null;
        Proxy.Type type = Proxy.Type.DIRECT;

        host = getSystemProperty("https.proxyHost"); //$NON-NLS-1$
        if (null != host) {
            // case 1: use exact https proxy
            type = Proxy.Type.HTTP;
            port = getSystemProperty(
                    "https.proxyPort", String.valueOf(HTTPS_PROXY_PORT)); //$NON-NLS-1$
        } else {
            host = getSystemProperty("socksProxyHost"); //$NON-NLS-1$
            if (null != host) {
                // case 2: use socks proxy instead
                type = Proxy.Type.SOCKS;
                port = getSystemProperty(
                        "socksProxyPort", String.valueOf(SOCKS_PROXY_PORT)); //$NON-NLS-1$
            }
        }
        int defaultPort = (type == Proxy.Type.SOCKS) ? SOCKS_PROXY_PORT
                : HTTPS_PROXY_PORT;
        return createProxy(type, host, port, defaultPort);
    
private java.net.ProxyselectSocksProxy()

        String host;
        String port = null;
        Proxy.Type type = Proxy.Type.DIRECT;

        host = getSystemProperty("socksProxyHost"); //$NON-NLS-1$
        if (null != host) {
            type = Proxy.Type.SOCKS;
            port = getSystemProperty(
                    "socksProxyPort", String.valueOf(SOCKS_PROXY_PORT)); //$NON-NLS-1$
        }
        return createProxy(type, host, port, SOCKS_PROXY_PORT);