|
| 1 | +package Invokers; |
| 2 | + |
| 3 | +import com.cybersource.authsdk.core.ConfigException; |
| 4 | +import com.cybersource.authsdk.core.MerchantConfig; |
| 5 | +import com.cybersource.authsdk.util.GlobalLabelParameters; |
| 6 | +import okhttp3.ConnectionPool; |
| 7 | +import okhttp3.OkHttpClient; |
| 8 | + |
| 9 | +import java.util.Objects; |
| 10 | +import java.util.Properties; |
| 11 | +import java.util.concurrent.ConcurrentHashMap; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | + |
| 14 | +public class HttpClientFactory { |
| 15 | + private static final ConcurrentHashMap<Integer, OkHttpClient> _httpClientInstances = new ConcurrentHashMap<>(); |
| 16 | + |
| 17 | + public static OkHttpClient getHttpClient(MerchantConfig merchantConfig, HttpClientFactoryAdditionalSettings additionalSettings) throws ConfigException { |
| 18 | + if (merchantConfig == null) { |
| 19 | + Properties customProperties = new Properties(); |
| 20 | + customProperties.setProperty("userDefinedConnectionTimeout", GlobalLabelParameters.DEFAULT_MAX_CONNECT_TIMEOUT_IN_SECONDS); |
| 21 | + customProperties.setProperty("userDefinedReadTimeout", GlobalLabelParameters.DEFAULT_MAX_READ_TIMEOUT_IN_SECONDS); |
| 22 | + customProperties.setProperty("userDefinedWriteTimeout", GlobalLabelParameters.DEFAULT_MAX_WRITE_TIMEOUT_IN_SECONDS); |
| 23 | + customProperties.setProperty("userDefinedKeepAliveDuration", GlobalLabelParameters.DEFAULT_MAX_KEEP_ALIVE_DURATION_IN_SECONDS); |
| 24 | + customProperties.setProperty("userDefinedMaxIdleConnections", GlobalLabelParameters.DEFAULT_MAX_IDLE_CONNECTIONS); |
| 25 | + |
| 26 | + merchantConfig = new MerchantConfig(customProperties); |
| 27 | + } |
| 28 | + |
| 29 | + int hash = GetHashOfMerchantConfiguration(merchantConfig, additionalSettings); |
| 30 | + |
| 31 | + MerchantConfig finalMerchantConfig = merchantConfig; |
| 32 | + |
| 33 | + return _httpClientInstances.computeIfAbsent(hash, value -> createNewHttpClient(finalMerchantConfig, additionalSettings)); |
| 34 | + } |
| 35 | + |
| 36 | + private static OkHttpClient createNewHttpClient(MerchantConfig merchantConfig, HttpClientFactoryAdditionalSettings additionalSettings) { |
| 37 | + ConnectionPool customConnectionPool = new ConnectionPool(merchantConfig.getUserDefinedMaxIdleConnections(), merchantConfig.getUserDefinedKeepAliveDuration(), TimeUnit.SECONDS); |
| 38 | + |
| 39 | + OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
| 40 | + |
| 41 | + builder.connectionPool(customConnectionPool); |
| 42 | + builder.connectTimeout(merchantConfig.getUserDefinedConnectionTimeout(), TimeUnit.SECONDS); |
| 43 | + builder.readTimeout(merchantConfig.getUserDefinedReadTimeout(), TimeUnit.SECONDS); |
| 44 | + builder.writeTimeout(merchantConfig.getUserDefinedWriteTimeout(), TimeUnit.SECONDS); |
| 45 | + |
| 46 | + /* OPTIONAL ADD-ONS */ |
| 47 | + if (additionalSettings != null) { |
| 48 | + if (additionalSettings.getCustomLoggingInterceptor() != null) { |
| 49 | + builder.addInterceptor(additionalSettings.getCustomLoggingInterceptor()); |
| 50 | + } |
| 51 | + |
| 52 | + if (additionalSettings.getCustomRetryInterceptor() != null) { |
| 53 | + builder.addInterceptor(additionalSettings.getCustomRetryInterceptor()); |
| 54 | + } |
| 55 | + |
| 56 | + if (additionalSettings.getCustomSSLSocketFactory() != null && additionalSettings.getCustomX509TrustManager() != null) { |
| 57 | + builder.sslSocketFactory(additionalSettings.getCustomSSLSocketFactory(), additionalSettings.getCustomX509TrustManager()); |
| 58 | + } |
| 59 | + |
| 60 | + if (additionalSettings.getCustomHostnameVerifier() != null) { |
| 61 | + builder.hostnameVerifier(additionalSettings.getCustomHostnameVerifier()); |
| 62 | + } |
| 63 | + |
| 64 | + if (additionalSettings.getCustomRetryOnConnectionFailure() != null) { |
| 65 | + builder.retryOnConnectionFailure(additionalSettings.getCustomRetryOnConnectionFailure()); |
| 66 | + } |
| 67 | + |
| 68 | + if (additionalSettings.getCustomNetworkEventListener() != null) { |
| 69 | + builder.eventListener(additionalSettings.getCustomNetworkEventListener()); |
| 70 | + } |
| 71 | + |
| 72 | + if (additionalSettings.getCustomProxy() != null) { |
| 73 | + builder.proxy(additionalSettings.getCustomProxy()); |
| 74 | + |
| 75 | + if (additionalSettings.getCustomProxyAuthenticator() != null) { |
| 76 | + builder.proxyAuthenticator(additionalSettings.getCustomProxyAuthenticator()); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return builder.build(); |
| 82 | + } |
| 83 | + |
| 84 | + private static int GetHashOfMerchantConfiguration(MerchantConfig merchantConfig, HttpClientFactoryAdditionalSettings additionalSettings) { |
| 85 | + return Objects.hash( |
| 86 | + merchantConfig.getUserDefinedConnectionTimeout(), |
| 87 | + merchantConfig.getUserDefinedReadTimeout(), |
| 88 | + merchantConfig.getUserDefinedWriteTimeout(), |
| 89 | + merchantConfig.getUserDefinedKeepAliveDuration(), |
| 90 | + merchantConfig.getUserDefinedMaxIdleConnections(), |
| 91 | + additionalSettings.getCustomLoggingInterceptor(), |
| 92 | + additionalSettings.getCustomRetryInterceptor(), |
| 93 | + additionalSettings.getCustomSSLSocketFactory(), |
| 94 | + additionalSettings.getCustomX509TrustManager(), |
| 95 | + additionalSettings.getCustomHostnameVerifier(), |
| 96 | + additionalSettings.getCustomRetryOnConnectionFailure(), |
| 97 | + additionalSettings.getCustomNetworkEventListener(), |
| 98 | + additionalSettings.getCustomProxy(), |
| 99 | + additionalSettings.getCustomProxyAuthenticator() |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments