FileDocCategorySizeDatePackage
ProxySelectorRoutePlanner.javaAPI DocAndroid 1.5 API10056Wed May 06 22:41:10 BST 2009org.apache.http.impl.conn

ProxySelectorRoutePlanner

public class ProxySelectorRoutePlanner extends Object implements HttpRoutePlanner
Default implementation of an {@link HttpRoutePlanner}. This implementation is based on {@link java.net.ProxySelector}. By default, it will pick up the proxy settings of the JVM, either from system properties or from the browser running the application. Additionally, it interprets some {@link org.apache.http.conn.params.ConnRoutePNames parameters}, though not the {@link org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY}.

Fields Summary
protected SchemeRegistry
schemeRegistry
The scheme registry.
protected ProxySelector
proxySelector
The proxy selector to use, or null for system default.
Constructors Summary
public ProxySelectorRoutePlanner(SchemeRegistry schreg, ProxySelector prosel)
Creates a new proxy selector route planner.

param
schreg the scheme registry
param
prosel the proxy selector, or null for the system default


        if (schreg == null) {
            throw new IllegalArgumentException
                ("SchemeRegistry must not be null.");
        }
        schemeRegistry = schreg;
        proxySelector  = prosel;
    
Methods Summary
protected java.net.ProxychooseProxy(java.util.List proxies, org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context)


        if ((proxies == null) || proxies.isEmpty()) {
            throw new IllegalArgumentException
                ("Proxy list must not be empty.");
        }

        Proxy result = null;

        // check the list for one we can use
        for (int i=0; (result == null) && (i < proxies.size()); i++) {

            Proxy p = proxies.get(i);
            switch (p.type()) {

            case DIRECT:
            case HTTP:
                result = p;
                break;

            case SOCKS:
                // SOCKS hosts are not handled on the route level.
                // The socket may make use of the SOCKS host though.
                break;
            }
        }

        if (result == null) {
            //@@@ log as warning or info that only a socks proxy is available?
            // result can only be null if all proxies are socks proxies
            // socks proxies are not handled on the route planning level
            result = Proxy.NO_PROXY;
        }

        return result;
    
protected org.apache.http.HttpHostdetermineProxy(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context)
Determines a proxy for the given target.

param
target the planned target, never null
param
request the request to be sent, never null
param
context the context, or null
return
the proxy to use, or null for a direct route
throws
HttpException in case of system proxy settings that cannot be handled


        // 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;
    
public org.apache.http.conn.routing.HttpRoutedetermineRoute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context)


        if (request == null) {
            throw new IllegalStateException
                ("Request must not be null.");
        }

        // If we have a forced route, we can do without a target.
        HttpRoute route =
            ConnRouteParams.getForcedRoute(request.getParams());
        if (route != null)
            return route;

        // If we get here, there is no forced route.
        // So we need a target to compute a route.

        if (target == null) {
            throw new IllegalStateException
                ("Target host must not be null.");
        }

        final InetAddress local =
            ConnRouteParams.getLocalAddress(request.getParams());
        final HttpHost proxy = determineProxy(target, request, context);

        final Scheme schm =
            this.schemeRegistry.getScheme(target.getSchemeName());
        // as it is typically used for TLS/SSL, we assume that
        // a layered scheme implies a secure connection
        final boolean secure = schm.isLayered();

        if (proxy == null) {
            route = new HttpRoute(target, local, secure);
        } else {
            route = new HttpRoute(target, local, proxy, secure);
        }
        return route;
    
protected java.lang.StringgetHost(java.net.InetSocketAddress isa)
Obtains a host from an {@link InetSocketAddress}.

param
isa the socket address
return
a host string, either as a symbolic name or as a literal IP address string
(TODO: determine format for IPv6 addresses, with or without [brackets])


        //@@@ Will this work with literal IPv6 addresses, or do we
        //@@@ need to wrap these in [] for the string representation?
        //@@@ Having it in this method at least allows for easy workarounds.
       return isa.isUnresolved() ?
            isa.getHostName() : isa.getAddress().getHostAddress();
 
    
public java.net.ProxySelectorgetProxySelector()
Obtains the proxy selector to use.

return
the proxy selector, or null for the system default

        return this.proxySelector;
    
public voidsetProxySelector(java.net.ProxySelector prosel)
Sets the proxy selector to use.

param
prosel the proxy selector, or null to use the system default

        this.proxySelector = prosel;