From 8b5642ae925aed532e29aba6c2127e0bcf43f05f Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sun, 28 Aug 2016 13:03:02 +0000 Subject: [PATCH] Support changing system default ProxySelector Contributed by Robin Stevens The `ProxySelectorRoutePlanner` class which got deprecated in favor of the `SystemDefaultRoutePlanner` could deal with: - `null` as default `ProxySelector` - a change in the default `ProxySelector` This change ports that behavior to the `SystemDefaultRoutePlanner`. git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.5.x@1758107 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/conn/SystemDefaultRoutePlanner.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/SystemDefaultRoutePlanner.java b/httpclient/src/main/java/org/apache/http/impl/conn/SystemDefaultRoutePlanner.java index 6b872b9eb..30de52c19 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/SystemDefaultRoutePlanner.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/SystemDefaultRoutePlanner.java @@ -54,13 +54,19 @@ public class SystemDefaultRoutePlanner extends DefaultRoutePlanner { private final ProxySelector proxySelector; + /** + * @param proxySelector the proxy selector, or {@code null} for the system default + */ public SystemDefaultRoutePlanner( final SchemePortResolver schemePortResolver, final ProxySelector proxySelector) { super(schemePortResolver); - this.proxySelector = proxySelector != null ? proxySelector : ProxySelector.getDefault(); + this.proxySelector = proxySelector; } + /** + * @param proxySelector the proxy selector, or {@code null} for the system default + */ public SystemDefaultRoutePlanner(final ProxySelector proxySelector) { this(null, proxySelector); } @@ -76,7 +82,15 @@ public class SystemDefaultRoutePlanner extends DefaultRoutePlanner { } catch (final URISyntaxException ex) { throw new HttpException("Cannot convert host to URI: " + target, ex); } - final List proxies = this.proxySelector.select(targetURI); + ProxySelector proxySelectorInstance = this.proxySelector; + if (proxySelectorInstance == null) { + proxySelectorInstance = ProxySelector.getDefault(); + } + if (proxySelectorInstance == null) { + //The proxy selector can be "unset", so we must be able to deal with a null selector + return null; + } + final List proxies = proxySelectorInstance.select(targetURI); final Proxy p = chooseProxy(proxies); HttpHost result = null; if (p.type() == Proxy.Type.HTTP) {