Skip to content

Commit c56bb89

Browse files
committed
fixes #2229 Add httpClient to PathPrefixAuth to cache the client instance
1 parent 0829a48 commit c56bb89

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

client/src/main/java/com/networknt/client/Http2Client.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public class Http2Client {
135135

136136

137137
// TokenManager is to manage cached jwt tokens for this client.
138-
private TokenManager tokenManager = TokenManager.getInstance();
138+
private final TokenManager tokenManager = TokenManager.getInstance();
139139

140140
// This is the old connection pool that is kept for backward compatibility.
141141
private final Http2ClientConnectionPool http2ClientConnectionPool = Http2ClientConnectionPool.getInstance();

config/src/main/java/com/networknt/config/PathPrefixAuth.java

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.networknt.config;
22

3+
import java.net.http.HttpClient;
4+
35
/**
46
* This is an object that contains all the authentication info for each path prefix in the pathPrefixAuth config
57
* section. By making it a list of objects, we can support unlimited number of APIs with different authentication
@@ -28,6 +30,7 @@ public class PathPrefixAuth {
2830

2931
long expiration;
3032
String accessToken;
33+
HttpClient httpClient;
3134

3235
public PathPrefixAuth() {
3336
}
@@ -191,4 +194,12 @@ public String getScope() {
191194
public void setScope(String scope) {
192195
this.scope = scope;
193196
}
197+
198+
public HttpClient getHttpClient() {
199+
return httpClient;
200+
}
201+
202+
public void setHttpClient(HttpClient httpClient) {
203+
this.httpClient = httpClient;
204+
}
194205
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.networknt.config;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.util.List;
8+
9+
public class PathPrefixAuthTest {
10+
11+
/**
12+
* This test can confirm that even httpClient is added to the class, we can still use jackson to
13+
* convert a string into an array of PathPrefixAuth objects.
14+
*/
15+
@Test
16+
public void testPathPrefixAuth() {
17+
String s = "[{\"clientId\":\"my-client\",\"clientSecret\":\"my-secret\",\"tokenUrl\":\"www.example.com/token\"}]";
18+
List<PathPrefixAuth> pathPrefixAuths = null;
19+
try {
20+
pathPrefixAuths = Config.getInstance().getMapper().readValue(s, new TypeReference<>() {});
21+
} catch (Exception e) {
22+
e.printStackTrace();
23+
}
24+
Assert.assertNotNull(pathPrefixAuths);
25+
Assert.assertEquals(pathPrefixAuths.size(), 1);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)