Skip to content

Commit 5b8d870

Browse files
committed
refactor: use core's setClient() and getClient()
1 parent e559b43 commit 5b8d870

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* © Copyright IBM Corporation 2020. 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
@@ -28,7 +28,6 @@
2828
import okhttp3.Cookie;
2929
import okhttp3.Headers;
3030
import okhttp3.HttpUrl;
31-
import okhttp3.OkHttpClient;
3231
import okhttp3.Response;
3332

3433
import java.io.IOException;
@@ -55,7 +54,6 @@ public class CouchDbSessionAuthenticator
5554
private HttpUrl sessionUrl = null;
5655
private HttpConfigOptions options = null;
5756
private Headers headers = null;
58-
private OkHttpClient sessionAuthClient = null;
5957

6058
/**
6159
* Get a new Authenticator instance for the supplied service URL and credentials that provides
@@ -166,27 +164,28 @@ public CouchDbSessionToken requestToken() {
166164
}
167165
}
168166

169-
try {
170-
// Build the client if we need to
171-
if (sessionAuthClient == null) {
172-
sessionAuthClient = HttpClientSingleton
173-
.getInstance()
174-
.configureClient(options)
175-
.newBuilder()
176-
.cookieJar(cookieJar)
177-
.build();
178-
}
179-
try (Response response = sessionAuthClient.newCall(postSessionbuilder.build()).execute()) {
180-
if (response.isSuccessful()) {
181-
List<Cookie> cookies = Cookie.parseAll(sessionUrl, response.headers());
182-
for (Cookie cookie : cookies) {
183-
if ("AuthSession".equals(cookie.name())) {
184-
return new CouchDbSessionToken(cookie.expiresAt());
185-
}
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+
179+
try (Response response = getClient().newCall(postSessionbuilder.build()).execute()) {
180+
if (response.isSuccessful()) {
181+
List<Cookie> cookies = Cookie.parseAll(sessionUrl, response.headers());
182+
for (Cookie cookie : cookies) {
183+
if ("AuthSession".equals(cookie.name())) {
184+
return new CouchDbSessionToken(cookie.expiresAt());
186185
}
187186
}
188-
throw new ServiceResponseException(response.code(), response);
189187
}
188+
throw new ServiceResponseException(response.code(), response);
190189
} catch (RuntimeException | IOException t) {
191190
return new CouchDbSessionToken(t);
192191
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* © Copyright IBM Corporation 2021. All Rights Reserved.
2+
* © Copyright IBM Corporation 2021, 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
@@ -162,9 +162,7 @@ void testSessionAuthTimeout() throws NoSuchFieldException, IllegalAccessExceptio
162162
assertEquals(TimeUnit.MILLISECONDS.toSeconds(testRequestCall.getClient().readTimeoutMillis()), testCases[i]);
163163

164164
CouchDbSessionAuthenticator auth = (CouchDbSessionAuthenticator) myService.getAuthenticator();
165-
Field clientField = auth.getClass().getDeclaredField("sessionAuthClient");
166-
clientField.setAccessible(true);
167-
OkHttpClient c = (OkHttpClient) clientField.get(auth);
165+
OkHttpClient c = auth.getClient();
168166
assertEquals(TimeUnit.MILLISECONDS.toSeconds(c.readTimeoutMillis()), authenticationTimeoutValue);
169167
}
170168
}

0 commit comments

Comments
 (0)