@@ -67,7 +67,7 @@ public class AsyncHttpClientConfig {
67
67
protected boolean allowPoolingConnection ;
68
68
protected ScheduledExecutorService reaper ;
69
69
protected ExecutorService applicationThreadPool ;
70
- protected ProxyServer proxyServer ;
70
+ protected ProxyServerSelector proxyServerSelector ;
71
71
protected SSLContext sslContext ;
72
72
protected SSLEngineFactory sslEngineFactory ;
73
73
protected AsyncHttpProviderConfig <?, ?> providerConfig ;
@@ -106,7 +106,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
106
106
boolean keepAlive ,
107
107
ScheduledExecutorService reaper ,
108
108
ExecutorService applicationThreadPool ,
109
- ProxyServer proxyServer ,
109
+ ProxyServerSelector proxyServerSelector ,
110
110
SSLContext sslContext ,
111
111
SSLEngineFactory sslEngineFactory ,
112
112
AsyncHttpProviderConfig <?, ?> providerConfig ,
@@ -162,7 +162,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
162
162
} else {
163
163
this .applicationThreadPool = applicationThreadPool ;
164
164
}
165
- this .proxyServer = proxyServer ;
165
+ this .proxyServerSelector = proxyServerSelector ;
166
166
this .useRawUrl = useRawUrl ;
167
167
}
168
168
@@ -310,8 +310,8 @@ public ExecutorService executorService() {
310
310
*
311
311
* @return instance of {@link com.ning.http.client.ProxyServer}
312
312
*/
313
- public ProxyServer getProxyServer () {
314
- return proxyServer ;
313
+ public ProxyServerSelector getProxyServerSelector () {
314
+ return proxyServerSelector ;
315
315
}
316
316
317
317
/**
@@ -531,11 +531,12 @@ public static class Builder {
531
531
private boolean compressionEnabled = Boolean .getBoolean (ASYNC_CLIENT + "compressionEnabled" );
532
532
private String userAgent = System .getProperty (ASYNC_CLIENT + "userAgent" , "NING/1.0" );
533
533
private boolean useProxyProperties = Boolean .getBoolean (ASYNC_CLIENT + "useProxyProperties" );
534
+ private boolean useProxySelector = Boolean .getBoolean (ASYNC_CLIENT + "useProxySelector" );
534
535
private boolean allowPoolingConnection = true ;
535
536
private boolean useRelativeURIsWithSSLProxies = Boolean .getBoolean (ASYNC_CLIENT + "useRelativeURIsWithSSLProxies" );
536
537
private ScheduledExecutorService reaper ;
537
538
private ExecutorService applicationThreadPool ;
538
- private ProxyServer proxyServer = null ;
539
+ private ProxyServerSelector proxyServerSelector = null ;
539
540
private SSLContext sslContext ;
540
541
private SSLEngineFactory sslEngineFactory ;
541
542
private AsyncHttpProviderConfig <?, ?> providerConfig ;
@@ -731,13 +732,24 @@ public Builder setExecutorService(ExecutorService applicationThreadPool) {
731
732
}
732
733
733
734
/**
734
- * Set an instance of {@link com.ning.http.client.ProxyServer} used by an {@link AsyncHttpClient}
735
+ * Set an instance of {@link ProxyServerSelector} used by an {@link AsyncHttpClient}
736
+ *
737
+ * @param proxyServerSelector instance of {@link ProxyServerSelector}
738
+ * @return a {@link Builder}
739
+ */
740
+ public Builder setProxyServerSelector (ProxyServerSelector proxyServerSelector ) {
741
+ this .proxyServerSelector = proxyServerSelector ;
742
+ return this ;
743
+ }
744
+
745
+ /**
746
+ * Set an instance of {@link ProxyServer} used by an {@link AsyncHttpClient}
735
747
*
736
748
* @param proxyServer instance of {@link com.ning.http.client.ProxyServer}
737
749
* @return a {@link Builder}
738
750
*/
739
751
public Builder setProxyServer (ProxyServer proxyServer ) {
740
- this .proxyServer = proxyServer ;
752
+ this .proxyServerSelector = ProxyUtils . createProxyServerSelector ( proxyServer ) ;
741
753
return this ;
742
754
}
743
755
@@ -938,12 +950,27 @@ public Builder setRemoveQueryParamsOnRedirect(boolean removeQueryParamOnRedirect
938
950
return this ;
939
951
}
940
952
953
+ /**
954
+ * Sets whether AHC should use the default JDK ProxySelector to select a proxy server.
955
+ * <p/>
956
+ * If useProxySelector is set to <code>true</code> but {@link #setProxyServer(ProxyServer)}
957
+ * was used to explicitly set a proxy server, the latter is preferred.
958
+ * <p/>
959
+ * See http://docs.oracle.com/javase/7/docs/api/java/net/ProxySelector.html
960
+ */
961
+ public Builder setUseProxySelector (boolean useProxySelector ) {
962
+ this .useProxySelector = useProxySelector ;
963
+ return this ;
964
+ }
965
+
941
966
/**
942
967
* Sets whether AHC should use the default http.proxy* system properties
943
- * to obtain proxy information.
968
+ * to obtain proxy information. This differs from {@link #setUseProxySelector(boolean)}
969
+ * in that AsyncHttpClient will use its own logic to handle the system properties,
970
+ * potentially supporting other protocols that the the JDK ProxySelector doesn't.
944
971
* <p/>
945
- * If useProxyProperties is set to <code>true</code> but {@link #setProxyServer(ProxyServer)} was used
946
- * to explicitly set a proxy server , the latter is preferred.
972
+ * If useProxyProperties is set to <code>true</code> but {@link #setUseProxySelector(boolean)}
973
+ * was also set to true , the latter is preferred.
947
974
* <p/>
948
975
* See http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
949
976
*/
@@ -1036,7 +1063,7 @@ public Builder(AsyncHttpClientConfig prototype) {
1036
1063
defaultMaxConnectionLifeTimeInMs = prototype .getMaxConnectionLifeTimeInMs ();
1037
1064
maxDefaultRedirects = prototype .getMaxRedirects ();
1038
1065
defaultMaxTotalConnections = prototype .getMaxTotalConnections ();
1039
- proxyServer = prototype .getProxyServer ();
1066
+ proxyServerSelector = prototype .getProxyServerSelector ();
1040
1067
realm = prototype .getRealm ();
1041
1068
defaultRequestTimeoutInMs = prototype .getRequestTimeoutInMs ();
1042
1069
sslContext = prototype .getSSLContext ();
@@ -1100,8 +1127,16 @@ public Thread newThread(Runnable r) {
1100
1127
throw new IllegalStateException ("ExecutorServices closed" );
1101
1128
}
1102
1129
1103
- if (proxyServer == null && useProxyProperties ) {
1104
- proxyServer = ProxyUtils .createProxy (System .getProperties ());
1130
+ if (proxyServerSelector == null && useProxySelector ) {
1131
+ proxyServerSelector = ProxyUtils .getJdkDefaultProxyServerSelector ();
1132
+ }
1133
+
1134
+ if (proxyServerSelector == null && useProxyProperties ) {
1135
+ proxyServerSelector = ProxyUtils .createProxyServerSelector (System .getProperties ());
1136
+ }
1137
+
1138
+ if (proxyServerSelector == null ) {
1139
+ proxyServerSelector = ProxyServerSelector .NO_PROXY_SELECTOR ;
1105
1140
}
1106
1141
1107
1142
return new AsyncHttpClientConfig (defaultMaxTotalConnections ,
@@ -1119,7 +1154,7 @@ public Thread newThread(Runnable r) {
1119
1154
allowPoolingConnection ,
1120
1155
reaper ,
1121
1156
applicationThreadPool ,
1122
- proxyServer ,
1157
+ proxyServerSelector ,
1123
1158
sslContext ,
1124
1159
sslEngineFactory ,
1125
1160
providerConfig ,
0 commit comments