From e573c4e6ae99f42978eb8db7fc19bfe1ed1273a7 Mon Sep 17 00:00:00 2001 From: Andrea Patricelli Date: Tue, 22 Oct 2024 14:56:41 +0200 Subject: [PATCH 1/4] [GOOGLEAPPS-22] added support to proxy --- .gitignore | 1 + .../googleapps/GoogleAppsConfiguration.java | 129 ++++++++++++++++-- .../bundles/googleapps/Messages.properties | 11 ++ .../GoogleAppsConnectorUnitTests.java | 3 + 4 files changed, 134 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 612c5bc..31f94c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target .idea *.iml +.java-version diff --git a/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java b/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java index b3402f9..cee0dee 100644 --- a/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java +++ b/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java @@ -26,6 +26,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.apache.v2.ApacheHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.gson.GsonFactory; @@ -33,11 +34,21 @@ import com.google.api.services.directory.DirectoryScopes; import com.google.api.services.licensing.Licensing; import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.http.HttpTransportFactory; import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.UserCredentials; import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.util.Arrays; import java.util.List; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.ProxyAuthenticationStrategy; import org.identityconnectors.common.StringUtil; import org.identityconnectors.common.logging.Log; import org.identityconnectors.common.security.GuardedString; @@ -55,11 +66,6 @@ public class GoogleAppsConfiguration extends AbstractConfiguration implements St private static final Log LOG = Log.getLog(GoogleAppsConfiguration.class); - /** - * Global instance of the HTTP transport. - */ - private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); - /** * Global instance of the JSON factory. */ @@ -97,6 +103,17 @@ public class GoogleAppsConfiguration extends AbstractConfiguration implements St private boolean removeLicenseOnDisable = false; + // proxy configuration section + private String proxyServerType = Proxy.Type.HTTP.name(); + + private String proxyServerHost; + + private Integer proxyServerPort; + + private String proxyServerUser; + + private String proxyServerPassword; + @ConfigurationProperty(order = 1, displayMessageKey = "domain.display", groupMessageKey = "basic.group", helpMessageKey = "domain.help", required = true, confidential = false) @@ -192,6 +209,61 @@ public void setRemoveLicenseOnDisable(final boolean removeLicenseOnDisable) { this.removeLicenseOnDisable = removeLicenseOnDisable; } + @ConfigurationProperty(displayMessageKey = "proxyServerType.display", + helpMessageKey = "proxyServerType.help", + order = 10) + public String getProxyServerType() { + return proxyServerType; + } + + public void setProxyServerType(final String proxyServerType) { + this.proxyServerType = proxyServerType; + } + + @ConfigurationProperty(displayMessageKey = "proxyServerHost.display", + helpMessageKey = "proxyServerHost.help", + order = 11) + public String getProxyServerHost() { + return proxyServerHost; + } + + public void setProxyServerHost(final String proxyServerHost) { + this.proxyServerHost = proxyServerHost; + } + + @ConfigurationProperty(displayMessageKey = "proxyServerPort.display", + helpMessageKey = "proxyServerPort.help", + order = 12) + public Integer getProxyServerPort() { + return proxyServerPort; + } + + public void setProxyServerPort(final Integer proxyServerPort) { + this.proxyServerPort = proxyServerPort; + } + + @ConfigurationProperty(displayMessageKey = "proxyServerUser.display", + helpMessageKey = "proxyServerUser.help", + order = 13) + public String getProxyServerUser() { + return proxyServerUser; + } + + public void setProxyServerUser(final String proxyServerUser) { + this.proxyServerUser = proxyServerUser; + } + + @ConfigurationProperty(displayMessageKey = "proxyServerPassword.display", + helpMessageKey = "proxyServerPassword.help", + order = 14, confidential = true) + public String getProxyServerPassword() { + return proxyServerPassword; + } + + public void setProxyServerPassword(final String proxyServerPassword) { + this.proxyServerPassword = proxyServerPassword; + } + @Override public void validate() { if (StringUtil.isBlank(domain)) { @@ -221,14 +293,45 @@ public void validate() { throw new ConfigurationException("'customSchemaJSON' parameter must be a valid JSON."); } } + if (StringUtil.isNotBlank(proxyServerHost) && proxyServerPort == null + || StringUtil.isBlank(proxyServerHost) && proxyServerPort != null) { + throw new ConfigurationException("Proxy host and port cannot be empty."); + } + + if (StringUtil.isNotBlank(proxyServerUser) && StringUtil.isBlank(proxyServerPassword)) { + throw new ConfigurationException("Proxy server password cannot be null or empty if user is specified."); + } + if (StringUtil.isNotBlank(proxyServerPassword) && StringUtil.isBlank(proxyServerUser)) { + throw new ConfigurationException("Proxy server user cannot be null or empty if password is specified."); + } } private void initGoogleCredentials() { synchronized (this) { if (null == googleCredentials) { - UserCredentials.Builder credentialsBuilder = UserCredentials.newBuilder() - .setClientId(getClientId()) - .setClientSecret(SecurityUtil.decrypt(getClientSecret())); + UserCredentials.Builder credentialsBuilder = UserCredentials.newBuilder(); + + boolean proxyEnabled = StringUtil.isNotBlank(proxyServerHost) && proxyServerPort != null; + if (proxyEnabled) { + HttpClientBuilder clientBuilder = HttpClientBuilder.create(); + clientBuilder.useSystemProperties(); + clientBuilder.setProxy(new HttpHost(proxyServerHost, proxyServerPort)); + if (StringUtil.isNotBlank(proxyServerUser) && StringUtil.isNotBlank(proxyServerPassword)) { + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), + new UsernamePasswordCredentials(proxyServerUser, proxyServerPassword)); + clientBuilder.setDefaultCredentialsProvider(credsProvider); + clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); + } + + credentialsBuilder.setHttpTransportFactory(new HttpTransportFactory() { + @Override + public HttpTransport create() { + return new ApacheHttpTransport(clientBuilder.build()); + } + }); + } + credentialsBuilder.setClientId(getClientId()).setClientSecret(SecurityUtil.decrypt(getClientSecret())); getRefreshToken().access(chars -> credentialsBuilder.setRefreshToken(new String(chars))); @@ -243,11 +346,17 @@ private void initGoogleCredentials() { DirectoryScopes.ADMIN_DIRECTORY_GROUP, DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER)); + HttpTransport httpTransport = proxyEnabled + ? new NetHttpTransport.Builder().setProxy( + new Proxy(Proxy.Type.valueOf(proxyServerType.toUpperCase()), + new InetSocketAddress(proxyServerHost, proxyServerPort))).build() + : new NetHttpTransport(); + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials); - directory = new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer). + directory = new Directory.Builder(httpTransport, JSON_FACTORY, requestInitializer). setApplicationName(APPLICATION_NAME). build(); - licensing = new Licensing.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer). + licensing = new Licensing.Builder(httpTransport, JSON_FACTORY, requestInitializer). setApplicationName(APPLICATION_NAME). build(); } diff --git a/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties b/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties index 389e444..85603d3 100644 --- a/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties +++ b/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties @@ -42,6 +42,17 @@ productId.help=Google Product ID removeLicenseOnDisable.display=Remove license while disabling an user removeLicenseOnDisable.help=Also performs license removal when an user is disabled (suspend = true); Needs skuIds and productId to be defined. basic.group=Basic Configuration Properties +proxyServerType.display=Proxy Server Type +proxyServerType.help=Specifies the type of the proxy server to use (if any) to access to the SCIM server, allowed values are HTTP and SOCKS +proxyServerHost.display=Proxy Server Host +proxyServerHost.help=Specifies proxy host to connect to reach SCIM server +proxyServerPort.display=Proxy Server Port +proxyServerPort.help=Specifies proxy port to connect to reach SCIM server +proxyServerUser.display=Proxy Server Username +proxyServerUser.help=Specifies username to authenticate on proxy (optional, only Basic auth is supported) +proxyServerPassword.display=Proxy Server Password +proxyServerPassword.help=Specifies password to authenticate on proxy + MESSAGE_OBJECT_CLASS___ACCOUNT__=Account MESSAGE_OBJECT_CLASS___GROUP__=Group diff --git a/src/test/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConnectorUnitTests.java b/src/test/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConnectorUnitTests.java index bb3d4d2..5f0ea27 100644 --- a/src/test/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConnectorUnitTests.java +++ b/src/test/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConnectorUnitTests.java @@ -96,6 +96,9 @@ public static void setUp() throws IOException { + "}]" + "}]"); + CONN_CONF.setProxyServerHost("localhost"); + CONN_CONF.setProxyServerPort(3128); + CONNECTOR = newFacade(); } From 406ef764e19fc65b1cac1d8cb8b4a5119a42930f Mon Sep 17 00:00:00 2001 From: Andrea Patricelli Date: Wed, 23 Oct 2024 10:47:39 +0200 Subject: [PATCH 2/4] removed properties, use system variables --- .../googleapps/GoogleAppsConfiguration.java | 127 ++++-------------- .../bundles/googleapps/Messages.properties | 10 -- .../GoogleAppsConnectorUnitTests.java | 7 +- 3 files changed, 33 insertions(+), 111 deletions(-) diff --git a/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java b/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java index cee0dee..43549a5 100644 --- a/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java +++ b/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java @@ -43,12 +43,7 @@ import java.util.Arrays; import java.util.List; import org.apache.http.HttpHost; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.impl.client.ProxyAuthenticationStrategy; import org.identityconnectors.common.StringUtil; import org.identityconnectors.common.logging.Log; import org.identityconnectors.common.security.GuardedString; @@ -103,17 +98,6 @@ public class GoogleAppsConfiguration extends AbstractConfiguration implements St private boolean removeLicenseOnDisable = false; - // proxy configuration section - private String proxyServerType = Proxy.Type.HTTP.name(); - - private String proxyServerHost; - - private Integer proxyServerPort; - - private String proxyServerUser; - - private String proxyServerPassword; - @ConfigurationProperty(order = 1, displayMessageKey = "domain.display", groupMessageKey = "basic.group", helpMessageKey = "domain.help", required = true, confidential = false) @@ -209,61 +193,6 @@ public void setRemoveLicenseOnDisable(final boolean removeLicenseOnDisable) { this.removeLicenseOnDisable = removeLicenseOnDisable; } - @ConfigurationProperty(displayMessageKey = "proxyServerType.display", - helpMessageKey = "proxyServerType.help", - order = 10) - public String getProxyServerType() { - return proxyServerType; - } - - public void setProxyServerType(final String proxyServerType) { - this.proxyServerType = proxyServerType; - } - - @ConfigurationProperty(displayMessageKey = "proxyServerHost.display", - helpMessageKey = "proxyServerHost.help", - order = 11) - public String getProxyServerHost() { - return proxyServerHost; - } - - public void setProxyServerHost(final String proxyServerHost) { - this.proxyServerHost = proxyServerHost; - } - - @ConfigurationProperty(displayMessageKey = "proxyServerPort.display", - helpMessageKey = "proxyServerPort.help", - order = 12) - public Integer getProxyServerPort() { - return proxyServerPort; - } - - public void setProxyServerPort(final Integer proxyServerPort) { - this.proxyServerPort = proxyServerPort; - } - - @ConfigurationProperty(displayMessageKey = "proxyServerUser.display", - helpMessageKey = "proxyServerUser.help", - order = 13) - public String getProxyServerUser() { - return proxyServerUser; - } - - public void setProxyServerUser(final String proxyServerUser) { - this.proxyServerUser = proxyServerUser; - } - - @ConfigurationProperty(displayMessageKey = "proxyServerPassword.display", - helpMessageKey = "proxyServerPassword.help", - order = 14, confidential = true) - public String getProxyServerPassword() { - return proxyServerPassword; - } - - public void setProxyServerPassword(final String proxyServerPassword) { - this.proxyServerPassword = proxyServerPassword; - } - @Override public void validate() { if (StringUtil.isBlank(domain)) { @@ -293,17 +222,6 @@ public void validate() { throw new ConfigurationException("'customSchemaJSON' parameter must be a valid JSON."); } } - if (StringUtil.isNotBlank(proxyServerHost) && proxyServerPort == null - || StringUtil.isBlank(proxyServerHost) && proxyServerPort != null) { - throw new ConfigurationException("Proxy host and port cannot be empty."); - } - - if (StringUtil.isNotBlank(proxyServerUser) && StringUtil.isBlank(proxyServerPassword)) { - throw new ConfigurationException("Proxy server password cannot be null or empty if user is specified."); - } - if (StringUtil.isNotBlank(proxyServerPassword) && StringUtil.isBlank(proxyServerUser)) { - throw new ConfigurationException("Proxy server user cannot be null or empty if password is specified."); - } } private void initGoogleCredentials() { @@ -311,19 +229,24 @@ private void initGoogleCredentials() { if (null == googleCredentials) { UserCredentials.Builder credentialsBuilder = UserCredentials.newBuilder(); - boolean proxyEnabled = StringUtil.isNotBlank(proxyServerHost) && proxyServerPort != null; - if (proxyEnabled) { + Proxy.Type proxyType = + (System.getProperty("http.proxyHost") != null && System.getProperty("http.proxyPort") != null) + || (System.getProperty("https.proxyHost") != null + && System.getProperty("https.proxyPort") != null) + ? Proxy.Type.HTTP + : System.getProperty("socksProxyHost") != null + && System.getProperty("socksProxyPort") != null + ? Proxy.Type.SOCKS + : Proxy.Type.DIRECT; + + if (Proxy.Type.HTTP == proxyType) { HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.useSystemProperties(); - clientBuilder.setProxy(new HttpHost(proxyServerHost, proxyServerPort)); - if (StringUtil.isNotBlank(proxyServerUser) && StringUtil.isNotBlank(proxyServerPassword)) { - CredentialsProvider credsProvider = new BasicCredentialsProvider(); - credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), - new UsernamePasswordCredentials(proxyServerUser, proxyServerPassword)); - clientBuilder.setDefaultCredentialsProvider(credsProvider); - clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); - } - + clientBuilder.setProxy(System.getProperty("https.proxyHost") != null + ? new HttpHost(System.getProperty("https.proxyHost"), + Integer.parseInt(System.getProperty("https.proxyPort"))) + : new HttpHost(System.getProperty("http.proxyHost"), + Integer.parseInt(System.getProperty("http.proxyPort")))); credentialsBuilder.setHttpTransportFactory(new HttpTransportFactory() { @Override public HttpTransport create() { @@ -346,11 +269,19 @@ public HttpTransport create() { DirectoryScopes.ADMIN_DIRECTORY_GROUP, DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER)); - HttpTransport httpTransport = proxyEnabled - ? new NetHttpTransport.Builder().setProxy( - new Proxy(Proxy.Type.valueOf(proxyServerType.toUpperCase()), - new InetSocketAddress(proxyServerHost, proxyServerPort))).build() - : new NetHttpTransport(); + HttpTransport httpTransport = Proxy.Type.DIRECT == proxyType + ? new NetHttpTransport() + : new NetHttpTransport.Builder().setProxy(new Proxy(proxyType, + Proxy.Type.SOCKS == proxyType + ? new InetSocketAddress(System.getProperty("socksProxyHost"), + Integer.parseInt(System.getProperty("socksProxyHost"))) + : System.getProperty("https.proxyHost") != null + ? new InetSocketAddress(System.getProperty("https.proxyHost"), + Integer.parseInt(System.getProperty("https.proxyPort"))) + : new InetSocketAddress(System.getProperty("http.proxyHost"), + Integer.parseInt( + System.getProperty("http.proxyPort"))))) + .build(); HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials); directory = new Directory.Builder(httpTransport, JSON_FACTORY, requestInitializer). diff --git a/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties b/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties index 85603d3..797743e 100644 --- a/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties +++ b/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties @@ -42,16 +42,6 @@ productId.help=Google Product ID removeLicenseOnDisable.display=Remove license while disabling an user removeLicenseOnDisable.help=Also performs license removal when an user is disabled (suspend = true); Needs skuIds and productId to be defined. basic.group=Basic Configuration Properties -proxyServerType.display=Proxy Server Type -proxyServerType.help=Specifies the type of the proxy server to use (if any) to access to the SCIM server, allowed values are HTTP and SOCKS -proxyServerHost.display=Proxy Server Host -proxyServerHost.help=Specifies proxy host to connect to reach SCIM server -proxyServerPort.display=Proxy Server Port -proxyServerPort.help=Specifies proxy port to connect to reach SCIM server -proxyServerUser.display=Proxy Server Username -proxyServerUser.help=Specifies username to authenticate on proxy (optional, only Basic auth is supported) -proxyServerPassword.display=Proxy Server Password -proxyServerPassword.help=Specifies password to authenticate on proxy MESSAGE_OBJECT_CLASS___ACCOUNT__=Account MESSAGE_OBJECT_CLASS___GROUP__=Group diff --git a/src/test/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConnectorUnitTests.java b/src/test/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConnectorUnitTests.java index 5f0ea27..8bbc049 100644 --- a/src/test/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConnectorUnitTests.java +++ b/src/test/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConnectorUnitTests.java @@ -60,6 +60,10 @@ private static ConnectorFacade newFacade() { @BeforeAll public static void setUp() throws IOException { + + System.setProperty("http.proxyHost", "localhost"); + System.setProperty("http.proxyPort", "3128"); + CONN_CONF = new GoogleAppsConfiguration(); CONN_CONF.setClientId("aclientid"); @@ -96,9 +100,6 @@ public static void setUp() throws IOException { + "}]" + "}]"); - CONN_CONF.setProxyServerHost("localhost"); - CONN_CONF.setProxyServerPort(3128); - CONNECTOR = newFacade(); } From 00c4b23a4b1f0a6b59f06e6ff408998e2e024b79 Mon Sep 17 00:00:00 2001 From: Andrea Patricelli Date: Wed, 23 Oct 2024 10:48:41 +0200 Subject: [PATCH 3/4] removing unnecessary newline --- .../net/tirasa/connid/bundles/googleapps/Messages.properties | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties b/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties index 797743e..389e444 100644 --- a/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties +++ b/src/main/resources/net/tirasa/connid/bundles/googleapps/Messages.properties @@ -42,7 +42,6 @@ productId.help=Google Product ID removeLicenseOnDisable.display=Remove license while disabling an user removeLicenseOnDisable.help=Also performs license removal when an user is disabled (suspend = true); Needs skuIds and productId to be defined. basic.group=Basic Configuration Properties - MESSAGE_OBJECT_CLASS___ACCOUNT__=Account MESSAGE_OBJECT_CLASS___GROUP__=Group From e4680a77f0ac061fd80558d8b6a7fc1dca5e0c8e Mon Sep 17 00:00:00 2001 From: Andrea Patricelli Date: Wed, 23 Oct 2024 12:25:34 +0200 Subject: [PATCH 4/4] refactoring --- .../googleapps/GoogleAppsConfiguration.java | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java b/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java index 43549a5..ab1ef7b 100644 --- a/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java +++ b/src/main/java/net/tirasa/connid/bundles/googleapps/GoogleAppsConfiguration.java @@ -42,6 +42,7 @@ import java.net.Proxy; import java.util.Arrays; import java.util.List; +import java.util.Optional; import org.apache.http.HttpHost; import org.apache.http.impl.client.HttpClientBuilder; import org.identityconnectors.common.StringUtil; @@ -229,24 +230,28 @@ private void initGoogleCredentials() { if (null == googleCredentials) { UserCredentials.Builder credentialsBuilder = UserCredentials.newBuilder(); + Optional httpProxyHost = Optional.ofNullable(System.getProperty("http.proxyHost")); + Optional httpProxyPort = Optional.ofNullable(System.getProperty("http.proxyPort")); + Optional httpsProxyHost = Optional.ofNullable(System.getProperty("https.proxyHost")); + Optional httpsProxyPort = Optional.ofNullable(System.getProperty("https.proxyPort")); + Optional socksProxyHost = Optional.ofNullable(System.getProperty("socksProxyHost")); + Optional socksProxyPort = Optional.ofNullable(System.getProperty("socksProxyPort")); Proxy.Type proxyType = - (System.getProperty("http.proxyHost") != null && System.getProperty("http.proxyPort") != null) - || (System.getProperty("https.proxyHost") != null - && System.getProperty("https.proxyPort") != null) + (httpProxyHost.isPresent() && httpProxyPort.isPresent()) + || (httpsProxyHost.isPresent() && httpsProxyPort.isPresent()) ? Proxy.Type.HTTP - : System.getProperty("socksProxyHost") != null - && System.getProperty("socksProxyPort") != null + : socksProxyHost.isPresent() && socksProxyPort.isPresent() ? Proxy.Type.SOCKS : Proxy.Type.DIRECT; if (Proxy.Type.HTTP == proxyType) { HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.useSystemProperties(); - clientBuilder.setProxy(System.getProperty("https.proxyHost") != null - ? new HttpHost(System.getProperty("https.proxyHost"), - Integer.parseInt(System.getProperty("https.proxyPort"))) - : new HttpHost(System.getProperty("http.proxyHost"), - Integer.parseInt(System.getProperty("http.proxyPort")))); + clientBuilder.setProxy(httpsProxyHost.isPresent() && httpsProxyPort.isPresent() + ? new HttpHost(httpsProxyHost.get(), + Integer.parseInt(httpsProxyPort.get())) + : new HttpHost(httpProxyHost.get(), + Integer.parseInt(httpProxyPort.get()))); credentialsBuilder.setHttpTransportFactory(new HttpTransportFactory() { @Override public HttpTransport create() { @@ -273,14 +278,13 @@ public HttpTransport create() { ? new NetHttpTransport() : new NetHttpTransport.Builder().setProxy(new Proxy(proxyType, Proxy.Type.SOCKS == proxyType - ? new InetSocketAddress(System.getProperty("socksProxyHost"), - Integer.parseInt(System.getProperty("socksProxyHost"))) - : System.getProperty("https.proxyHost") != null - ? new InetSocketAddress(System.getProperty("https.proxyHost"), - Integer.parseInt(System.getProperty("https.proxyPort"))) - : new InetSocketAddress(System.getProperty("http.proxyHost"), - Integer.parseInt( - System.getProperty("http.proxyPort"))))) + ? new InetSocketAddress(socksProxyHost.get(), + Integer.parseInt(socksProxyHost.get())) + : httpsProxyHost.isPresent() && httpsProxyPort.isPresent() + ? new InetSocketAddress(httpsProxyHost.get(), + Integer.parseInt(httpsProxyPort.get())) + : new InetSocketAddress(httpProxyHost.get(), + Integer.parseInt(httpProxyPort.get())))) .build(); HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials);