@@ -92,7 +92,7 @@ public class AsyncHttpClientConfig {
92
92
protected boolean allowPoolingConnection ;
93
93
protected ScheduledExecutorService reaper ;
94
94
protected ExecutorService applicationThreadPool ;
95
- protected ProxyServer proxyServer ;
95
+ protected ProxyServerSelector proxyServerSelector ;
96
96
protected SSLContext sslContext ;
97
97
protected SSLEngineFactory sslEngineFactory ;
98
98
protected AsyncHttpProviderConfig <?, ?> providerConfig ;
@@ -136,7 +136,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
136
136
boolean keepAlive ,
137
137
ScheduledExecutorService reaper ,
138
138
ExecutorService applicationThreadPool ,
139
- ProxyServer proxyServer ,
139
+ ProxyServerSelector proxyServerSelector ,
140
140
SSLContext sslContext ,
141
141
SSLEngineFactory sslEngineFactory ,
142
142
AsyncHttpProviderConfig <?, ?> providerConfig ,
@@ -196,7 +196,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
196
196
} else {
197
197
this .applicationThreadPool = applicationThreadPool ;
198
198
}
199
- this .proxyServer = proxyServer ;
199
+ this .proxyServerSelector = proxyServerSelector ;
200
200
this .useRawUrl = useRawUrl ;
201
201
this .spdyEnabled = spdyEnabled ;
202
202
this .spdyInitialWindowSize = spdyInitialWindowSize ;
@@ -362,8 +362,8 @@ public boolean isManagedExecutorService() {
362
362
*
363
363
* @return instance of {@link ProxyServer}
364
364
*/
365
- public ProxyServer getProxyServer () {
366
- return proxyServer ;
365
+ public ProxyServerSelector getProxyServerSelector () {
366
+ return proxyServerSelector ;
367
367
}
368
368
369
369
/**
@@ -613,11 +613,12 @@ public static class Builder {
613
613
private boolean compressionEnabled = Boolean .getBoolean (ASYNC_CLIENT + "compressionEnabled" );
614
614
private String userAgent = System .getProperty (ASYNC_CLIENT + "userAgent" , "AsyncHttpClient/" + AHC_VERSION );
615
615
private boolean useProxyProperties = Boolean .getBoolean (ASYNC_CLIENT + "useProxyProperties" );
616
+ private boolean useProxySelector = Boolean .getBoolean (ASYNC_CLIENT + "useProxySelector" );
616
617
private boolean allowPoolingConnection = true ;
617
618
private boolean useRelativeURIsWithSSLProxies = Boolean .getBoolean (ASYNC_CLIENT + "useRelativeURIsWithSSLProxies" );
618
619
private ScheduledExecutorService reaper ;
619
620
private ExecutorService applicationThreadPool ;
620
- private ProxyServer proxyServer = null ;
621
+ private ProxyServerSelector proxyServerSelector = null ;
621
622
private SSLContext sslContext ;
622
623
private SSLEngineFactory sslEngineFactory ;
623
624
private AsyncHttpProviderConfig <?, ?> providerConfig ;
@@ -816,14 +817,25 @@ public Builder setExecutorService(ExecutorService applicationThreadPool) {
816
817
return this ;
817
818
}
818
819
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
+
819
831
/**
820
832
* Set an instance of {@link ProxyServer} used by an {@link AsyncHttpClient}
821
833
*
822
834
* @param proxyServer instance of {@link ProxyServer}
823
835
* @return a {@link Builder}
824
836
*/
825
837
public Builder setProxyServer (ProxyServer proxyServer ) {
826
- this .proxyServer = proxyServer ;
838
+ this .proxyServerSelector = ProxyUtils . createProxyServerSelector ( proxyServer ) ;
827
839
return this ;
828
840
}
829
841
@@ -1024,12 +1036,27 @@ public Builder setRemoveQueryParamsOnRedirect(boolean removeQueryParamOnRedirect
1024
1036
return this ;
1025
1037
}
1026
1038
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
+
1027
1052
/**
1028
1053
* 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.
1030
1057
* <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.
1033
1060
* <p/>
1034
1061
* See http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
1035
1062
*/
@@ -1182,7 +1209,7 @@ public Builder(AsyncHttpClientConfig prototype) {
1182
1209
defaultMaxConnectionLifeTimeInMs = prototype .getMaxConnectionLifeTimeInMs ();
1183
1210
maxDefaultRedirects = prototype .getMaxRedirects ();
1184
1211
defaultMaxTotalConnections = prototype .getMaxTotalConnections ();
1185
- proxyServer = prototype .getProxyServer ();
1212
+ proxyServerSelector = prototype .getProxyServerSelector ();
1186
1213
realm = prototype .getRealm ();
1187
1214
defaultRequestTimeoutInMs = prototype .getRequestTimeoutInMs ();
1188
1215
sslContext = prototype .getSSLContext ();
@@ -1248,8 +1275,16 @@ public Thread newThread(Runnable r) {
1248
1275
throw new IllegalStateException ("ExecutorServices closed" );
1249
1276
}
1250
1277
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 ;
1253
1288
}
1254
1289
1255
1290
return new AsyncHttpClientConfig (defaultMaxTotalConnections ,
@@ -1267,7 +1302,7 @@ public Thread newThread(Runnable r) {
1267
1302
allowPoolingConnection ,
1268
1303
reaper ,
1269
1304
applicationThreadPool ,
1270
- proxyServer ,
1305
+ proxyServerSelector ,
1271
1306
sslContext ,
1272
1307
sslEngineFactory ,
1273
1308
providerConfig ,
0 commit comments