|
23 | 23 | */
|
24 | 24 | package hec.army.usace.hec.cwbi.auth.http.client;
|
25 | 25 |
|
26 |
| - |
27 |
| -import static hec.army.usace.hec.cwbi.auth.http.client.trustmanagers.CwbiAuthTrustManager.TOKEN_URL; |
28 |
| - |
29 | 26 | import java.io.IOException;
|
| 27 | +import java.io.UncheckedIOException; |
30 | 28 | import java.util.Collections;
|
| 29 | +import java.util.Map; |
31 | 30 | import java.util.Objects;
|
| 31 | +import java.util.concurrent.ConcurrentHashMap; |
32 | 32 | import javax.net.ssl.KeyManager;
|
33 | 33 | import javax.net.ssl.SSLSocketFactory;
|
34 | 34 | import mil.army.usace.hec.cwms.http.client.auth.OAuth2TokenProvider;
|
35 | 35 |
|
36 | 36 | public final class CwbiAuthUtil {
|
37 | 37 |
|
| 38 | + private static final Map<String, OAuth2TokenProvider> TOKEN_PROVIDER_CACHE = new ConcurrentHashMap<>(); |
| 39 | + |
38 | 40 | private CwbiAuthUtil() {
|
39 | 41 | throw new AssertionError("Utility class");
|
40 | 42 | }
|
41 | 43 |
|
42 | 44 | /**
|
43 | 45 | * Builds CumulusTokenProvider for retrieving and refreshing tokens for cumulus authentication.
|
44 |
| - * @param keyManager - KeyManager for client |
45 |
| - * @return OAuth2TokenProvider - CumulusTokenProvider |
| 46 | + * Caches the TokenProvider instance per KeyCloak token URL to prevent redundant creation. |
| 47 | + * Ensures thread safety and propagates IOException. |
| 48 | + * |
| 49 | + * @param tokenUrl - KeyCloak token URL |
| 50 | + * @param clientId - Client ID for authentication |
| 51 | + * @param keyManager - KeyManager for client SSL |
| 52 | + * @return OAuth2TokenProvider - Cached or newly created TokenProvider |
46 | 53 | * @throws IOException - thrown if failed to build CumulusTokenProvider
|
47 | 54 | */
|
48 |
| - public static OAuth2TokenProvider buildCwbiAuthTokenProvider(String clientId, KeyManager keyManager) throws IOException { |
49 |
| - SSLSocketFactory sslSocketFactory = CwbiAuthSslSocketFactory.buildSSLSocketFactory( |
50 |
| - Collections.singletonList(Objects.requireNonNull(keyManager, "Missing required KeyManager"))); |
51 |
| - return new CwbiAuthTokenProvider(TOKEN_URL, clientId, sslSocketFactory); |
52 |
| - } |
| 55 | + public static OAuth2TokenProvider buildCwbiAuthTokenProvider(String tokenUrl, String clientId, KeyManager keyManager) throws IOException { |
| 56 | + Objects.requireNonNull(tokenUrl, "Missing required tokenUrl"); |
| 57 | + Objects.requireNonNull(clientId, "Missing required clientId"); |
| 58 | + Objects.requireNonNull(keyManager, "Missing required KeyManager"); |
53 | 59 |
|
| 60 | + try { |
| 61 | + return TOKEN_PROVIDER_CACHE.computeIfAbsent(tokenUrl, url -> { |
| 62 | + try { |
| 63 | + SSLSocketFactory sslSocketFactory = CwbiAuthSslSocketFactory.buildSSLSocketFactory( |
| 64 | + Collections.singletonList(keyManager)); |
| 65 | + return new CwbiAuthTokenProvider(url, clientId, sslSocketFactory); |
| 66 | + } catch (IOException e) { |
| 67 | + throw new UncheckedIOException(e); |
| 68 | + } |
| 69 | + }); |
| 70 | + } catch (UncheckedIOException e) { |
| 71 | + throw e.getCause(); |
| 72 | + } |
| 73 | + } |
54 | 74 | }
|
| 75 | + |
| 76 | + |
0 commit comments