package com.trade.common.utils.http;

import com.trade.common.exception.HttpProcessException;
import org.apache.http.impl.client.HttpClientBuilder;

/**
 * 自定义 HttpClient 对象创建实体类，支持级联调用
 * <p>
 * Copyright (c) 2019 - 2029 摸金校尉 All Rights Reserved.
 * <p>
 * Created by wanxiangZ 2019/8/14 19:36
 */
public class HttpClientBuilderCustom extends HttpClientBuilder {

    private final SSLs.SSLProtocolVersion sslpv = SSLs.SSLProtocolVersion.TLSv1_2;// ssl 协议版本
    /**
     * 注意：LianLian 服务器采用的是 TLSv1.1 版本 Created by wanxiangZ 2021/8/27 12:00
     */
    private final SSLs.SSLProtocolVersion sslpv1_1 = SSLs.SSLProtocolVersion.TLSv1_1;// ssl 协议版本

    private HttpClientBuilderCustom(){
    }

    public static HttpClientBuilderCustom custom() {
        return new HttpClientBuilderCustom();
    }

    // 用于配置ssl
    private SSLs ssls = SSLs.getInstance();

    /**
     * 设置ssl安全链接，采用 默认的 TLSv1.2 协议
     *
     * @return 返回当前对象
     * @throws HttpProcessException http处理异常
     */
    public HttpClientBuilderCustom ssl() throws HttpProcessException {
        // if(isSetPool){//如果已经设置过线程池，那肯定也就是https链接了
        // if(isNewSSL){
        // throw new HttpProcessException("请先设置ssl，后设置pool");
        // }
        // return this;
        // }
        // Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
        // .<ConnectionSocketFactory> create()
        // .register("http", PlainConnectionSocketFactory.INSTANCE)
        // .register("https", ssls.getSSLCONNSF()).build();
        // //设置连接池大小
        // PoolingHttpClientConnectionManager connManager = new
        // PoolingHttpClientConnectionManager(socketFactoryRegistry);
        // return (HttpClientBuilderCustom) this.setConnectionManager(connManager);
        return (HttpClientBuilderCustom) this.setSSLSocketFactory(ssls.getSSLCONNSF(sslpv));
    }

    /**
     * 设置ssl安全链接，采用 指定的 TLSv1.1 协议
     *
     * @return com.sunxung.ecommerce.factoring.util.http.HttpClientBuilderCustom
     * @author ZhouYu Created in 2021/8/27 13:42
     */
    public HttpClientBuilderCustom ssl_TLSv1_1() throws HttpProcessException {
        return (HttpClientBuilderCustom) this.setSSLSocketFactory(ssls.getSSLCONNSF(sslpv1_1));
    }
}
