org.zodiac.okhttp.config.OkHttpConfigurer Maven / Gradle / Ivy
The newest version!
package org.zodiac.okhttp.config;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.slf4j.Logger;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
import org.zodiac.core.web.remote.config.WebRemoteConfigInfo;
import org.zodiac.core.web.support.RestTemplateHeaderInterceptor;
import org.zodiac.okhttp.logger.HttpLoggingInterceptor;
import org.zodiac.okhttp.logger.OkHttpSlf4jLogger;
import org.zodiac.sdk.toolkit.random.RandomHolders;
import org.zodiac.sdk.toolkit.ssl.DisableValidationTrustManager;
import org.zodiac.sdk.toolkit.ssl.TrustAllHostNames;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import okhttp3.Interceptor;
public abstract class OkHttpConfigurer {
protected final Logger log = SmartSlf4jLoggerFactory.getLogger(getClass());
protected OkHttpConfigurer() {}
protected HttpLoggingInterceptor loggingInterceptor(WebRemoteConfigInfo webRemoteConfigInfo) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new OkHttpSlf4jLogger());
interceptor.setLevel(webRemoteConfigInfo.getLevel());
return interceptor;
}
protected okhttp3.ConnectionPool createOkHttpConnectionPool(WebRemoteConfigInfo config) {
if (null == config) {
return new okhttp3.ConnectionPool();
}
int maxTotalConnections = config.getMaxIdleConnections();
long timeToLive = config.getTimeToLiveSeconds();
return new okhttp3.ConnectionPool(maxTotalConnections, timeToLive, TimeUnit.SECONDS);
}
protected okhttp3.OkHttpClient createOkHttpClient(okhttp3.ConnectionPool connectionPool,
List interceptors, WebRemoteConfigInfo config) {
okhttp3.OkHttpClient.Builder builder = null;
if (null == config) {
builder = this.createBuilder(true);
} else {
builder = this.createBuilder(config.isDisableSslValidation());
boolean followRedirects = config.isFollowRedirects();
int connectTimeout = config.getConnectTimeoutMills();
builder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
.writeTimeout(config.getWriteTimeoutMills(), TimeUnit.MILLISECONDS)
.readTimeout(config.getReadTimeoutMills(), TimeUnit.MILLISECONDS).followRedirects(followRedirects);
if (null != connectionPool) {
builder.connectionPool(connectionPool);
}
if (CollUtil.isNotEmptyColl(interceptors)) {
interceptors.stream().forEach(builder::addInterceptor);
}
}
okhttp3.OkHttpClient client = builder.build();
return client;
}
protected RestTemplateHeaderInterceptor requestHeaderInterceptor() {
return new RestTemplateHeaderInterceptor();
}
private okhttp3.OkHttpClient.Builder createBuilder(boolean disableSslValidation) {
okhttp3.OkHttpClient.Builder builder = new okhttp3.OkHttpClient.Builder();
if (disableSslValidation) {
try {
X509TrustManager disabledTrustManager = DisableValidationTrustManager.getInstance();
TrustManager[] trustManagers = new TrustManager[] {disabledTrustManager};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManagers, RandomHolders.SECURE_RANDOM_HOLDER.get());
SSLSocketFactory disabledSslSocketFactory = sslContext.getSocketFactory();
builder.sslSocketFactory(disabledSslSocketFactory, disabledTrustManager);
builder.hostnameVerifier(TrustAllHostNames.getInstance());
} catch (NoSuchAlgorithmException | KeyManagementException e) {
log.warn("Error setting SSLSocketFactory in OKHttpClient", e);
}
}
return builder;
}
}