io.freefair.spring.okhttp.autoconfigure.OkHttp3AutoConfiguration Maven / Gradle / Ivy
package io.freefair.spring.okhttp.autoconfigure;
import io.freefair.spring.okhttp.ApplicationInterceptor;
import io.freefair.spring.okhttp.NetworkInterceptor;
import io.freefair.spring.okhttp.OkHttp3Configurer;
import okhttp3.*;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import javax.net.ssl.HostnameVerifier;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
* @author Lars Grefer
*/
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@AutoConfiguration
@ConditionalOnClass(OkHttpClient.class)
@EnableConfigurationProperties(OkHttpProperties.class)
public class OkHttp3AutoConfiguration {
@Autowired
private OkHttpProperties okHttpProperties;
@Autowired
private ObjectProvider configurers;
@Autowired
@ApplicationInterceptor
private ObjectProvider applicationInterceptors;
@Autowired
@NetworkInterceptor
private ObjectProvider networkInterceptors;
@Bean
@ConditionalOnMissingBean
public OkHttpClient okHttp3Client(
ObjectProvider cache,
ObjectProvider cookieJar,
ObjectProvider dns,
ObjectProvider hostnameVerifier,
ObjectProvider certificatePinner,
ConnectionPool connectionPool,
ObjectProvider eventListener
) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
cache.ifUnique(builder::cache);
eventListener.ifUnique(builder::eventListener);
builder.connectTimeout(okHttpProperties.getConnectTimeout());
builder.readTimeout(okHttpProperties.getReadTimeout());
builder.writeTimeout(okHttpProperties.getWriteTimeout());
builder.pingInterval(okHttpProperties.getPingInterval());
cookieJar.ifUnique(builder::cookieJar);
dns.ifUnique(builder::dns);
hostnameVerifier.ifUnique(builder::hostnameVerifier);
certificatePinner.ifUnique(builder::certificatePinner);
builder.connectionPool(connectionPool);
builder.followRedirects(okHttpProperties.isFollowRedirects());
builder.followSslRedirects(okHttpProperties.isFollowSslRedirects());
builder.retryOnConnectionFailure(okHttpProperties.isRetryOnConnectionFailure());
applicationInterceptors.forEach(builder::addInterceptor);
networkInterceptors.forEach(builder::addNetworkInterceptor);
configurers.forEach(configurer -> configurer.configure(builder));
return builder.build();
}
@Bean
@ConditionalOnMissingBean
public ConnectionPool okHttp3ConnectionPool() {
int maxIdleConnections = okHttpProperties.getConnectionPool().getMaxIdleConnections();
Duration keepAliveDuration = okHttpProperties.getConnectionPool().getKeepAliveDuration();
return new ConnectionPool(maxIdleConnections, keepAliveDuration.toNanos(), TimeUnit.NANOSECONDS);
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "okhttp.cache.enabled", havingValue = "true", matchIfMissing = true)
public Cache okHttp3Cache() throws IOException {
File directory = okHttpProperties.getCache().getDirectory();
if (directory == null) {
directory = Files.createTempDirectory("okhttp-cache").toFile();
}
return new Cache(directory, okHttpProperties.getCache().getMaxSize().toBytes());
}
}