Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-patricelli committed Oct 23, 2024
1 parent 00c4b23 commit e4680a7
Showing 1 changed file with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -229,24 +230,28 @@ private void initGoogleCredentials() {
if (null == googleCredentials) {
UserCredentials.Builder credentialsBuilder = UserCredentials.newBuilder();

Optional<String> httpProxyHost = Optional.ofNullable(System.getProperty("http.proxyHost"));
Optional<String> httpProxyPort = Optional.ofNullable(System.getProperty("http.proxyPort"));
Optional<String> httpsProxyHost = Optional.ofNullable(System.getProperty("https.proxyHost"));
Optional<String> httpsProxyPort = Optional.ofNullable(System.getProperty("https.proxyPort"));
Optional<String> socksProxyHost = Optional.ofNullable(System.getProperty("socksProxyHost"));
Optional<String> 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() {
Expand All @@ -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);
Expand Down

0 comments on commit e4680a7

Please sign in to comment.