1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/ProxySelectorRoutePlanner.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 658785 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-05-21 10:47:40 -0700 (Wed, 21 May 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * or more contributor license agreements.  See the NOTICE file
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed with this work for additional information
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * regarding copyright ownership.  The ASF licenses this file
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * to you under the Apache License, Version 2.0 (the
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "License"); you may not use this file except in compliance
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * with the License.  You may obtain a copy of the License at
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *   http://www.apache.org/licenses/LICENSE-2.0
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing,
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * software distributed under the License is distributed on an
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * KIND, either express or implied.  See the License for the
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * specific language governing permissions and limitations
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * under the License.
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.impl.conn;
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.net.InetAddress;
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.net.InetSocketAddress;
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.net.Proxy;
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.net.ProxySelector;
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.net.URI;
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.net.URISyntaxException;
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.List;
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpException;
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpHost;
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpRequest;
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.protocol.HttpContext;
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.routing.HttpRoute;
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.routing.HttpRoutePlanner;
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.scheme.Scheme;
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.scheme.SchemeRegistry;
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.params.ConnRouteParams;
54e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilsonimport org.apache.http.conn.params.ConnRoutePNames;
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Default implementation of an {@link HttpRoutePlanner}.
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This implementation is based on {@link java.net.ProxySelector}.
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * By default, it will pick up the proxy settings of the JVM, either
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * from system properties or from the browser running the application.
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Additionally, it interprets some
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * {@link org.apache.http.conn.params.ConnRoutePNames parameters},
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * though not the {@link
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY}.
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class ProxySelectorRoutePlanner implements HttpRoutePlanner {
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The scheme registry. */
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected SchemeRegistry schemeRegistry;
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The proxy selector to use, or <code>null</code> for system default. */
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected ProxySelector proxySelector;
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Creates a new proxy selector route planner.
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param schreg    the scheme registry
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param prosel    the proxy selector, or
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  <code>null</code> for the system default
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public ProxySelectorRoutePlanner(SchemeRegistry schreg,
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                     ProxySelector prosel) {
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (schreg == null) {
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("SchemeRegistry must not be null.");
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        schemeRegistry = schreg;
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        proxySelector  = prosel;
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Obtains the proxy selector to use.
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the proxy selector, or <code>null</code> for the system default
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public ProxySelector getProxySelector() {
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.proxySelector;
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Sets the proxy selector to use.
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param prosel    the proxy selector, or
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  <code>null</code> to use the system default
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setProxySelector(ProxySelector prosel) {
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.proxySelector = prosel;
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see interface HttpRoutePlanner
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public HttpRoute determineRoute(HttpHost target,
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                    HttpRequest request,
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                    HttpContext context)
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        throws HttpException {
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (request == null) {
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Request must not be null.");
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // If we have a forced route, we can do without a target.
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        HttpRoute route =
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            ConnRouteParams.getForcedRoute(request.getParams());
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (route != null)
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return route;
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // If we get here, there is no forced route.
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // So we need a target to compute a route.
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (target == null) {
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Target host must not be null.");
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final InetAddress local =
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            ConnRouteParams.getLocalAddress(request.getParams());
144e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson
145e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson        // BEGIN android-changed
146e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson        //     If the client or request explicitly specifies a proxy (or no
147e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson        //     proxy), prefer that over the ProxySelector's VM-wide default.
148e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson        HttpHost proxy = (HttpHost) request.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY);
149e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson        if (proxy == null) {
150e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson            proxy = determineProxy(target, request, context);
151e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson        } else if (ConnRouteParams.NO_HOST.equals(proxy)) {
152e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson            // value is explicitly unset
153e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson            proxy = null;
154e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson        }
155e7c14c45c80070fee70451deb8a0a53de66e5a43Jesse Wilson        // END android-changed
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final Scheme schm =
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            this.schemeRegistry.getScheme(target.getSchemeName());
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // as it is typically used for TLS/SSL, we assume that
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // a layered scheme implies a secure connection
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final boolean secure = schm.isLayered();
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (proxy == null) {
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            route = new HttpRoute(target, local, secure);
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            route = new HttpRoute(target, local, proxy, secure);
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return route;
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Determines a proxy for the given target.
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param target    the planned target, never <code>null</code>
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param request   the request to be sent, never <code>null</code>
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param context   the context, or <code>null</code>
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  the proxy to use, or <code>null</code> for a direct route
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws HttpException
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *         in case of system proxy settings that cannot be handled
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected HttpHost determineProxy(HttpHost    target,
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                      HttpRequest request,
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                      HttpContext context)
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        throws HttpException {
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // the proxy selector can be 'unset', so we better deal with null here
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        ProxySelector psel = this.proxySelector;
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (psel == null)
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            psel = ProxySelector.getDefault();
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (psel == null)
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return null;
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        URI targetURI = null;
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        try {
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            targetURI = new URI(target.toURI());
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } catch (URISyntaxException usx) {
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new HttpException
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Cannot convert host to URI: " + target, usx);
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        List<Proxy> proxies = psel.select(targetURI);
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        Proxy p = chooseProxy(proxies, target, request, context);
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        HttpHost result = null;
208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (p.type() == Proxy.Type.HTTP) {
209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            // convert the socket address to an HttpHost
210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (!(p.address() instanceof InetSocketAddress)) {
211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw new HttpException
212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    ("Unable to handle non-Inet proxy address: "+p.address());
213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final InetSocketAddress isa = (InetSocketAddress) p.address();
215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            // assume default scheme (http)
216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            result = new HttpHost(getHost(isa), isa.getPort());
217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return result;
220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
224069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Obtains a host from an {@link InetSocketAddress}.
225069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
226069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param isa       the socket address
227069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
228069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  a host string, either as a symbolic name or
229069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          as a literal IP address string
230069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <br/>
231069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * (TODO: determine format for IPv6 addresses, with or without [brackets])
232069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
233069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected String getHost(InetSocketAddress isa) {
234069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
235069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        //@@@ Will this work with literal IPv6 addresses, or do we
236069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        //@@@ need to wrap these in [] for the string representation?
237069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        //@@@ Having it in this method at least allows for easy workarounds.
238069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project       return isa.isUnresolved() ?
239069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            isa.getHostName() : isa.getAddress().getHostAddress();
240069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
241069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
242069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
243069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
244069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /*
245069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Chooses a proxy from a list of available proxies.
246069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The default implementation just picks the first non-SOCKS proxy
247069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * from the list. If there are only SOCKS proxies,
248069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * {@link Proxy#NO_PROXY Proxy.NO_PROXY} is returned.
249069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Derived classes may implement more advanced strategies,
250069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * such as proxy rotation if there are multiple options.
251069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
252069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param proxies   the list of proxies to choose from,
253069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  never <code>null</code> or empty
254069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param target    the planned target, never <code>null</code>
255069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param request   the request to be sent, never <code>null</code>
256069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param context   the context, or <code>null</code>
257069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
258069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  a proxy of type {@link Proxy.Type#DIRECT DIRECT}
259069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          or {@link Proxy.Type#HTTP HTTP}, never <code>null</code>
260069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
261069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected Proxy chooseProxy(List<Proxy> proxies,
262069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                HttpHost    target,
263069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                HttpRequest request,
264069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                HttpContext context) {
265069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
266069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((proxies == null) || proxies.isEmpty()) {
267069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
268069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Proxy list must not be empty.");
269069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
270069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
271069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        Proxy result = null;
272069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
273069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // check the list for one we can use
274069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        for (int i=0; (result == null) && (i < proxies.size()); i++) {
275069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
276069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            Proxy p = proxies.get(i);
277069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            switch (p.type()) {
278069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
279069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            case DIRECT:
280069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            case HTTP:
281069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                result = p;
282069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                break;
283069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
284069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            case SOCKS:
285069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                // SOCKS hosts are not handled on the route level.
286069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                // The socket may make use of the SOCKS host though.
287069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                break;
288069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
289069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
290069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
291069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (result == null) {
292069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            //@@@ log as warning or info that only a socks proxy is available?
293069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            // result can only be null if all proxies are socks proxies
294069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            // socks proxies are not handled on the route planning level
295069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            result = Proxy.NO_PROXY;
296069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
297069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
298069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return result;
299069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
300069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
301069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} // class ProxySelectorRoutePlanner
302069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
303