Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
improve readability of code by simplifying check for null value of currentHttpClient variable.
  • Loading branch information
ashitsalesforce committed Sep 18, 2024
1 parent b590e8f commit 99c74b6
Showing 1 changed file with 34 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,49 +182,44 @@ private boolean areEquivalentConfigs(ConnectorConfig config1, ConnectorConfig co
}

private static synchronized void initializeHttpClient() throws UnknownHostException {
if (!isReuseConnection()) {
closeConnections();
currentHttpClient = null;
if (isReuseConnection() && currentHttpClient != null) {
// already initialized.
return;
}
if (currentHttpClient == null) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().useSystemProperties();

if (currentConfig.getProxy().address() != null) {
String proxyUser = currentConfig.getProxyUsername() == null ? "" : currentConfig.getProxyUsername();
String proxyPassword = currentConfig.getProxyPassword() == null ? "" : currentConfig.getProxyPassword();

InetSocketAddress proxyAddress = (InetSocketAddress) currentConfig.getProxy().address();
HttpHost proxyHost = new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort(), "http");
httpClientBuilder.setProxy(proxyHost);

CredentialsProvider credentialsprovider = new BasicCredentialsProvider();
AuthScope scope = new AuthScope(proxyAddress.getHostName(), proxyAddress.getPort(), null, null);
httpClientBuilder.setDefaultCredentialsProvider(credentialsprovider);

Credentials credentials;
if (AppUtil.getOSType() == AppUtil.OSType.WINDOWS) {
String computerName = InetAddress.getLocalHost().getCanonicalHostName();
credentials = new NTCredentials(proxyUser, proxyPassword, computerName, currentConfig.getNtlmDomain());
} else {
credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
}
credentialsprovider.setCredentials(scope, credentials);
currentHttpClient = httpClientBuilder.build();
if (AppUtil.getOSType() == AppUtil.OSType.WINDOWS) {
try (CloseableHttpResponse ignored = currentHttpClient.execute(new HttpHead("http://salesforce.com"))) {
} catch (Exception e) {
logger.info("Unable to use NTCredentials for proxy. Switching to UsernamePasswordCredentials");
credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
credentialsprovider.setCredentials(scope, credentials);
currentHttpClient = httpClientBuilder.build();
}
}
closeConnections();
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().useSystemProperties();

if (currentConfig.getProxy().address() != null) {
String proxyUser = currentConfig.getProxyUsername() == null ? "" : currentConfig.getProxyUsername();
String proxyPassword = currentConfig.getProxyPassword() == null ? "" : currentConfig.getProxyPassword();

InetSocketAddress proxyAddress = (InetSocketAddress) currentConfig.getProxy().address();
HttpHost proxyHost = new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort(), "http");
httpClientBuilder.setProxy(proxyHost);

CredentialsProvider credentialsprovider = new BasicCredentialsProvider();
AuthScope scope = new AuthScope(proxyAddress.getHostName(), proxyAddress.getPort(), null, null);
httpClientBuilder.setDefaultCredentialsProvider(credentialsprovider);

Credentials credentials;
if (AppUtil.getOSType() == AppUtil.OSType.WINDOWS) {
String computerName = InetAddress.getLocalHost().getCanonicalHostName();
credentials = new NTCredentials(proxyUser, proxyPassword, computerName, currentConfig.getNtlmDomain());
} else {
credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
}

if (currentHttpClient == null) {
currentHttpClient = httpClientBuilder.build();
credentialsprovider.setCredentials(scope, credentials);
currentHttpClient = httpClientBuilder.build();
if (AppUtil.getOSType() == AppUtil.OSType.WINDOWS) {
try (CloseableHttpResponse ignored = currentHttpClient.execute(new HttpHead("http://salesforce.com"))) {
} catch (Exception e) {
logger.info("Unable to use NTCredentials for proxy. Switching to UsernamePasswordCredentials");
credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
credentialsprovider.setCredentials(scope, credentials);
}
}
}
currentHttpClient = httpClientBuilder.build();
}

@Override
Expand Down

0 comments on commit 99c74b6

Please sign in to comment.