Newer
Older
huludao / src / main / java / com / newfiber / api / pc / camera / util / HttpSend.java
package com.newfiber.api.pc.camera.util;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.newfiber.api.core.commons.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

/**
 * @param
 * @author djt
 * @return
 * @creed: Talk is cheap,show me the code
 * @date 2020/12/11 9:32
 * @description: ${description}
 */
@Component
@Slf4j
public class HttpSend {
    @Value("${camera.appSecret}")
    private String appSecret;
    @Value("${camera.appid}")
    private String appid;

    /**
     * @param url 请求地址
     * @param map 参数
     * @return java.util.Optional<com.alibaba.fastjson.JSONObject>
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/11 9:31
     * @description: ${发送post请求}
     */
    public Optional<String> doPost(String url, Map<String, Object> map) {
        String result = null;
        log.info("请求url:{},请求参数:{}", url, JSON.toJSONString(map));
        ProtocolSocketFactory factory = new MySecureProtocolSocketFactory();
        Protocol.registerProtocol("https", new Protocol("https", factory, 443));
        HttpClient client = new HttpClient();
        client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        PostMethod method = new PostMethod(url);
        try {
            RequestEntity entity = new StringRequestEntity(JSON.toJSONString(map), "application/json", "UTF-8");
            method.setRequestEntity(entity);
            client.executeMethod(method);
            InputStream inputStream = method.getResponseBodyAsStream();
            result = IOUtils.toString(inputStream, "UTF-8");
            log.info("返回结果:{}", result);
        } catch (Exception e) {
            log.error("请求错误,错误信息:{}", e.getLocalizedMessage());
            e.getStackTrace();
            throw new CustomException(500,"与平台通讯错误,请重新请求");
        } finally {
            method.releaseConnection();
        }
        return Optional.ofNullable(result);
    }

    /**
     * @param
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/11 9:31
     * @description: ${构建请求固定参数}
     */
    public Map<String, Object> buildSystemParams() {
        long time = System.currentTimeMillis() / 1000;
        String nonce = UUID.randomUUID().toString();
        StringBuilder paramString = new StringBuilder();
        paramString.append("time:").append(time).append(",");
        paramString.append("nonce:").append(nonce).append(",");
        paramString.append("appSecret:").append(appSecret);
        String sign = "";
        // 计算MD5得值
        try {
            log.info("传入参数:{}", paramString.toString().trim());
            sign = DigestUtils.md5Hex(paramString.toString().trim().getBytes("UTF-8"));
        } catch (Exception e) {
            log.error("MD5加密错误:{}", e.getLocalizedMessage());
            throw new CustomException(500,"MD5加密错误,请检查乐橙云平台配置");
        }
        Map<String, Object> map = Maps.newHashMap();
        Map<String, Object> systemMap = ImmutableMap.of("ver", "1.0"
                , "sign", sign
                , "appId", appid
                , "nonce", nonce
                , "time", time);
        map.put("id", UUID.randomUUID().toString());
        map.put("system", systemMap);
        return map;
    }


    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getAppSecret(){
        return this.appSecret;
    }

    public String getAppid(){
        return this.appid;
    }
}