All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.freefair.spring.okhttp.autoconfigure.OkHttp3AutoConfiguration Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
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 jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.util.FileSystemUtils;

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
 */
@Slf4j
@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;

    private File tempDirCache = null;

    @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) {
            tempDirCache = Files.createTempDirectory("okhttp-cache").toFile();
            directory = tempDirCache;
        }
        return new Cache(directory, okHttpProperties.getCache().getMaxSize().toBytes());
    }

    @PreDestroy
    public void deleteTempCache() {
        if (tempDirCache != null) {
            log.debug("Deleting the temporary OkHttp Cache at {}", tempDirCache.getAbsolutePath());
            try {
                FileSystemUtils.deleteRecursively(tempDirCache);
            } catch (Exception e) {
                log.warn("Failed to delete the temporary OkHttp Cache at {}", tempDirCache.getAbsolutePath());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy