计算
弹性云服务器 ECS
Flexus云服务
裸金属服务器 BMS
弹性伸缩 AS
镜像服务 IMS
专属主机 DeH
函数工作流 FunctionGraph
云手机服务器 CPH
Huawei Cloud EulerOS
网络
虚拟私有云 VPC
弹性公网IP EIP
虚拟专用网络 VPN
弹性负载均衡 ELB
NAT网关 NAT
云专线 DC
VPC终端节点 VPCEP
云连接 CC
企业路由器 ER
企业交换机 ESW
全球加速 GA
安全与合规
安全技术与应用
Web应用防火墙 WAF
企业主机安全 HSS
云防火墙 CFW
安全云脑 SecMaster
DDoS防护 AAD
数据加密服务 DEW
数据库安全服务 DBSS
云堡垒机 CBH
数据安全中心 DSC
云证书管理服务 CCM
边缘安全 EdgeSec
威胁检测服务 MTD
CDN与智能边缘
内容分发网络 CDN
CloudPond云服务
智能边缘云 IEC
迁移
主机迁移服务 SMS
对象存储迁移服务 OMS
云数据迁移 CDM
迁移中心 MGC
大数据
MapReduce服务 MRS
数据湖探索 DLI
表格存储服务 CloudTable
云搜索服务 CSS
数据接入服务 DIS
数据仓库服务 GaussDB(DWS)
数据治理中心 DataArts Studio
数据可视化 DLV
数据湖工厂 DLF
湖仓构建 LakeFormation
企业应用
云桌面 Workspace
应用与数据集成平台 ROMA Connect
云解析服务 DNS
专属云
专属计算集群 DCC
IoT物联网
IoT物联网
设备接入 IoTDA
智能边缘平台 IEF
用户服务
账号中心
费用中心
成本中心
资源中心
企业管理
工单管理
国际站常见问题
ICP备案
我的凭证
支持计划
客户运营能力
合作伙伴支持计划
专业服务
区块链
区块链服务 BCS
Web3节点引擎服务 NES
解决方案
SAP
高性能计算 HPC
视频
视频直播 Live
视频点播 VOD
媒体处理 MPC
实时音视频 SparkRTC
数字内容生产线 MetaStudio
存储
对象存储服务 OBS
云硬盘 EVS
云备份 CBR
存储容灾服务 SDRS
高性能弹性文件服务 SFS Turbo
弹性文件服务 SFS
云硬盘备份 VBS
云服务器备份 CSBS
数据快递服务 DES
专属分布式存储服务 DSS
容器
云容器引擎 CCE
容器镜像服务 SWR
应用服务网格 ASM
华为云UCS
云容器实例 CCI
管理与监管
云监控服务 CES
统一身份认证服务 IAM
资源编排服务 RFS
云审计服务 CTS
标签管理服务 TMS
云日志服务 LTS
配置审计 Config
资源访问管理 RAM
消息通知服务 SMN
应用运维管理 AOM
应用性能管理 APM
组织 Organizations
优化顾问 OA
IAM 身份中心
云运维中心 COC
资源治理中心 RGC
应用身份管理服务 OneAccess
数据库
云数据库 RDS
文档数据库服务 DDS
数据管理服务 DAS
数据复制服务 DRS
云数据库 GeminiDB
云数据库 GaussDB
分布式数据库中间件 DDM
数据库和应用迁移 UGO
云数据库 TaurusDB
人工智能
人脸识别服务 FRS
图引擎服务 GES
图像识别 Image
内容审核 Moderation
文字识别 OCR
AI开发平台ModelArts
图像搜索 ImageSearch
对话机器人服务 CBS
华为HiLens
视频智能分析服务 VIAS
语音交互服务 SIS
应用中间件
分布式缓存服务 DCS
API网关 APIG
微服务引擎 CSE
分布式消息服务Kafka版
分布式消息服务RabbitMQ版
分布式消息服务RocketMQ版
多活高可用服务 MAS
事件网格 EG
企业协同
华为云会议 Meeting
云通信
消息&短信 MSGSMS
云生态
合作伙伴中心
云商店
开发者工具
SDK开发指南
API签名指南
Terraform
华为云命令行工具服务 KooCLI
其他
产品价格详情
系统权限
管理控制台
客户关联华为云合作伙伴须知
消息中心
公共问题
开发与运维
应用管理与运维平台 ServiceStage
软件开发生产线 CodeArts
需求管理 CodeArts Req
部署 CodeArts Deploy
性能测试 CodeArts PerfTest
编译构建 CodeArts Build
流水线 CodeArts Pipeline
制品仓库 CodeArts Artifact
测试计划 CodeArts TestPlan
代码检查 CodeArts Check
代码托管 CodeArts Repo
云应用引擎 CAE
开天aPaaS
云消息服务 KooMessage
云手机服务 KooPhone
云空间服务 KooDrive
本文导读

展开导读

实现示例

更新时间:2023-10-11 GMT+08:00
说明:

调用时请按照实际的cc-gateway地址修改样例:

  • https://ip:port/agentgateway

    其中,ip为CC-Gateway服务器地址,port为CC-Gateway服务器的HTTPS端口号。

  • WORKNO为座席工号,PASSWORD为座席密码,PHONENUMBER为座席软电话号码。
  • IF_TRUST_ALL为是否信任所有证书,取值范围为false/true,默认为false,需要加载CC-Gateway客户端证书truststore.jks,并配置客户端证书密码TRUSTSTORE_PASSWORD。配置为true有安全风险,请谨慎使用。
  • truststore.jks证书需要跟编译后的MainTest.class放在同一层目录。
package com.huawei.example;

import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j2;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.prng.SP800SecureRandomBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.security.provider.Sun;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.CRL;
import java.security.cert.CertPathBuilder;
import java.security.cert.CertStore;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.CollectionCertStoreParameters;
import java.security.cert.PKIXBuilderParameters;
import java.security.cert.PKIXParameters;
import java.security.cert.PKIXRevocationChecker;
import java.security.cert.X509CertSelector;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.CertPathTrustManagerParameters;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class MainTest {
    private static final String AGENTGATEWAY_URL = "https://ip:port/agentgateway";

    private static final String WORKNO = "100";

    private static final String PASSWORD = "xxxxxxxx";

    private static final String PHONENUMBER = "88880001";

    private static final boolean IF_TRUST_ALL = false;

    private static final String TRUSTSTORE_PASSWORD = "******";

    private static final long MAX_IDLE_TIME = 2L;

    private static final String HTTP = "http";

    private static final String HTTPS = "https";

    private static SSLConnectionSocketFactory sslsf = null;

    private static PoolingHttpClientConnectionManager cm = null;

    private static CloseableHttpClient httpClient = null;

    private static final int HTTPCLIENT_CM_MAXCONNECTION = 200;

    private static final int HTTPCLIENT_CM_MAXPERROUTECONNECTION = 20;

    private static final int CONNECT_TIMEOUT = 10000;

    private static final int CONNECTION_REQUEST_TIMEOUT = 10000;

    private static final int SOCKET_TIMEOUT = 20000;

    private static final int AES_KEY_BIT_LENGTH = 256;

    private static final int ENTROPY_SOURCE_BIT_LENGTH = 384;

    private static final boolean FORCE_RESEED = false;

    private static final boolean PREDICTION_RESISTANT = false;

    private static final FastSecureRandomUtil FAST_SECURE_RANDOM = new FastSecureRandomUtil();

    /**
     * @param args
     */
    public static void main(String[] args) {
        MainTest test = new MainTest();
        Map<String, String> resultMap = test.login(WORKNO, PASSWORD, PHONENUMBER);
        if (resultMap == null) {
            System.out.println("Send http request to agentgateway failed");
            return;
        }
        String guid = resultMap.get("guid");
        String agwResultString = resultMap.get("result");
        HashMap<String, Object> agwResult = JSON.parseObject(agwResultString, HashMap.class);
        if (agwResult == null) {
            System.out.println("Parse json to map failed");
            return;
        }


        if ("0".equals(agwResult.get("retcode"))) {
            //log in successfully
            System.out.println("----login ok");
        } else if ("100-002".equals(agwResult.get("retcode"))) {
            //Agent has logged in, but Agent can forcibly log in
            System.out.println("----has login");
            resultMap = test.forceLogin(WORKNO, PASSWORD, PHONENUMBER);
            if (resultMap == null) {
                System.out.println("Send http request to agentgateway failed");
                return;
            }

            guid = resultMap.get("guid");
            agwResultString = resultMap.get("result");
            agwResult = JSON.parseObject(agwResultString, HashMap.class);
            if (agwResult == null) {
                System.out.println("Parse json to map failed");
                return;
            }

            if ("0".equals(agwResult.get("retcode"))) {
                //forcibly log in successfully
                System.out.println("----forceLogin ok");
            } else {
                System.out.println("----forceLogin failed");
                return;
            }
        } else {
            System.out.println("----login failed");
            return;
        }

        //Need to add guid to http request for Authentication after log in
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("guid", guid);

        //After log in,when agent reset skills,  agent can receive call from customer request
        resultMap = test.resetSkill(WORKNO, true, "", headers);
        if (resultMap == null) {
            System.out.println("Send http request to agentgateway failed");
            return;
        }
        agwResultString = resultMap.get("result");
        agwResult = JSON.parseObject(agwResultString, HashMap.class);
        if (agwResult == null) {
            System.out.println("Parse json to map failed");
            return;
        }

        if ("0".equals(agwResult.get("retcode"))) {
            //Reset skills successfully
            System.out.println("----resetSkill ok");
        } else {
            System.out.println("----resetSkill failed");
            return;
        }

        /**
         *After log in and reset skill successfully. We need to start a thread to get the agent's event by interval.
         */
        Map<String, String> event = null;
        while (true) {
            event = null;
            resultMap = test.getAgentEvent(WORKNO, headers);
            if (resultMap == null) {
                System.out.println("Send http request to agentgateway failed");
                return;
            }

            //if agentgateway uses dynamic authentication mode, the guid will be updated by interval.
            //You can get the guid from the reponse of get the agent's event request
            guid = resultMap.get("guid");
            if (guid != null && guid != "") {
                headers = new HashMap<String, String>();
                headers.put("guid", guid);
            }

            agwResultString = resultMap.get("result");
            agwResult = JSON.parseObject(agwResultString, HashMap.class);
            if (agwResult == null) {
                System.out.println("Parse json to map failed");
                return;
            }

            if ("0".equals(agwResult.get("retcode"))) {
                //Get the agent's event successfully
                event = (Map<String, String>) agwResult.get("event");
                if (event != null) {
                    System.out.println("----getAgentEvent ok:" + agwResultString);
                } else {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        System.out.println("getAgentEvent InterruptedException ");
                    }
                }
            } else if ("000-003".equals(agwResult.get("retcode")) || "100-006".equals(agwResult.get("retcode"))) {
                //No right to visit the interface
                break;
            } else {
                System.out.println("----getAgentEvent failed");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    System.out.println("getAgentEvent InterruptedException ");
                }
            }
        }
    }

    private static TrustManager[] trustManagers = new TrustManager[]{
            new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            }
    };

    static {
        InputStream inStream = null;
        InputStream crlInputStream = null;
        SSLContext context = null;
        try {
            context = SSLContext.getInstance("TLSv1.2");

            if (IF_TRUST_ALL) {
                context.init(null, trustManagers, getSecurityRandomInstance());
            } else {

                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
                inStream = MainTest.class.getResourceAsStream("truststore.jks");

                KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
                keyStore.load(inStream, TRUSTSTORE_PASSWORD.toCharArray());

                String crlFile = "";
                if (!crlFile.isEmpty()) {
                    crlInputStream = new FileInputStream(crlFile);
                }
                loadTrustCertificate(context, certificateFactory, keyStore, crlInputStream);
            }

            sslsf = new SSLConnectionSocketFactory(context, new String[]{"TLSv1.2"}, null,
                    NoopHostnameVerifier.INSTANCE);
            httpClient = createHttpClient();
        } catch (Exception e) {
            System.out.println("init occur Exception: " + e.getMessage());
        } finally {
            IOUtils.closeQuietly(inStream, null);
            IOUtils.closeQuietly(crlInputStream, null);
        }
    }

    public static SecureRandom getSecurityRandomInstance() {
        try {
            SecureRandom secureRandom = FAST_SECURE_RANDOM.getSecureRandom();
            secureRandom.setSeed(secureRandom.generateSeed(64));
            return secureRandom;
        } catch (Exception e) {
            System.out.println("get SecureRandom instance failed" + e);
            throw e;
        }
    }

    private static void loadTrustCertificate(SSLContext context, CertificateFactory certificateFactory,
                                             KeyStore keyStore, InputStream crlInputStream) {
        try {
            Collection<CRL> crls = null;
            if (null != crlInputStream) {
                crls = new HashSet();
                crls.add(certificateFactory.generateCRL(crlInputStream));
            }
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
            PKIXParameters pkixParams = new PKIXBuilderParameters(keyStore, new X509CertSelector());
            if (null != crls) {
                List<CertStore> certStores = new ArrayList();
                certStores.add(CertStore.getInstance("Collection", new CollectionCertStoreParameters(crls)));
                CertPathBuilder certPathBuilder = CertPathBuilder.getInstance("PKIX", new Sun());
                PKIXRevocationChecker revocateChecker = (PKIXRevocationChecker) certPathBuilder.getRevocationChecker();
                revocateChecker.setOptions(EnumSet.of(PKIXRevocationChecker.Option.PREFER_CRLS,
                        PKIXRevocationChecker.Option.NO_FALLBACK));
                pkixParams.setRevocationEnabled(true);
                pkixParams.setCertStores(certStores);
                pkixParams.addCertPathChecker(revocateChecker);
            } else {
                pkixParams.setRevocationEnabled(false);
            }

            tmf.init(new CertPathTrustManagerParameters(pkixParams));
            context.init(null, tmf.getTrustManagers(), getSecurityRandomInstance());
        } catch (Exception e) {
            System.out.println("loadTrustCertificate occur exception: " + e.getMessage());
        }
    }

    /**
     * createHttpClient
     *
     * @return CloseableHttpClient
     * @throws Exception Exception
     */
    private static CloseableHttpClient createHttpClient() {
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register(
                HTTP, new PlainConnectionSocketFactory()).register(HTTPS, sslsf).build();

        cm = new PoolingHttpClientConnectionManager(registry);

        int maxConnect;
        int maxPerRouteConnect;
        String maxConnectNum = "10";
        String maxPreConnectNum = "100";
        try {
            maxConnect = Integer.parseInt(maxConnectNum);
        } catch (NumberFormatException e) {
            maxConnect = HTTPCLIENT_CM_MAXCONNECTION;
        }

        try {
            maxPerRouteConnect = Integer.parseInt(maxPreConnectNum);
        } catch (NumberFormatException e) {
            maxPerRouteConnect = HTTPCLIENT_CM_MAXPERROUTECONNECTION;
        }

        cm.setMaxTotal(maxConnect);
        cm.setDefaultMaxPerRoute(maxPerRouteConnect);

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(CONNECT_TIMEOUT)
                .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
                .setSocketTimeout(SOCKET_TIMEOUT)
                .build();

        httpClient = HttpClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .evictExpiredConnections()
                .evictIdleConnections(MAX_IDLE_TIME, TimeUnit.SECONDS)
                .disableAutomaticRetries()
                .setConnectionManager(cm)
                .build();
        return httpClient;
    }

    /**
     * log in
     *
     * @param workNo      the work no of the agent
     * @param password    the password of the agent
     * @param phoneNumber the phone number of the agent
     * @return
     */
    public Map<String, String> login(String workNo, String password, String phoneNumber) {
        String loginUrl = AGENTGATEWAY_URL + "/resource/onlineagent/" + workNo;
        Map<String, Object> loginParam = new HashMap<String, Object>();
        loginParam.put("password", password);
        loginParam.put("phonenum", phoneNumber);
        return put(loginUrl, loginParam, null);
    }

    /**
     * When agent has logged in, call the interface to forcibly log in
     *
     * @param workNo      the work no of the agent
     * @param password    the password of the agent
     * @param phoneNumber the phone number of the agent
     * @return
     */
    public Map<String, String> forceLogin(String workNo, String password, String phoneNumber) {
        String loginUrl = AGENTGATEWAY_URL + "/resource/onlineagent/" + workNo + "/forcelogin";
        Map<String, Object> loginParam = new HashMap<String, Object>();
        loginParam.put("password", password);
        loginParam.put("phonenum", phoneNumber);
        return put(loginUrl, loginParam, null);
    }

    /**
     * After log in, reset the skills
     *
     * @param workNo   the work no of the agent
     * @param autoFlag Is automatically signed into the skill queue
     * @param skillId  the id of the skill. if has more than one skill that need to be sign, it is split by ;
     * @param headers  the field is used to set the header of http request
     * @return
     */
    public Map<String, String> resetSkill(String workNo, boolean autoFlag,
                                          String skillId, Map<String, String> headers) {
        String url = AGENTGATEWAY_URL + "/resource/onlineagent/" + workNo + "/resetskill/" + autoFlag;
        if (skillId != null && skillId != "") {
            url = url + "?skillid=" + skillId;
        }
        return post(url, null, headers);
    }

    /**
     * Get the agent's event
     *
     * @param workNo  workNo the work no of the agent
     * @param headers the field is used to set the header of http request
     * @return
     */
    public Map<String, String> getAgentEvent(String workNo, Map<String, String> headers) {
        String url = AGENTGATEWAY_URL + "/resource/agentevent/" + workNo;
        return get(url, headers);
    }

    /**
     * Send http's PUT request
     *
     * @param url          the address of the request
     * @param entityParams the paramters of entity
     * @param headers      the field is used to set the header of http request
     * @return
     */
    public Map<String, String> put(String url, Map<String, Object> entityParams, Map<String, String> headers) {
        CloseableHttpClient httpClient = null;
        Map<String, String> resultMap = null;
        try {
            httpClient = createHttpClient();

            HttpPut httpPut = new HttpPut(url);
            if (entityParams != null) {
                String jsonString = JSON.toJSONString(entityParams);
                HttpEntity entity = new StringEntity(jsonString);
                httpPut.setEntity(entity);

            }

            if (headers != null) {
                Set<Entry<String, String>> headersSet = headers.entrySet();
                for (Entry<String, String> entry : headersSet) {
                    httpPut.setHeader(entry.getKey(), entry.getValue());
                }
            }
            httpPut.setHeader("Content-Type", "application/json");
            HttpResponse response = httpClient.execute(httpPut);
            int returnCode = response.getStatusLine().getStatusCode();
            if (returnCode == HttpStatus.SC_OK) {
                InputStream is = response.getEntity().getContent();
                BufferedReader in = new BufferedReader(new InputStreamReader(is));
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while ((line = in.readLine()) != null) {
                    buffer.append(line);
                }

                Header[] allHeaders = response.getAllHeaders();
                String guid = "";
                if (allHeaders != null && allHeaders.length > 0) {
                    for (Header header : allHeaders) {
                        if (header.getName().equals("Set-GUID")) {
                            String setGuid = header.getValue();
                            if (setGuid != null) {
                                guid = setGuid.replace("JSESSIONID=", "");
                            }
                            break;
                        }
                    }
                }
                resultMap = new HashMap<String, String>();
                resultMap.put("guid", guid);
                resultMap.put("result", buffer.toString());
            } else {
                System.out.println(returnCode);
            }
            return resultMap;
        } catch (ClientProtocolException e) {
            System.out.println("HttpPut ClientProtocolException ");
        } catch (IOException e) {
            System.out.println("HttpPut IOException ");
        } catch (Exception e) {
            System.out.println("HttpPut Exception ");
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return resultMap;
    }


    /**
     * Send http's POST request
     *
     * @param url          the address of the request
     * @param entityParams the paramters of entity
     * @param headers      the field is used to set the header of http request
     * @return
     */
    public Map<String, String> post(String url, Map<String, Object> entityParams, Map<String, String> headers) {
        CloseableHttpClient httpClient = null;
        Map<String, String> resultMap = null;
        try {
            httpClient = createHttpClient();

            HttpPost httpPost = new HttpPost(url);
            if (entityParams != null) {
                String jsonString = JSON.toJSONString(entityParams);
                HttpEntity entity = new StringEntity(jsonString);
                httpPost.setEntity(entity);

            }

            if (headers != null) {
                Set<Entry<String, String>> headersSet = headers.entrySet();
                for (Entry<String, String> entry : headersSet) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            httpPost.setHeader("Content-Type", "application/json");
            HttpResponse response = httpClient.execute(httpPost);
            int returnCode = response.getStatusLine().getStatusCode();
            if (returnCode == HttpStatus.SC_OK) {
                InputStream is = response.getEntity().getContent();
                BufferedReader in = new BufferedReader(new InputStreamReader(is));
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while ((line = in.readLine()) != null) {
                    buffer.append(line);
                }
                resultMap = new HashMap<String, String>();
                resultMap.put("result", buffer.toString());
            } else {
                System.out.println(returnCode);
            }
            return resultMap;
        } catch (ClientProtocolException e) {
            System.out.println("HttpPost ClientProtocolException");
        } catch (IOException e) {
            System.out.println("HttpPost IOException");
        } catch (Exception e) {
            System.out.println("HttpPost Exception");
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return resultMap;
    }


    /**
     * Send http's GET request
     *
     * @param url     the address of the request
     * @param headers the field is used to set the header of http request
     * @return
     */
    public Map<String, String> get(String url, Map<String, String> headers) {
        CloseableHttpClient httpClient = null;
        Map<String, String> resultMap = null;
        try {
            httpClient = createHttpClient();

            HttpGet httpGet = new HttpGet(url);
            if (headers != null) {
                Set<Entry<String, String>> headersSet = headers.entrySet();
                for (Entry<String, String> entry : headersSet) {
                    httpGet.setHeader(entry.getKey(), entry.getValue());
                }
            }
            httpGet.setHeader("Content-Type", "application/json");
            HttpResponse response = httpClient.execute(httpGet);
            int returnCode = response.getStatusLine().getStatusCode();
            if (returnCode == HttpStatus.SC_OK) {
                InputStream is = response.getEntity().getContent();
                BufferedReader in = new BufferedReader(new InputStreamReader(is));
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while ((line = in.readLine()) != null) {
                    buffer.append(line);
                }

                Header[] allHeaders = response.getAllHeaders();
                String guid = "";
                if (allHeaders != null && allHeaders.length > 0) {
                    for (Header header : allHeaders) {
                        if (header.getName().equals("Set-GUID")) {
                            String setGuid = header.getValue();
                            if (setGuid != null) {
                                guid = setGuid.replace("JSESSIONID=", "");
                            }
                            break;
                        }
                    }
                }
                resultMap = new HashMap<String, String>();
                resultMap.put("guid", guid);
                resultMap.put("result", buffer.toString());
            } else {
                System.out.println(returnCode);
            }
            return resultMap;
        } catch (ClientProtocolException e) {
            System.out.println("HttpGet ClientProtocolException");
        } catch (IOException e) {
            System.out.println("HttpGet IOException");
        } catch (Exception e) {
            System.out.println("HttpGet Exception");
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return resultMap;
    }
}

package com.huawei.example;

import lombok.extern.log4j.Log4j2;

import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.prng.SP800SecureRandomBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;

@Log4j2
public class FastSecureRandomUtil {
    private static final int AES_KEY_BIT_LENGTH = 256;

    private static final int ENTROPY_SOURCE_BIT_LENGTH = 384;

    private static final boolean FORCE_RESEED = false;

    private static final boolean PREDICTION_RESISTANT = false;

    static {
        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
            Security.addProvider(new BouncyCastleProvider());
        }
    }

    private final SecureRandom fastRandom;

    public FastSecureRandomUtil() throws Exception {
        SecureRandom source = null;
        try {
            source = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException e) {
            log.error("FastSecureRandom NoSuchAlgorithmException: {}.", e.getMessage());
            throw new Exception("FastSecureRandom NoSuchAlgorithmException.", e);
        }

        BlockCipher cipher = new AESEngine();

        byte[] nonce = new byte[ENTROPY_SOURCE_BIT_LENGTH / Byte.SIZE];
        source.nextBytes(nonce);

        fastRandom = new SP800SecureRandomBuilder(source, PREDICTION_RESISTANT).setEntropyBitsRequired(
                ENTROPY_SOURCE_BIT_LENGTH).buildCTR(cipher, AES_KEY_BIT_LENGTH, nonce, FORCE_RESEED);
    }

    public byte[] getRandomBytes(int byteSize) {
        byte[] randomBytes = new byte[byteSize];
        fastRandom.nextBytes(randomBytes);
        return randomBytes;
    }


    public SecureRandom getSecureRandom() {
        return fastRandom;
    }
}

我们使用cookie来确保您的高速浏览体验。继续浏览本站,即表示您同意我们使用cookie。 详情

文档反馈

文档反馈

意见反馈

0/500

标记内容

同时提交标记内容