package cn.com.taiji.video.config;

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

/**
 * @Description
 *
 * @Author  yangyue
 * @Date 2022/3/22
 */
@Configuration
public class HttpClientConfig {

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

    // @Bean
    // RestTemplate restTemplate() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    //     TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
    //     SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
    //     SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
    //
    //     Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
    //             .register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();
    //     BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(
    //             socketFactoryRegistry);
    //     CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
    //             .setConnectionManager(connectionManager).build();
    //     HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    //     requestFactory.setConnectTimeout(10000);
    //     requestFactory.setReadTimeout(10000);
    //     return new RestTemplate(requestFactory);
    // }

}
