package top.quantic.sentry.config;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.cache.CacheConfig;
import org.apache.http.impl.client.cache.CachingHttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.io.File;

@Configuration
public class HttpClientConfiguration {

    @Autowired
    private SentryProperties sentryProperties;

    @Bean
    public ClientHttpRequestFactory httpRequestFactory() {
        return new HttpComponentsClientHttpRequestFactory(httpClient());
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate(httpRequestFactory());
    }

    @Bean
    public CloseableHttpClient httpClient() {
        RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(properties().getReadTimeout())
            .setSocketTimeout(properties().getSocketTimeout())
            .build();

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(properties().getMaxTotalConnections());
        connectionManager.setDefaultMaxPerRoute(properties().getMaxConnectionsPerRoute());

        CacheConfig cacheConfig = CacheConfig.custom()
            .setMaxCacheEntries(properties().getMaxCacheEntries())
            .setMaxObjectSize(properties().getMaxObjectSize())
            .setHeuristicCachingEnabled(true) // important!
            .build();

        return CachingHttpClients.custom()
            .setCacheConfig(cacheConfig)
            .setCacheDir(new File(properties().getCacheDir()))
            .setDefaultRequestConfig(requestConfig)
            .setConnectionManager(connectionManager)
            .build();
    }

    private SentryProperties.Http properties() {
        return sentryProperties.getHttp();
    }
}

@@ -321,16 +321,73 @@
 anager;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -776,62 +776,213 @@
 le;%0A
-%0A@Configuration%0Apublic class HttpClientConfiguration %7B
+import java.io.IOException;%0Aimport java.nio.file.Files;%0A%0A@Configuration%0Apublic class HttpClientConfiguration %7B%0A%0A    private static final Logger log = LoggerFactory.getLogger(HttpClientConfiguration.class);
 %0A%0A  
@@ -2104,32 +2104,504 @@
      .build();%0A%0A
+        File cacheDir = new File(properties().getCacheDir());%0A        try %7B%0A            Files.createDirectories(cacheDir.toPath());%0A        %7D catch (IOException e) %7B%0A            log.warn(%22Could not create cache directory - using temp folder%22, e);%0A            try %7B%0A                cacheDir = Files.createTempDirectory(%22cache%22).toFile();%0A            %7D catch (IOException ee) %7B%0A                log.warn(%22Could not create temp cache directory%22, ee);%0A            %7D%0A        %7D%0A%0A
         return C
@@ -2697,44 +2697,16 @@
 Dir(
-new File(properties().getC
+c
 acheDir
-())
 )%0A  

Create cache folders if missing