|
| 1 | +package com.sinch.sdk.domains.numberlookup.api.v2.adapters; |
| 2 | + |
| 3 | +import com.sinch.sdk.auth.adapters.OAuthManager; |
| 4 | +import com.sinch.sdk.core.http.AuthManager; |
| 5 | +import com.sinch.sdk.core.http.HttpClient; |
| 6 | +import com.sinch.sdk.core.http.HttpMapper; |
| 7 | +import com.sinch.sdk.core.models.ServerConfiguration; |
| 8 | +import com.sinch.sdk.core.utils.StringUtil; |
| 9 | +import com.sinch.sdk.domains.numberlookup.api.v2.NumberLookupV2Service; |
| 10 | +import com.sinch.sdk.models.NumberLookupContext; |
| 11 | +import com.sinch.sdk.models.UnifiedCredentials; |
| 12 | +import java.util.AbstractMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.function.Supplier; |
| 16 | +import java.util.logging.Logger; |
| 17 | +import java.util.stream.Collectors; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +public class NumberLookupService |
| 21 | + implements com.sinch.sdk.domains.numberlookup.api.v2.NumberLookupService { |
| 22 | + |
| 23 | + private static final Logger LOGGER = Logger.getLogger(NumberLookupService.class.getName()); |
| 24 | + private static final String SECURITY_SCHEME_KEYWORD_NUMBER_LOOKUP = "OAuth2"; |
| 25 | + |
| 26 | + private final UnifiedCredentials credentials; |
| 27 | + private final NumberLookupContext context; |
| 28 | + private final ServerConfiguration oAuthServer; |
| 29 | + private final Supplier<HttpClient> httpClientSupplier; |
| 30 | + |
| 31 | + private volatile String uriUUID; |
| 32 | + private volatile Map<String, AuthManager> authManagers; |
| 33 | + |
| 34 | + private volatile NumberLookupV2Service lookup; |
| 35 | + |
| 36 | + public NumberLookupService( |
| 37 | + UnifiedCredentials credentials, |
| 38 | + NumberLookupContext context, |
| 39 | + ServerConfiguration oAuthServer, |
| 40 | + Supplier<HttpClient> httpClientSupplier) { |
| 41 | + this.credentials = credentials; |
| 42 | + this.context = context; |
| 43 | + this.oAuthServer = oAuthServer; |
| 44 | + this.httpClientSupplier = httpClientSupplier; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public NumberLookupV2Service lookup() { |
| 49 | + if (null == this.lookup) { |
| 50 | + synchronized (this) { |
| 51 | + if (null == this.lookup) { |
| 52 | + instanceLazyInit(); |
| 53 | + this.lookup = |
| 54 | + new NumberLookupV2ServiceImpl( |
| 55 | + httpClientSupplier.get(), |
| 56 | + context.getNumberLookupServer(), |
| 57 | + authManagers, |
| 58 | + HttpMapper.getInstance(), |
| 59 | + uriUUID); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return this.lookup; |
| 64 | + } |
| 65 | + |
| 66 | + private void instanceLazyInit() { |
| 67 | + if (null != this.authManagers) { |
| 68 | + return; |
| 69 | + } |
| 70 | + synchronized (this) { |
| 71 | + if (null == this.authManagers) { |
| 72 | + Objects.requireNonNull( |
| 73 | + credentials, "Number Lookup service requires unified credentials to be defined"); |
| 74 | + Objects.requireNonNull(context, "Number Lookup service requires context to be defined"); |
| 75 | + StringUtil.requireNonEmpty( |
| 76 | + credentials.getKeyId(), "Number Lookup service requires 'keyId' to be defined"); |
| 77 | + StringUtil.requireNonEmpty( |
| 78 | + credentials.getKeySecret(), "Number Lookup service requires 'keySecret' to be defined"); |
| 79 | + StringUtil.requireNonEmpty( |
| 80 | + credentials.getProjectId(), "Number Lookup service requires 'projectId' to be defined"); |
| 81 | + StringUtil.requireNonEmpty( |
| 82 | + context.getNumberLookupUrl(), |
| 83 | + "Number Lookup service requires 'numberLookupUrl' to be defined"); |
| 84 | + |
| 85 | + LOGGER.fine( |
| 86 | + "Activate Number Lookup API with server='" |
| 87 | + + context.getNumberLookupServer().getUrl() |
| 88 | + + "'"); |
| 89 | + |
| 90 | + AuthManager authManager = |
| 91 | + new OAuthManager( |
| 92 | + credentials, oAuthServer, HttpMapper.getInstance(), httpClientSupplier); |
| 93 | + |
| 94 | + uriUUID = credentials.getProjectId(); |
| 95 | + authManagers = |
| 96 | + Stream.of( |
| 97 | + new AbstractMap.SimpleEntry<>( |
| 98 | + SECURITY_SCHEME_KEYWORD_NUMBER_LOOKUP, authManager)) |
| 99 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments