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

top.jfunc.common.http.boot.SmartHttpAutoConfigure Maven / Gradle / Ivy

package top.jfunc.common.http.boot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.jfunc.common.http.base.Config;
import top.jfunc.common.http.base.ProxyInfo;
import top.jfunc.common.http.smart.JoddSmartHttpClient;
import top.jfunc.common.http.smart.SmartHttpClient;

import java.net.InetSocketAddress;
import java.net.Proxy;

/**
 * @author xiongshiyan at 2019/5/10 , contact me with email [email protected] or phone 15208384257
 */
@Configuration
@EnableConfigurationProperties(SmartHttpProperties.class)
public class SmartHttpAutoConfigure {

    @Autowired
    SmartHttpProperties smartHttpProperties;

    @Bean(name = "smartHttpClient")
    @ConditionalOnMissingBean
    public SmartHttpClient smartHttpClient(){
        SmartHttpClient smartHttpClient = new JoddSmartHttpClient();

        Config config = Config.defaultConfig();
        if(null != smartHttpProperties.getBaseUrl()){
            config.setBaseUrl(smartHttpProperties.getBaseUrl());
        }
        config.setDefaultConnectionTimeout(smartHttpProperties.getDefaultConnectionTimeout());
        config.setDefaultReadTimeout(smartHttpProperties.getDefaultReadTimeout());
        config.setDefaultBodyCharset(smartHttpProperties.getDefaultBodyCharset());
        config.setDefaultResultCharset(smartHttpProperties.getDefaultResultCharset());

        if(null != smartHttpProperties.getDefaultHeaders()){
            config.setDefaultHeaders(smartHttpProperties.getDefaultHeaders());
        }
        if(null != smartHttpProperties.getDefaultQueryParams()){
            config.setDefaultQueryParams(smartHttpProperties.getDefaultQueryParams());
        }
        SmartHttpProperties.Proxy propertiesProxy = smartHttpProperties.getProxy();
        if(null != propertiesProxy){

            InetSocketAddress inetSocketAddress = new InetSocketAddress(
                    propertiesProxy.getHostName(), propertiesProxy.getPort());
            Proxy.Type type = Proxy.Type.valueOf(propertiesProxy.getType());

            config.setProxyInfo(ProxyInfo.of(new Proxy(type,inetSocketAddress),
                    propertiesProxy.getUsername() , propertiesProxy.getPassword()));
        }

        smartHttpClient.setConfig(config);

        return smartHttpClient;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy