zhanglin
2023-07-20 cde69bd6e9ce00b22df3690d85ea295459e3b168
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.ruoyi.order.tools.utils;
 
import com.ruoyi.order.tools.auth.AutoUpdateInCloudCertificatesVerifier;
import com.ruoyi.order.tools.auth.CacheService;
import com.ruoyi.order.tools.config.ProfitsSharingConfig;
import com.ruoyi.order.tools.response.AbstractResponse;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
 
 
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.stream.Collectors;
 
@Slf4j
public class HttpUtils {
 
    private final ProfitsSharingConfig config;
    private CloseableHttpClient httpClient;
    private CloseableHttpClient httpClientNotVerify;
    private volatile AutoUpdateInCloudCertificatesVerifier verifier;
 
    public HttpUtils(ProfitsSharingConfig config, CacheService cacheService) {
        this.config = config;
        init(cacheService);
    }
 
    public AutoUpdateInCloudCertificatesVerifier getVerifier() {
        return verifier;
    }
 
    private void init(CacheService cacheService) {
        String mchId = config.getMchId();
        String apiKey = config.getApiKey();
        String mchSerialNo = config.getMchSerialNo();
        PrivateKey privateKey = config.getPrivateKey();
        // 不需要传入微信支付证书,将会自动更新
        verifier = new AutoUpdateInCloudCertificatesVerifier(
                new WechatPay2Credentials(mchId, new PrivateKeySigner(mchSerialNo, privateKey)),
                apiKey.getBytes(StandardCharsets.UTF_8), cacheService);
        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                .withMerchant(mchId, mchSerialNo, privateKey)
                .withValidator(new WechatPay2Validator(verifier));
        httpClient = builder.build();
 
        builder = WechatPayHttpClientBuilder.create()
                .withMerchant(mchId, mchSerialNo, privateKey)
                .withValidator(response -> true);
        httpClientNotVerify = builder.build();
    }
 
    public List<X509Certificate> getLastCertificateList() {
        List<X509Certificate> certificateList = verifier.getLastCertificateList().stream().filter(certificate -> {
            try {
                certificate.checkValidity();
                return true;
            } catch (CertificateExpiredException | CertificateNotYetValidException e) {
                e.printStackTrace();
                return false;
            }
        }).collect(Collectors.toList());
        log.info("可用证书数量:" + certificateList.size());
        return certificateList;
    }
 
    private <T extends AbstractResponse> T request(Class<T> responseClass, HttpUriRequest request) {
        request.addHeader("Content-Type", "application/json");
        request.addHeader("Accept", "application/json");
        try {
            HttpResponse response = httpClient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            String responseText = entity == null ? "{}" : EntityUtils.toString(entity, "utf-8");
            EntityUtils.consume(entity);
            log.info(responseText);
            T responseInstance = JsonUtils.json2Bean(responseClass, responseText);
            responseInstance.setHttpStatusCode(statusCode);
            return responseInstance;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public <T extends AbstractResponse> T get(Class<T> responseClass, String url) {
        return request(responseClass, new HttpGet(url));
    }
 
    public <T extends AbstractResponse> T post(Class<T> responseClass, Object requestObj, String url) {
        HttpPost httpPost = getHttpPost(requestObj, url);
        return request(responseClass, httpPost);
    }
 
    public <T extends AbstractResponse> T post(Class<T> responseClass, Object requestObj, String url, X509Certificate certificate) {
        HttpPost httpPost = getHttpPost(requestObj, url);
        httpPost.addHeader("Wechatpay-Serial", certificate.getSerialNumber().toString(16).toUpperCase());
        return request(responseClass, httpPost);
    }
 
    private HttpPost getHttpPost(Object requestObj, String url) {
        HttpPost httpPost = new HttpPost(url);
        String jsonData = "";
        //增加一个判断是否有Declared方法,要不JsonUtils.bean2Json(requestBody)会报异常
        int methodsNum = requestObj.getClass().getDeclaredMethods().length;
        if (requestObj instanceof String) {
            jsonData = requestObj.toString();
        } else if (methodsNum > 0) {
            jsonData = JsonUtils.bean2Json(requestObj);
        }
        if (jsonData != null && !jsonData.equals("{}")) {
            log.info(jsonData);
            StringEntity reqEntity = new StringEntity(
                    jsonData, ContentType.create("application/json", "utf-8"));
            httpPost.setEntity(reqEntity);
        }
        return httpPost;
    }
 
    public InputStream download(String downloadUrl) {
        HttpGet httpGet = new HttpGet(downloadUrl);
        httpGet.addHeader("Accept", "application/json");
 
        try (CloseableHttpResponse response = httpClientNotVerify.execute(httpGet)) {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                return response.getEntity().getContent();
            } else {
                String body = EntityUtils.toString(response.getEntity());
                log.info(body);
                throw new IOException("request failed");
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
}