18
18
import java .util .Objects ;
19
19
import software .amazon .awssdk .annotations .SdkPreviewApi ;
20
20
import software .amazon .awssdk .annotations .SdkPublicApi ;
21
+ import software .amazon .awssdk .utils .ProxySystemSetting ;
21
22
import software .amazon .awssdk .utils .builder .CopyableBuilder ;
22
23
import software .amazon .awssdk .utils .builder .ToCopyableBuilder ;
23
24
@@ -39,11 +40,13 @@ public final class ProxyConfiguration implements ToCopyableBuilder<ProxyConfigur
39
40
40
41
private final String username ;
41
42
private final String password ;
43
+ private final Boolean useSystemPropertyValues ;
42
44
43
45
private ProxyConfiguration (BuilderImpl builder ) {
46
+ this .useSystemPropertyValues = builder .useSystemPropertyValues ;
44
47
this .scheme = builder .scheme ;
45
- this .host = builder .host ;
46
- this .port = builder .port ;
48
+ this .host = resolveHost ( builder .host ) ;
49
+ this .port = resolvePort ( builder .port ) ;
47
50
this .username = builder .username ;
48
51
this .password = builder .password ;
49
52
}
@@ -56,31 +59,35 @@ public String scheme() {
56
59
}
57
60
58
61
/**
59
- * @return The proxy host.
62
+ * @return The proxy host from the configuration if set, or from the "http.proxyHost" system property if
63
+ * {@link Builder#useSystemPropertyValues(Boolean)} is set to true
60
64
*/
61
65
public String host () {
62
66
return host ;
63
67
}
64
68
65
69
/**
66
- * @return The proxy port.
70
+ * @return The proxy port from the configuration if set, or from the "http.proxyPort" system property if
71
+ * {@link Builder#useSystemPropertyValues(Boolean)} is set to true
67
72
*/
68
73
public int port () {
69
74
return port ;
70
75
}
71
76
72
77
/**
73
- * @return Basic authentication username
74
- */
78
+ * @return The proxy username from the configuration if set, or from the "http.proxyUser" system property if
79
+ * {@link Builder#useSystemPropertyValues(Boolean)} is set to true
80
+ * */
75
81
public String username () {
76
- return username ;
82
+ return resolveValue ( username , ProxySystemSetting . PROXY_USERNAME ) ;
77
83
}
78
84
79
85
/**
80
- * @return Basic authentication password
81
- */
86
+ * @return The proxy password from the configuration if set, or from the "http.proxyPassword" system property if
87
+ * {@link Builder#useSystemPropertyValues(Boolean)} is set to true
88
+ * */
82
89
public String password () {
83
- return password ;
90
+ return resolveValue ( password , ProxySystemSetting . PROXY_PASSWORD ) ;
84
91
}
85
92
86
93
@ Override
@@ -182,19 +189,49 @@ public interface Builder extends CopyableBuilder<Builder, ProxyConfiguration> {
182
189
* @return This object for method chaining.
183
190
*/
184
191
Builder password (String password );
192
+
193
+ /**
194
+ * The option whether to use system property values from {@link ProxySystemSetting} if any of the config options
195
+ * are missing. The value is set to "true" by default which means SDK will automatically use system property values if
196
+ * options are not provided during building the {@link ProxyConfiguration} object. To disable this behaviour, set this
197
+ * value to false.
198
+ *
199
+ * @param useSystemPropertyValues The option whether to use system property values
200
+ * @return This object for method chaining.
201
+ */
202
+ Builder useSystemPropertyValues (Boolean useSystemPropertyValues );
203
+ }
204
+
205
+ private String resolveHost (String host ) {
206
+ return resolveValue (host , ProxySystemSetting .PROXY_HOST );
207
+ }
208
+
209
+ private int resolvePort (int port ) {
210
+ return port == 0 && Boolean .TRUE .equals (useSystemPropertyValues ) ?
211
+ ProxySystemSetting .PROXY_PORT .getStringValue ().map (Integer ::parseInt ).orElse (0 ) : port ;
212
+ }
213
+
214
+ /**
215
+ * Uses the configuration options, system setting property and returns the final value of the given member.
216
+ */
217
+ private String resolveValue (String value , ProxySystemSetting systemSetting ) {
218
+ return value == null && Boolean .TRUE .equals (useSystemPropertyValues ) ?
219
+ systemSetting .getStringValue ().orElse (null ) : value ;
185
220
}
186
221
187
222
private static final class BuilderImpl implements Builder {
188
223
private String scheme ;
189
224
private String host ;
190
- private int port ;
225
+ private int port = 0 ;
191
226
private String username ;
192
227
private String password ;
228
+ private Boolean useSystemPropertyValues = Boolean .TRUE ;
193
229
194
230
private BuilderImpl () {
195
231
}
196
232
197
233
private BuilderImpl (ProxyConfiguration proxyConfiguration ) {
234
+ this .useSystemPropertyValues = proxyConfiguration .useSystemPropertyValues ;
198
235
this .scheme = proxyConfiguration .scheme ;
199
236
this .host = proxyConfiguration .host ;
200
237
this .port = proxyConfiguration .port ;
@@ -232,6 +269,16 @@ public Builder password(String password) {
232
269
return this ;
233
270
}
234
271
272
+ @ Override
273
+ public Builder useSystemPropertyValues (Boolean useSystemPropertyValues ) {
274
+ this .useSystemPropertyValues = useSystemPropertyValues ;
275
+ return this ;
276
+ }
277
+
278
+ public void setUseSystemPropertyValues (Boolean useSystemPropertyValues ) {
279
+ useSystemPropertyValues (useSystemPropertyValues );
280
+ }
281
+
235
282
@ Override
236
283
public ProxyConfiguration build () {
237
284
return new ProxyConfiguration (this );
0 commit comments