Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Add support for UseDefaultCredentials Option #517

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<jacoco-maven-plugin.version>0.7.5.201505241946</jacoco-maven-plugin.version>
<!-- Dependencies [COMPILE]: -->
<httpclient.version>4.4.1</httpclient.version>
<httpclient-win.version>4.4.1</httpclient-win.version>
<httpcore.version>4.4.1</httpcore.version>
<commons-logging.version>1.2</commons-logging.version>
<joda-time.version>2.8</joda-time.version>
Expand Down Expand Up @@ -219,6 +220,12 @@
<version>${httpclient.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-win</artifactId>
<version>${httpclient-win.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.WinHttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

Expand Down Expand Up @@ -171,7 +173,7 @@ public abstract class ExchangeServiceBase implements Closeable {
* every other constructor.
*/
protected ExchangeServiceBase() {
setUseDefaultCredentials(true);
setUseDefaultCredentials(false);
initializeHttpClient();
initializeHttpContext();
}
Expand Down Expand Up @@ -200,7 +202,7 @@ private void initializeHttpClient() {
HttpClientConnectionManager httpConnectionManager = new BasicHttpClientConnectionManager(registry);
AuthenticationStrategy authStrategy = new CookieProcessingTargetAuthenticationStrategy();

httpClient = HttpClients.custom()
httpClient = constructHttpClientBuilder()
.setConnectionManager(httpConnectionManager)
.setTargetAuthenticationStrategy(authStrategy)
.build();
Expand All @@ -213,11 +215,21 @@ private void initializeHttpPoolingClient() {
httpConnectionManager.setDefaultMaxPerRoute(maximumPoolingConnections);
AuthenticationStrategy authStrategy = new CookieProcessingTargetAuthenticationStrategy();

httpPoolingClient = HttpClients.custom()
httpPoolingClient = constructHttpClientBuilder()
.setConnectionManager(httpConnectionManager)
.setTargetAuthenticationStrategy(authStrategy)
.build();
}

private HttpClientBuilder constructHttpClientBuilder(){
if(useDefaultCredentials) {
if(System.getProperty("os.name").toLowerCase().contains("windows")) {
return WinHttpClients.custom();
}
}

return HttpClients.custom();
}

/**
* Sets the maximum number of connections for the pooling connection manager which is used for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.win.WindowsCredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
Expand Down Expand Up @@ -152,6 +153,10 @@ public void prepareConnection() {
credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), webServiceCredentials);
}

if(isUseDefaultCredentials() && System.getProperty("os.name").toLowerCase().contains("windows")) {
credentialsProvider = new WindowsCredentialsProvider(credentialsProvider);
}

httpContext.setCredentialsProvider(credentialsProvider);

httpPost.setConfig(requestConfigBuilder.build());
Expand Down