Skip to content

Commit 5243968

Browse files
committed
refactor: use same client in CouchDbSessionAuthenticator
1 parent 3885a01 commit 5243968

File tree

4 files changed

+13
-50
lines changed

4 files changed

+13
-50
lines changed

modules/common/src/main/java/com/ibm/cloud/cloudant/internal/CloudantBaseService.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* © Copyright IBM Corporation 2020, 2022. All Rights Reserved.
2+
* © Copyright IBM Corporation 2020, 2024. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -53,7 +53,9 @@ protected OkHttpClient configureHttpClient() {
5353
// If we are using a CouchDB session authenticator we need to customize the cookie jar
5454
customizeAuthenticator(a -> builder
5555
.cookieJar(a.getCookieJar()));
56-
return builder.build();
56+
OkHttpClient client = builder.build();
57+
this.setClient(client);
58+
return this.getClient();
5759
}
5860

5961
@Override
@@ -76,10 +78,16 @@ public void setServiceUrl(String serviceUrl) {
7678
});
7779
}
7880

81+
@Override
82+
public void setClient(OkHttpClient client) {
83+
super.setClient(client);
84+
customizeAuthenticator(a -> a.setClient(this.getClient()));
85+
}
86+
7987
@Override
8088
public void configureClient(HttpConfigOptions options) {
8189
super.configureClient(options);
82-
customizeAuthenticator(a -> a.setHttpConfigOptions(options));
90+
customizeAuthenticator(a -> a.setClient(this.getClient()));
8391
}
8492

8593
@Override

modules/common/src/main/java/com/ibm/cloud/cloudant/security/CouchDbSessionAuthenticator.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
package com.ibm.cloud.cloudant.security;
1515

1616
import com.google.gson.JsonObject;
17-
import com.ibm.cloud.sdk.core.http.HttpClientSingleton;
18-
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
1917
import com.ibm.cloud.sdk.core.http.RequestBuilder;
2018
import com.ibm.cloud.sdk.core.http.ServiceCookieJar;
2119
import com.ibm.cloud.sdk.core.security.AbstractToken;
@@ -42,8 +40,7 @@
4240
*/
4341
public class CouchDbSessionAuthenticator
4442
extends TokenRequestBasedAuthenticator<CouchDbSessionAuthenticator.CouchDbSessionToken,
45-
CouchDbSessionAuthenticator.CouchDbSessionToken>
46-
implements Authenticator {
43+
CouchDbSessionAuthenticator.CouchDbSessionToken> {
4744

4845
public static final String AUTH_TYPE = "COUCHDB_SESSION";
4946

@@ -52,7 +49,6 @@ public class CouchDbSessionAuthenticator
5249
private final ServiceCookieJar cookieJar;
5350

5451
private HttpUrl sessionUrl = null;
55-
private HttpConfigOptions options = null;
5652
private Headers headers = null;
5753

5854
/**
@@ -91,18 +87,6 @@ public ServiceCookieJar getCookieJar() {
9187
return this.cookieJar;
9288
}
9389

94-
/**
95-
* Setter to configure this authenticator with HttpConfigOptions. This is called
96-
* internally to
97-
* apply the configuration options of the
98-
* service client automatically.
99-
*
100-
* @param options
101-
*/
102-
public void setHttpConfigOptions(HttpConfigOptions options) {
103-
this.options = options;
104-
}
105-
10690
/**
10791
* Setter to configure this authenticator with a new session URL derived from the supplied
10892
* service URL. This is called internally to apply the service URL from the
@@ -164,18 +148,6 @@ public CouchDbSessionToken requestToken() {
164148
}
165149
}
166150

167-
// Build the client if we need to
168-
if (getClient() == null) {
169-
setClient(
170-
HttpClientSingleton
171-
.getInstance()
172-
.configureClient(options)
173-
.newBuilder()
174-
.cookieJar(cookieJar)
175-
.build()
176-
);
177-
}
178-
179151
try (Response response = getClient().newCall(postSessionbuilder.build()).execute()) {
180152
if (response.isSuccessful()) {
181153
List<Cookie> cookies = Cookie.parseAll(sessionUrl, response.headers());

modules/common/src/test/java/com/ibm/cloud/cloudant/internal/SdkTimeoutTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
// Every method tests an authenticator.
4040
public class SdkTimeoutTest {
4141
final long defaultTimeoutValue = 150L;
42-
final long authenticationTimeoutValue = 90L;
4342
final long customTimeoutValue = 10L;
4443

4544
// Every test case tests a timeout settings.
@@ -163,7 +162,7 @@ void testSessionAuthTimeout() throws NoSuchFieldException, IllegalAccessExceptio
163162

164163
CouchDbSessionAuthenticator auth = (CouchDbSessionAuthenticator) myService.getAuthenticator();
165164
OkHttpClient c = auth.getClient();
166-
assertEquals(TimeUnit.MILLISECONDS.toSeconds(c.readTimeoutMillis()), authenticationTimeoutValue);
165+
assertEquals(TimeUnit.MILLISECONDS.toSeconds(c.readTimeoutMillis()), testCases[i]);
167166
}
168167
}
169168

modules/common/src/test/java/com/ibm/cloud/cloudant/security/CouchDbSessionAuthenticatorTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import com.ibm.cloud.cloudant.common.SdkCommon;
1717
import com.ibm.cloud.cloudant.internal.CloudantBaseService;
1818
import com.ibm.cloud.cloudant.internal.TestCloudantService;
19-
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
2019
import com.ibm.cloud.sdk.core.http.Response;
2120
import com.ibm.cloud.sdk.core.security.Authenticator;
2221
import com.ibm.cloud.sdk.core.service.exception.ServiceResponseException;
@@ -244,21 +243,6 @@ void getCookieJar() {
244243

245244
}
246245

247-
/**
248-
* Test that options set on the client apply to the token request
249-
*/
250-
@Test
251-
void setHttpConfigOptions() {
252-
CouchDbSessionAuthenticator a =
253-
Mockito.spy(testAuthenticator);
254-
TestCloudantService testCloudantService = new TestCloudantService("test", a);
255-
256-
HttpConfigOptions options = new HttpConfigOptions.Builder().build();
257-
testCloudantService.configureClient(options);
258-
// Verify the setter was called
259-
Mockito.verify(a).setHttpConfigOptions(options);
260-
}
261-
262246
/**
263247
* Test that setting default headers for the client apply to the token request
264248
*/

0 commit comments

Comments
 (0)