|
36 | 36 | import java.lang.reflect.Method;
|
37 | 37 | import java.lang.reflect.Parameter;
|
38 | 38 | import java.lang.reflect.Proxy;
|
| 39 | +import java.net.InetSocketAddress; |
| 40 | +import java.net.ProxySelector; |
39 | 41 | import java.net.URI;
|
40 | 42 | import java.net.URISyntaxException;
|
41 | 43 | import java.net.URL;
|
| 44 | +import java.security.AccessController; |
42 | 45 | import java.security.KeyStore;
|
| 46 | +import java.security.PrivilegedAction; |
43 | 47 | import java.util.ArrayList;
|
44 | 48 | import java.util.Arrays;
|
45 | 49 | import java.util.HashMap;
|
|
51 | 55 | import java.util.concurrent.ExecutorService;
|
52 | 56 | import java.util.concurrent.Executors;
|
53 | 57 | import java.util.concurrent.TimeUnit;
|
| 58 | +import java.util.regex.Matcher; |
| 59 | +import java.util.regex.Pattern; |
| 60 | + |
| 61 | +import static org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder.*; |
54 | 62 |
|
55 | 63 | class RestClientBuilderImpl implements RestClientBuilder {
|
56 | 64 |
|
@@ -173,21 +181,59 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
|
173 | 181 | ClassLoader classLoader = aClass.getClassLoader();
|
174 | 182 |
|
175 | 183 | List<String> noProxyHosts = Arrays.asList(
|
176 |
| - ConfigProvider.getConfig().getOptionalValue("http.nonProxyHosts", String.class).orElse("localhost|127.*|[::1]").split("|")); |
177 |
| - String proxyHost = ConfigProvider.getConfig().getOptionalValue("http.proxyHost", String.class).orElse(null); |
| 184 | + getSystemProperty("http.nonProxyHosts", "localhost|127.*|[::1]").split("\\|")); |
| 185 | + String envProxyHost = getSystemProperty("http.proxyHost", null); |
178 | 186 |
|
179 | 187 | T actualClient;
|
180 | 188 | ResteasyClient client;
|
181 | 189 |
|
182 | 190 | ResteasyClientBuilder resteasyClientBuilder;
|
183 |
| - if (proxyHost != null && !noProxyHosts.contains(baseURI.getHost())) { |
184 |
| - // Use proxy, if defined |
| 191 | + |
| 192 | + boolean isUriMatched = false; |
| 193 | + if (envProxyHost != null && !noProxyHosts.isEmpty()) { |
| 194 | + for (String s : noProxyHosts) { |
| 195 | + Pattern p = Pattern.compile(s); |
| 196 | + Matcher m = p.matcher(baseURI.getHost()); |
| 197 | + isUriMatched = m.matches(); |
| 198 | + if (isUriMatched) { |
| 199 | + break; |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + if (envProxyHost != null && !isUriMatched) { |
| 205 | + // Use proxy, if defined in the env variables |
185 | 206 | resteasyClientBuilder = builderDelegate.defaultProxy(
|
186 |
| - proxyHost, |
187 |
| - ConfigProvider.getConfig().getOptionalValue("http.proxyPort", int.class).orElse(80)); |
| 207 | + envProxyHost, |
| 208 | + Integer.parseInt(getSystemProperty("http.proxyPort", "80"))); |
188 | 209 | } else {
|
189 |
| - resteasyClientBuilder = builderDelegate; |
| 210 | + // Search for proxy settings passed in the client builder, if passed and use them if found |
| 211 | + String userProxyHost = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_HOST)) |
| 212 | + .filter(String.class::isInstance) |
| 213 | + .map(String.class::cast) |
| 214 | + .orElse(null); |
| 215 | + |
| 216 | + Integer userProxyPort = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_PORT)) |
| 217 | + .filter(Integer.class::isInstance) |
| 218 | + .map(Integer.class::cast) |
| 219 | + .orElse(null); |
| 220 | + |
| 221 | + String userProxyScheme = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_SCHEME)) |
| 222 | + .filter(String.class::isInstance) |
| 223 | + .map(String.class::cast) |
| 224 | + .orElse(null); |
| 225 | + |
| 226 | + if (userProxyHost != null && userProxyPort != null) { |
| 227 | + resteasyClientBuilder = builderDelegate.defaultProxy(userProxyHost, userProxyPort, userProxyScheme); |
| 228 | + } else { |
| 229 | + //ProxySelector if applicable |
| 230 | + selectHttpProxy() |
| 231 | + .ifPresent(proxyAddress -> builderDelegate.defaultProxy(proxyAddress.getHostString(), proxyAddress.getPort())); |
| 232 | + |
| 233 | + resteasyClientBuilder = builderDelegate; |
| 234 | + } |
190 | 235 | }
|
| 236 | + |
191 | 237 | // this is rest easy default
|
192 | 238 | ExecutorService executorService = this.executorService != null ? this.executorService : Executors.newFixedThreadPool(10);
|
193 | 239 |
|
@@ -230,6 +276,14 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
|
230 | 276 | return proxy;
|
231 | 277 | }
|
232 | 278 |
|
| 279 | + private Optional<InetSocketAddress> selectHttpProxy() { |
| 280 | + return ProxySelector.getDefault().select(baseURI).stream() |
| 281 | + .filter(proxy -> proxy.type() == java.net.Proxy.Type.HTTP) |
| 282 | + .map(java.net.Proxy::address) |
| 283 | + .map(InetSocketAddress.class::cast) |
| 284 | + .findFirst(); |
| 285 | + } |
| 286 | + |
233 | 287 | private boolean isMapperDisabled() {
|
234 | 288 | boolean disabled = false;
|
235 | 289 | Optional<Boolean> defaultMapperProp = config == null ? Optional.empty() : config.getOptionalValue(DEFAULT_MAPPER_PROP, Boolean.class);
|
@@ -532,6 +586,13 @@ ResteasyClientBuilder getBuilderDelegate() {
|
532 | 586 | return builderDelegate;
|
533 | 587 | }
|
534 | 588 |
|
| 589 | + private String getSystemProperty(String key, String def) { |
| 590 | + if (System.getSecurityManager() == null) { |
| 591 | + return System.getProperty(key, def); |
| 592 | + } |
| 593 | + return AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty(key, def)); |
| 594 | + } |
| 595 | + |
535 | 596 | private final ResteasyClientBuilder builderDelegate;
|
536 | 597 |
|
537 | 598 | private final ConfigurationWrapper configurationWrapper;
|
|
0 commit comments