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

com.dahuatech.icc.oauth.handle.LoadHttpConfigInfo Maven / Gradle / Ivy

There is a newer version: 1.0.13.7
Show newest version
package com.dahuatech.icc.oauth.handle;

import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigBaseInfo;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigClientInfo;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigUserPwdInfo;
import com.dahuatech.icc.oauth.profile.GrantType;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class LoadHttpConfigInfo {

    private final Log logger = LogFactory.get();
    private volatile Properties configuration = new Properties();
    private OauthConfigBaseInfo oauthConfigBaseInfo;
    public synchronized void loadIccSdkProperties() {
        // 根路径为classpath
        if(oauthConfigBaseInfo != null){
            return;
        }
        InputStream is = this.getClass().getResourceAsStream("/config/iccSdk.properties");
        if (is != null) {
            try {
                this.configuration.clear();
                this.configuration.load(is);
                String host = configuration.getProperty("icc.sdk.host");
                String httpPort = "83";
                String httpsPort = "443";
                boolean enableHttps = Boolean.parseBoolean(configuration.getProperty("icc.sdk.enable.https","true"));
                if(enableHttps){
                    if(host.contains(":")){
                        httpsPort = host.split(":")[1];
                        host = host.split(":")[0];
                    }
                }else{
                    if(host.contains(":")){
                        httpPort = host.split(":")[1];
                        host = host.split(":")[0];
                    }
                }
                if(GrantType.password.name().equals(configuration.getProperty("icc.sdk.grantType"))){//密码模式
                    String pwdClientId = configuration.getProperty("icc.sdk.pwdClientId");
                    String pwdClientSecret = configuration.getProperty("icc.sdk.pwdClientSecret");
                    String username = configuration.getProperty("icc.sdk.username");
                    String password = configuration.getProperty("icc.sdk.password");
                    oauthConfigBaseInfo = new OauthConfigUserPwdInfo(host,pwdClientId,pwdClientSecret,username,password,!enableHttps,httpsPort,httpPort);
                }else{//客户端模式
                    String clientId = configuration.getProperty("icc.sdk.clientId");
                    String clientSecret = configuration.getProperty("icc.sdk.clientSecret");
                    String userId = configuration.getProperty("icc.sdk.config.client.userId","1");
                    oauthConfigBaseInfo = new OauthConfigClientInfo(host,clientId,clientSecret,userId,!enableHttps,httpsPort,httpPort);
                }
                InitVersionProcessor.getInstance().initSystemVersion(oauthConfigBaseInfo.getHttpConfigInfo());
                logger.info("加载配置文件完成");
            } catch (IOException | ClientException e) {
            } finally {
                try {
                    is.close();
                } catch (Throwable t) {
                }
            }
        }else{
            logger.info("can not load [classpath:resources/config/iccSdk.properties] , use DefaultClient constructor instead ");
        }
    }

    public OauthConfigBaseInfo getOauthConfigBaseInfo() {
        return oauthConfigBaseInfo;
    }

    public void setOauthConfigBaseInfo(OauthConfigBaseInfo oauthConfigBaseInfo) {
        this.oauthConfigBaseInfo = oauthConfigBaseInfo;
    }

    public static synchronized LoadHttpConfigInfo getInstance() {
        return LoadHttpConfigInfo.SingleHolder.INSTANCE;
    }

    private static class SingleHolder{
        public final static LoadHttpConfigInfo INSTANCE = new LoadHttpConfigInfo();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy