8000 [RESTEASY-2641] ported nonProxyHost code from master (#2624) · resteasy/resteasy@2e6bcbe · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e6bcbe

Browse files
authored
[RESTEASY-2641] ported nonProxyHost code from master (#2624)
1 parent 685132a commit 2e6bcbe

File tree

1 file changed

+68
-7
lines changed

1 file changed

+68
-7
lines changed

resteasy-client-microprofile/src/main/java/org/jboss/resteasy/microprofile/client/RestClientBuilderImpl.java

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@
3636
import java.lang.reflect.Method;
3737
import java.lang.reflect.Parameter;
3838
import java.lang.reflect.Proxy;
39+
import java.net.InetSocketAddress;
40+
import java.net.ProxySelector;
3941
import java.net.URI;
4042
import java.net.URISyntaxException;
4143
import java.net.URL;
44+
import java.security.AccessController;
4245
import java.security.KeyStore;
46+
import java.security.PrivilegedAction;
4347
import java.util.ArrayList;
4448
import java.util.Arrays;
4549
import java.util.HashMap;
@@ -51,6 +55,10 @@
5155
import java.util.concurrent.ExecutorService;
5256
import java.util.concurrent.Executors;
5357
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.*;
5462

5563
class RestClientBuilderImpl implements RestClientBuilder {
5664

@@ -173,21 +181,59 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
173181
ClassLoader classLoader = aClass.getClassLoader();
174182

175183
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);
178186

179187
T actualClient;
180188
ResteasyClient client;
181189

182190
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
185206
resteasyClientBuilder = builderDelegate.defaultProxy(
186-
proxyHost,
187-
ConfigProvider.getConfig().getOptionalValue("http.proxyPort", int.class).orElse(80));
207+
envProxyHost,
208+
Integer.parseInt(getSystemProperty("http.proxyPort", "80")));
188209
} 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+
}
190235
}
236+
191237
// this is rest easy default
192238
ExecutorService executorService = this.executorService != null ? this.executorService : Executors.newFixedThreadPool(10);
193239

@@ -230,6 +276,14 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
230276
return proxy;
231277
}
232278

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+
233287
private boolean isMapperDisabled() {
234288
boolean disabled = false;
235289
Optional<Boolean> defaultMapperProp = config == null ? Optional.empty() : config.getOptionalValue(DEFAULT_MAPPER_PROP, Boolean.class);
@@ -532,6 +586,13 @@ ResteasyClientBuilder getBuilderDelegate() {
532586
return builderDelegate;
533587
}
534588

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+
535596
private final ResteasyClientBuilder builderDelegate;
536597

537598
private final ConfigurationWrapper configurationWrapper;

0 commit comments

Comments
 (0)
0