@@ -92,7 +92,7 @@ public class AsyncHttpClientConfig {
9292 protected boolean allowPoolingConnection ;
9393 protected ScheduledExecutorService reaper ;
9494 protected ExecutorService applicationThreadPool ;
95- protected ProxyServer proxyServer ;
95+ protected ProxyServerSelector proxyServerSelector ;
9696 protected SSLContext sslContext ;
9797 protected SSLEngineFactory sslEngineFactory ;
9898 protected AsyncHttpProviderConfig <?, ?> providerConfig ;
@@ -136,7 +136,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
136136 boolean keepAlive ,
137137 ScheduledExecutorService reaper ,
138138 ExecutorService applicationThreadPool ,
139- ProxyServer proxyServer ,
139+ ProxyServerSelector proxyServerSelector ,
140140 SSLContext sslContext ,
141141 SSLEngineFactory sslEngineFactory ,
142142 AsyncHttpProviderConfig <?, ?> providerConfig ,
@@ -196,7 +196,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
196196 } else {
197197 this .applicationThreadPool = applicationThreadPool ;
198198 }
199- this .proxyServer = proxyServer ;
199+ this .proxyServerSelector = proxyServerSelector ;
200200 this .useRawUrl = useRawUrl ;
201201 this .spdyEnabled = spdyEnabled ;
202202 this .spdyInitialWindowSize = spdyInitialWindowSize ;
@@ -362,8 +362,8 @@ public boolean isManagedExecutorService() {
362362 *
363363 * @return instance of {@link ProxyServer}
364364 */
365- public ProxyServer getProxyServer () {
366- return proxyServer ;
365+ public ProxyServerSelector getProxyServerSelector () {
366+ return proxyServerSelector ;
367367 }
368368
369369 /**
@@ -613,11 +613,12 @@ public static class Builder {
613613 private boolean compressionEnabled = Boolean .getBoolean (ASYNC_CLIENT + "compressionEnabled" );
614614 private String userAgent = System .getProperty (ASYNC_CLIENT + "userAgent" , "AsyncHttpClient/" + AHC_VERSION );
615615 private boolean useProxyProperties = Boolean .getBoolean (ASYNC_CLIENT + "useProxyProperties" );
616+ private boolean useProxySelector = Boolean .getBoolean (ASYNC_CLIENT + "useProxySelector" );
616617 private boolean allowPoolingConnection = true ;
617618 private boolean useRelativeURIsWithSSLProxies = Boolean .getBoolean (ASYNC_CLIENT + "useRelativeURIsWithSSLProxies" );
618619 private ScheduledExecutorService reaper ;
619620 private ExecutorService applicationThreadPool ;
620- private ProxyServer proxyServer = null ;
621+ private ProxyServerSelector proxyServerSelector = null ;
621622 private SSLContext sslContext ;
622623 private SSLEngineFactory sslEngineFactory ;
623624 private AsyncHttpProviderConfig <?, ?> providerConfig ;
@@ -816,14 +817,25 @@ public Builder setExecutorService(ExecutorService applicationThreadPool) {
816817 return this ;
817818 }
818819
820+ /**
821+ * Set an instance of {@link ProxyServerSelector} used by an {@link AsyncHttpClient}
822+ *
823+ * @param proxyServerSelector instance of {@link ProxyServerSelector}
824+ * @return a {@link Builder}
825+ */
826+ public Builder setProxyServerSelector (ProxyServerSelector proxyServerSelector ) {
827+ this .proxyServerSelector = proxyServerSelector ;
828+ return this ;
829+ }
830+
819831 /**
820832 * Set an instance of {@link ProxyServer} used by an {@link AsyncHttpClient}
821833 *
822834 * @param proxyServer instance of {@link ProxyServer}
823835 * @return a {@link Builder}
824836 */
825837 public Builder setProxyServer (ProxyServer proxyServer ) {
826- this .proxyServer = proxyServer ;
838+ this .proxyServerSelector = ProxyUtils . createProxyServerSelector ( proxyServer ) ;
827839 return this ;
828840 }
829841
@@ -1024,12 +1036,27 @@ public Builder setRemoveQueryParamsOnRedirect(boolean removeQueryParamOnRedirect
10241036 return this ;
10251037 }
10261038
1039+ /**
1040+ * Sets whether AHC should use the default JDK ProxySelector to select a proxy server.
1041+ * <p/>
1042+ * If useProxySelector is set to <code>true</code> but {@link #setProxyServer(ProxyServer)}
1043+ * was used to explicitly set a proxy server, the latter is preferred.
1044+ * <p/>
1045+ * See http://docs.oracle.com/javase/7/docs/api/java/net/ProxySelector.html
1046+ */
1047+ public Builder setUseProxySelector (boolean useProxySelector ) {
1048+ this .useProxySelector = useProxySelector ;
1049+ return this ;
1050+ }
1051+
10271052 /**
10281053 * Sets whether AHC should use the default http.proxy* system properties
1029- * to obtain proxy information.
1054+ * to obtain proxy information. This differs from {@link #setUseProxySelector(boolean)}
1055+ * in that AsyncHttpClient will use its own logic to handle the system properties,
1056+ * potentially supporting other protocols that the the JDK ProxySelector doesn't.
10301057 * <p/>
1031- * If useProxyProperties is set to <code>true</code> but {@link #setProxyServer(ProxyServer)} was used
1032- * to explicitly set a proxy server , the latter is preferred.
1058+ * If useProxyProperties is set to <code>true</code> but {@link #setUseProxySelector(boolean)}
1059+ * was also set to true , the latter is preferred.
10331060 * <p/>
10341061 * See http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
10351062 */
@@ -1182,7 +1209,7 @@ public Builder(AsyncHttpClientConfig prototype) {
11821209 defaultMaxConnectionLifeTimeInMs = prototype .getMaxConnectionLifeTimeInMs ();
11831210 maxDefaultRedirects = prototype .getMaxRedirects ();
11841211 defaultMaxTotalConnections = prototype .getMaxTotalConnections ();
1185- proxyServer = prototype .getProxyServer ();
1212+ proxyServerSelector = prototype .getProxyServerSelector ();
11861213 realm = prototype .getRealm ();
11871214 defaultRequestTimeoutInMs = prototype .getRequestTimeoutInMs ();
11881215 sslContext = prototype .getSSLContext ();
@@ -1248,8 +1275,16 @@ public Thread newThread(Runnable r) {
12481275 throw new IllegalStateException ("ExecutorServices closed" );
12491276 }
12501277
1251- if (proxyServer == null && useProxyProperties ) {
1252- proxyServer = ProxyUtils .createProxy (System .getProperties ());
1278+ if (proxyServerSelector == null && useProxySelector ) {
1279+ proxyServerSelector = ProxyUtils .getJdkDefaultProxyServerSelector ();
1280+ }
1281+
1282+ if (proxyServerSelector == null && useProxyProperties ) {
1283+ proxyServerSelector = ProxyUtils .createProxyServerSelector (System .getProperties ());
1284+ }
1285+
1286+ if (proxyServerSelector == null ) {
1287+ proxyServerSelector = ProxyServerSelector .NO_PROXY_SELECTOR ;
12531288 }
12541289
12551290 return new AsyncHttpClientConfig (defaultMaxTotalConnections ,
@@ -1267,7 +1302,7 @@ public Thread newThread(Runnable r) {
12671302 allowPoolingConnection ,
12681303 reaper ,
12691304 applicationThreadPool ,
1270- proxyServer ,
1305+ proxyServerSelector ,
12711306 sslContext ,
12721307 sslEngineFactory ,
12731308 providerConfig ,
0 commit comments