15
15
16
16
package software .amazon .awssdk .http .nio .netty ;
17
17
18
+ import java .util .Arrays ;
18
19
import java .util .Collections ;
19
20
import java .util .HashSet ;
20
21
import java .util .Set ;
22
+ import java .util .stream .Collectors ;
21
23
import software .amazon .awssdk .annotations .SdkPublicApi ;
24
+ import software .amazon .awssdk .utils .ProxySystemSetting ;
25
+ import software .amazon .awssdk .utils .StringUtils ;
22
26
import software .amazon .awssdk .utils .builder .CopyableBuilder ;
23
27
import software .amazon .awssdk .utils .builder .ToCopyableBuilder ;
24
28
30
34
*/
31
35
@ SdkPublicApi
32
36
public final class ProxyConfiguration implements ToCopyableBuilder <ProxyConfiguration .Builder , ProxyConfiguration > {
37
+ private final Boolean useSystemPropertyValues ;
33
38
private final String scheme ;
34
39
private final String host ;
35
40
private final int port ;
@@ -38,12 +43,13 @@ public final class ProxyConfiguration implements ToCopyableBuilder<ProxyConfigur
38
43
private final Set <String > nonProxyHosts ;
39
44
40
45
private ProxyConfiguration (BuilderImpl builder ) {
46
+ this .useSystemPropertyValues = builder .useSystemPropertyValues ;
41
47
this .scheme = builder .scheme ;
42
- this .host = builder .host ;
43
- this .port = builder .port ;
48
+ this .host = resolveHost ( builder .host ) ;
49
+ this .port = resolvePort ( builder .port ) ;
44
50
this .username = builder .username ;
45
51
this .password = builder .password ;
46
- this .nonProxyHosts = Collections . unmodifiableSet ( builder .nonProxyHosts ) ;
52
+ this .nonProxyHosts = builder .nonProxyHosts ;
47
53
}
48
54
49
55
public static Builder builder () {
@@ -58,38 +64,45 @@ public String scheme() {
58
64
}
59
65
60
66
/**
61
- * @return The proxy host.
67
+ * @return The proxy host from the configuration if set, or from the "http.proxyHost" system property if
68
+ * {@link Builder#useSystemPropertyValues(Boolean)} is set to true
62
69
*/
63
70
public String host () {
64
71
return host ;
65
72
}
66
73
67
74
/**
68
- * @return The proxy port.
75
+ * @return The proxy port from the configuration if set, or from the "http.proxyPort" system property if
76
+ * {@link Builder#useSystemPropertyValues(Boolean)} is set to true
69
77
*/
70
78
public int port () {
71
79
return port ;
72
80
}
73
81
74
82
/**
75
- * @return The proxy username.
76
- */
83
+ * @return The proxy username from the configuration if set, or from the "http.proxyUser" system property if
84
+ * {@link Builder#useSystemPropertyValues(Boolean)} is set to true
85
+ * */
77
86
public String username () {
78
- return username ;
87
+ return resolveValue ( username , ProxySystemSetting . PROXY_USERNAME ) ;
79
88
}
80
89
81
90
/**
82
- * @return The proxy password.
83
- */
91
+ * @return The proxy password from the configuration if set, or from the "http.proxyPassword" system property if
92
+ * {@link Builder#useSystemPropertyValues(Boolean)} is set to true
93
+ * */
84
94
public String password () {
85
- return password ;
95
+ return resolveValue ( password , ProxySystemSetting . PROXY_PASSWORD ) ;
86
96
}
87
97
88
98
/**
89
- * @return The set of hosts that should not be proxied.
99
+ * @return The set of hosts that should not be proxied. If the value is not set, the value present by "http.nonProxyHost
100
+ * system property os returned. If system property is also not set, an unmodifiable empty set is returned.
90
101
*/
91
102
public Set <String > nonProxyHosts () {
92
- return nonProxyHosts ;
103
+ Set <String > hosts = nonProxyHosts == null && useSystemPropertyValues ? parseNonProxyHostProperty ()
104
+ : nonProxyHosts ;
105
+ return Collections .unmodifiableSet (hosts != null ? hosts : Collections .emptySet ());
93
106
}
94
107
95
108
@ Override
@@ -199,6 +212,47 @@ public interface Builder extends CopyableBuilder<Builder, ProxyConfiguration> {
199
212
* @return This object for method chaining.
200
213
*/
201
214
Builder password (String password );
215
+
216
+ /**
217
+ * Set the option whether to use system property values from {@link ProxySystemSetting} if any of the config options
218
+ * are missing. The value is set to "true" by default which means SDK will automatically use system property values if
219
+ * options are not provided during building the {@link ProxyConfiguration} object. To disable this behaviour, set this
220
+ * value to false.
221
+ *
222
+ * @param useSystemPropertyValues The option whether to use system proerpty values
223
+ * @return This object for method chaining.
224
+ */
225
+ Builder useSystemPropertyValues (Boolean useSystemPropertyValues );
226
+
227
+ }
228
+
229
+ private String resolveHost (String host ) {
230
+ return resolveValue (host , ProxySystemSetting .PROXY_HOST );
231
+ }
232
+
233
+ private int resolvePort (int port ) {
234
+ return useSystemPropertyValues ? ProxySystemSetting .PROXY_PORT .getStringValue ().map (Integer ::parseInt ).orElse (0 )
235
+ : port ;
236
+ }
237
+
238
+ /**
239
+ * Uses the configuration options, system setting property and returns the final value of the given member.
240
+ */
241
+ private String resolveValue (String value , ProxySystemSetting systemSetting ) {
242
+ return value == null && useSystemPropertyValues ? systemSetting .getStringValue ().orElse (null )
243
+ : value ;
244
+ }
245
+
246
+ private Set <String > parseNonProxyHostProperty () {
247
+ String nonProxyHosts = ProxySystemSetting .NON_PROXY_HOSTS .getStringValue ().orElse (null );
248
+
249
+ if (!StringUtils .isEmpty (nonProxyHosts )) {
250
+ return Arrays .stream (nonProxyHosts .split ("\\ |" ))
251
+ .map (String ::toLowerCase )
252
+ .map (s -> s .replace ("*" , ".*?" ))
253
+ .collect (Collectors .toSet ());
254
+ }
255
+ return Collections .emptySet ();
202
256
}
203
257
204
258
private static final class BuilderImpl implements Builder {
@@ -208,11 +262,13 @@ private static final class BuilderImpl implements Builder {
208
262
private String username ;
209
263
private String password ;
210
264
private Set <String > nonProxyHosts = Collections .emptySet ();
265
+ private Boolean useSystemPropertyValues = Boolean .TRUE ;
211
266
212
267
private BuilderImpl () {
213
268
}
214
269
215
270
private BuilderImpl (ProxyConfiguration proxyConfiguration ) {
271
+ this .useSystemPropertyValues = proxyConfiguration .useSystemPropertyValues ;
216
272
this .scheme = proxyConfiguration .scheme ;
217
273
this .host = proxyConfiguration .host ;
218
274
this .port = proxyConfiguration .port ;
@@ -262,6 +318,16 @@ public Builder password(String password) {
262
318
return this ;
263
319
}
264
320
321
+ @ Override
322
+ public Builder useSystemPropertyValues (Boolean useSystemPropertyValues ) {
323
+ this .useSystemPropertyValues = useSystemPropertyValues ;
324
+ return this ;
325
+ }
326
+
327
+ public void setUseSystemPropertyValues (Boolean useSystemPropertyValues ) {
328
+ useSystemPropertyValues (useSystemPropertyValues );
329
+ }
330
+
265
331
@ Override
266
332
public ProxyConfiguration build () {
267
333
return new ProxyConfiguration (this );
0 commit comments