From 2ab751fe2c742e112e91247a94aa10ec8d3537ac Mon Sep 17 00:00:00 2001 From: R Searls Date: Tue, 13 Oct 2020 10:37:06 -0400 Subject: [PATCH] [RESTEASY-2708] added code for proxy server --- .../client/RestClientBuilderImpl.java | 121 +++++++++--------- testsuite/microprofile-tck/pom.xml | 3 - 2 files changed, 63 insertions(+), 61 deletions(-) diff --git a/resteasy-client-microprofile-base/src/main/java/org/jboss/resteasy/microprofile/client/RestClientBuilderImpl.java b/resteasy-client-microprofile-base/src/main/java/org/jboss/resteasy/microprofile/client/RestClientBuilderImpl.java index 5ee300b9bf3..334fba0ea11 100644 --- a/resteasy-client-microprofile-base/src/main/java/org/jboss/resteasy/microprofile/client/RestClientBuilderImpl.java +++ b/resteasy-client-microprofile-base/src/main/java/org/jboss/resteasy/microprofile/client/RestClientBuilderImpl.java @@ -24,9 +24,6 @@ import org.jboss.resteasy.spi.ResteasyProviderFactory; import org.jboss.resteasy.spi.ResteasyUriBuilder; - - - import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.CDI; import javax.net.ssl.HostnameVerifier; @@ -100,7 +97,8 @@ public RestClientBuilderImpl() { builderDelegate.providerFactory(localProviderFactory); } if (getBeanManager() != null) { - builderDelegate.getProviderFactory().setInjectorFactory(new CdiInjectorFactory(getBeanManager())); + builderDelegate.getProviderFactory() + .setInjectorFactory(new CdiInjectorFactory(getBeanManager())); } configurationWrapper = new ConfigurationWrapper(builderDelegate.getConfiguration()); @@ -132,8 +130,15 @@ public RestClientBuilder queryParamStyle(QueryParamStyle queryParamStyle) { } @Override - public RestClientBuilder proxyAddress(String var1, int var2){ - // TODO implement under a different jira and branch + public RestClientBuilder proxyAddress(String host, int port){ + if (host == null) { + throw new IllegalArgumentException("proxyHost must not be null"); + } + if (port <=0 || port > 65535) { + throw new IllegalArgumentException("Invalid port number"); + } + this.proxyHost = host; + this.proxyPort = port; return this; } @@ -231,64 +236,61 @@ public T build(Class aClass) throws IllegalStateException, RestClientDefi ClassLoader classLoader = aClass.getClassLoader(); - List noProxyHosts = Arrays.asList( - getSystemProperty("http.nonProxyHosts", "localhost|127.*|[::1]").split("\\|")); - String envProxyHost = getSystemProperty("http.proxyHost", null); + T actualClient; ResteasyClient client; ResteasyClientBuilder resteasyClientBuilder; - - boolean isUriMatched = false; - if (envProxyHost != null && !noProxyHosts.isEmpty()) { - for (String s : noProxyHosts) { - Pattern p = Pattern.compile(s); - Matcher m = p.matcher(baseURI.getHost()); - isUriMatched = m.matches(); - if (isUriMatched) { - break; + List noProxyHosts = Arrays.asList( + getSystemProperty("http.nonProxyHosts", "localhost|127.*|[::1]").split("\\|")); + if (this.proxyHost != null) { + resteasyClientBuilder = builderDelegate.defaultProxy(proxyHost, this.proxyPort); + } else { + String envProxyHost = getSystemProperty("http.proxyHost", null); + boolean isUriMatched = false; + if (envProxyHost != null && !noProxyHosts.isEmpty()) { + for (String s : noProxyHosts) { + Pattern p = Pattern.compile(s); + Matcher m = p.matcher(baseURI.getHost()); + isUriMatched = m.matches(); + if (isUriMatched) { + break; + } } } - } - if (envProxyHost != null && !isUriMatched) { - // Use proxy, if defined in the env variables - resteasyClientBuilder = builderDelegate.defaultProxy( - envProxyHost, - Integer.parseInt(getSystemProperty("http.proxyPort", "80"))); - } else { - // Search for proxy settings passed in the client builder, if passed and use them if found - String userProxyHost = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_HOST)) - .filter(String.class::isInstance) - .map(String.class::cast) - .orElse(null); + if (envProxyHost != null && !isUriMatched) { + // Use proxy, if defined in the env variables + resteasyClientBuilder = builderDelegate.defaultProxy(envProxyHost, + Integer.parseInt(getSystemProperty("http.proxyPort", "80"))); + } else { + // Search for proxy settings passed in the client builder, if passed and use them if found + String userProxyHost = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_HOST)) + .filter(String.class::isInstance).map(String.class::cast).orElse(null); - Integer userProxyPort = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_PORT)) - .filter(Integer.class::isInstance) - .map(Integer.class::cast) - .orElse(null); + Integer userProxyPort = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_PORT)) + .filter(Integer.class::isInstance).map(Integer.class::cast).orElse(null); - String userProxyScheme = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_SCHEME)) - .filter(String.class::isInstance) - .map(String.class::cast) - .orElse(null); + String userProxyScheme = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_SCHEME)) + .filter(String.class::isInstance).map(String.class::cast).orElse(null); - if (userProxyHost != null && userProxyPort != null) { - resteasyClientBuilder = builderDelegate.defaultProxy(userProxyHost, userProxyPort, userProxyScheme); - } else { - //ProxySelector if applicable - selectHttpProxy() - .ifPresent(proxyAddress -> builderDelegate.defaultProxy(proxyAddress.getHostString(), proxyAddress.getPort())); + if (userProxyHost != null && userProxyPort != null) { + resteasyClientBuilder = builderDelegate.defaultProxy(userProxyHost, userProxyPort, userProxyScheme); + } else { + // ProxySelector if applicable + selectHttpProxy().ifPresent( + proxyAddress -> builderDelegate.defaultProxy(proxyAddress.getHostString(), proxyAddress.getPort())); - resteasyClientBuilder = builderDelegate; + resteasyClientBuilder = builderDelegate; + } } } if (this.executorService != null) { - resteasyClientBuilder.executorService(this.executorService); + resteasyClientBuilder.executorService(this.executorService); } else { - resteasyClientBuilder.executorService(Executors.newCachedThreadPool(), true); + resteasyClientBuilder.executorService(Executors.newCachedThreadPool(), true); } resteasyClientBuilder.register(DEFAULT_MEDIA_TYPE_FILTER); resteasyClientBuilder.register(METHOD_INJECTION_FILTER); @@ -410,8 +412,8 @@ private void checkFollowRedirectProperty (Class aClass) { RegisterRestClient registerRestClient = (RegisterRestClient)aClass.getAnnotation(RegisterRestClient.class); if (registerRestClient !=null && - registerRestClient.configKey() != null && - !registerRestClient.configKey().isEmpty()) { + registerRestClient.configKey() != null && + !registerRestClient.configKey().isEmpty()) { //property using configKey prop = config.getOptionalValue( @@ -728,14 +730,14 @@ ResteasyClientBuilder getBuilderDelegate() { return builderDelegate; } private static BeanManager getBeanManager() { - try { - CDI current = CDI.current(); - return current != null ? current.getBeanManager() : null; - } catch (IllegalStateException e) { - LOGGER.warnf("CDI container is not available"); - return null; - } - } + try { + CDI current = CDI.current(); + return current != null ? current.getBeanManager() : null; + } catch (IllegalStateException e) { + LOGGER.warnf("CDI container is not available"); + return null; + } + } private String getSystemProperty(String key, String def) { if (System.getSecurityManager() == null) { return System.getProperty(key, def); @@ -759,6 +761,9 @@ private String getSystemProperty(String key, String def) { private Long readTimeout; private TimeUnit readTimeoutUnit; + private String proxyHost; + private Integer proxyPort = null; + private SSLContext sslContext; private KeyStore trustStore; private KeyStore keyStore; @@ -769,4 +774,4 @@ private String getSystemProperty(String key, String def) { private QueryParamStyle queryParamStyle = null; private Set localProviderInstances = new HashSet<>(); -} +} \ No newline at end of file diff --git a/testsuite/microprofile-tck/pom.xml b/testsuite/microprofile-tck/pom.xml index 886f71ec6a1..57943d54791 100644 --- a/testsuite/microprofile-tck/pom.xml +++ b/testsuite/microprofile-tck/pom.xml @@ -218,9 +218,6 @@ org.eclipse.microprofile.rest.client.tck.sse.ReactiveStreamsPublisherTckTest org.eclipse.microprofile.rest.client.tck.sse.BasicReactiveStreamsTest - org.eclipse.microprofile.rest.client.tck.cditests.CDIQueryParamStyleTest - org.eclipse.microprofile.rest.client.tck.cditests.CDIManagedProviderTest - org.eclipse.microprofile.rest.client.tck.ProxyServerTest