Skip to content

Commit 67d0ef1

Browse files
常量可读性 & 组织形式优化
1 parent 90a07ca commit 67d0ef1

File tree

9 files changed

+76
-55
lines changed

9 files changed

+76
-55
lines changed

src/main/java/com/wechat/pay/contrib/apache/httpclient/Headers.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main/java/com/wechat/pay/contrib/apache/httpclient/SignatureExec.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.wechat.pay.contrib.apache.httpclient;
22

3+
import static org.apache.http.HttpHeaders.AUTHORIZATION;
4+
import static org.apache.http.HttpStatus.SC_MULTIPLE_CHOICES;
5+
import static org.apache.http.HttpStatus.SC_OK;
6+
37
import java.io.IOException;
48
import org.apache.http.HttpEntity;
59
import org.apache.http.HttpEntityEnclosingRequest;
@@ -13,6 +17,9 @@
1317
import org.apache.http.entity.BufferedHttpEntity;
1418
import org.apache.http.impl.execchain.ClientExecChain;
1519

20+
/**
21+
* @author bruse
22+
*/
1623
public class SignatureExec implements ClientExecChain {
1724

1825
final ClientExecChain mainExec;
@@ -59,14 +66,14 @@ protected CloseableHttpResponse executeWithSignature(HttpRoute route, HttpReques
5966
convertToRepeatableRequestEntity(request);
6067
}
6168
// 添加认证信息
62-
request.addHeader(Headers.AUTHORIZATION, credentials.getSchema() + " " + credentials.getToken(request));
69+
request.addHeader(AUTHORIZATION, credentials.getSchema() + " " + credentials.getToken(request));
6370

6471
// 执行
6572
CloseableHttpResponse response = mainExec.execute(route, request, context, execAware);
6673

6774
// 对成功应答验签
6875
StatusLine statusLine = response.getStatusLine();
69-
if (statusLine.getStatusCode() >= 200 && statusLine.getStatusCode() < 300) {
76+
if (statusLine.getStatusCode() >= SC_OK && statusLine.getStatusCode() < SC_MULTIPLE_CHOICES) {
7077
convertToRepeatableResponseEntity(response);
7178
if (!validator.validate(response)) {
7279
throw new HttpException("应答的微信支付签名验证失败");

src/main/java/com/wechat/pay/contrib/apache/httpclient/WechatPayUploadHttpPost.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.wechat.pay.contrib.apache.httpclient;
22

3+
import static org.apache.http.HttpHeaders.ACCEPT;
4+
import static org.apache.http.entity.ContentType.APPLICATION_JSON;
5+
36
import java.io.InputStream;
47
import java.net.URI;
58
import java.net.URLConnection;
@@ -63,10 +66,10 @@ public WechatPayUploadHttpPost build() {
6366
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
6467
entityBuilder.setMode(HttpMultipartMode.RFC6532)
6568
.addBinaryBody("file", fileInputStream, fileContentType, fileName)
66-
.addTextBody("meta", meta, ContentType.APPLICATION_JSON);
69+
.addTextBody("meta", meta, APPLICATION_JSON);
6770

6871
request.setEntity(entityBuilder.build());
69-
request.addHeader(Headers.ACCEPT, Headers.ACCEPT_APPLICATION_JSON);
72+
request.addHeader(ACCEPT, APPLICATION_JSON.getMimeType());
7073

7174
return request;
7275
}

src/main/java/com/wechat/pay/contrib/apache/httpclient/auth/AutoUpdateCertificatesVerifier.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.wechat.pay.contrib.apache.httpclient.auth;
22

3+
import static org.apache.http.HttpHeaders.ACCEPT;
4+
import static org.apache.http.HttpStatus.SC_OK;
5+
import static org.apache.http.entity.ContentType.APPLICATION_JSON;
6+
37
import com.fasterxml.jackson.databind.JsonNode;
48
import com.fasterxml.jackson.databind.ObjectMapper;
59
import com.wechat.pay.contrib.apache.httpclient.Credentials;
6-
import com.wechat.pay.contrib.apache.httpclient.Headers;
710
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
811
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
912
import java.io.ByteArrayInputStream;
@@ -89,12 +92,12 @@ protected void autoUpdateCert() throws IOException, GeneralSecurityException {
8992
.build()) {
9093

9194
HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH);
92-
httpGet.addHeader(Headers.ACCEPT, Headers.ACCEPT_APPLICATION_JSON);
95+
httpGet.addHeader(ACCEPT, APPLICATION_JSON.getMimeType());
9396

9497
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
9598
int statusCode = response.getStatusLine().getStatusCode();
9699
String body = EntityUtils.toString(response.getEntity());
97-
if (statusCode == 200) {
100+
if (statusCode == SC_OK) {
98101
List<X509Certificate> newCertList = deserializeToCerts(apiV3Key, body);
99102
if (newCertList.isEmpty()) {
100103
log.warn("Cert list is empty");

src/main/java/com/wechat/pay/contrib/apache/httpclient/auth/WechatPay2Validator.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.wechat.pay.contrib.apache.httpclient.auth;
22

3-
import static com.wechat.pay.contrib.apache.httpclient.Headers.REQUEST_ID;
4-
import static com.wechat.pay.contrib.apache.httpclient.Headers.WECHATPAY_NONCE;
5-
import static com.wechat.pay.contrib.apache.httpclient.Headers.WECHATPAY_SERIAL;
6-
import static com.wechat.pay.contrib.apache.httpclient.Headers.WECHATPAY_SIGNATURE;
7-
import static com.wechat.pay.contrib.apache.httpclient.Headers.WECHATPAY_TIMESTAMP;
3+
4+
import static com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders.REQUEST_ID;
5+
import static com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders.WECHAT_PAY_NONCE;
6+
import static com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders.WECHAT_PAY_SERIAL;
7+
import static com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders.WECHAT_PAY_SIGNATURE;
8+
import static com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders.WECHAT_PAY_TIMESTAMP;
89

910
import com.wechat.pay.contrib.apache.httpclient.Validator;
1011
import java.io.IOException;
@@ -43,8 +44,8 @@ public final boolean validate(CloseableHttpResponse response) throws IOException
4344
validateParameters(response);
4445

4546
String message = buildMessage(response);
46-
String serial = response.getFirstHeader(WECHATPAY_SERIAL).getValue();
47-
String signature = response.getFirstHeader(WECHATPAY_SIGNATURE).getValue();
47+
String serial = response.getFirstHeader(WECHAT_PAY_SERIAL).getValue();
48+
String signature = response.getFirstHeader(WECHAT_PAY_SIGNATURE).getValue();
4849

4950
if (!verifier.verify(serial, message.getBytes(StandardCharsets.UTF_8), signature)) {
5051
throw verifyFail("serial=[%s] message=[%s] sign=[%s], request-id=[%s]",
@@ -65,8 +66,8 @@ protected final void validateParameters(CloseableHttpResponse response) {
6566
}
6667
String requestId = firstHeader.getValue();
6768

68-
// NOTE: ensure HEADER_WECHATPAY_TIMESTAMP at last
69-
String[] headers = {WECHATPAY_SERIAL, WECHATPAY_SIGNATURE, WECHATPAY_NONCE, WECHATPAY_TIMESTAMP};
69+
// NOTE: ensure HEADER_WECHAT_PAY_TIMESTAMP at last
70+
String[] headers = {WECHAT_PAY_SERIAL, WECHAT_PAY_SIGNATURE, WECHAT_PAY_NONCE, WECHAT_PAY_TIMESTAMP};
7071

7172
Header header = null;
7273
for (String headerName : headers) {
@@ -90,8 +91,8 @@ protected final void validateParameters(CloseableHttpResponse response) {
9091
}
9192

9293
protected final String buildMessage(CloseableHttpResponse response) throws IOException {
93-
String timestamp = response.getFirstHeader(WECHATPAY_TIMESTAMP).getValue();
94-
String nonce = response.getFirstHeader(WECHATPAY_NONCE).getValue();
94+
String timestamp = response.getFirstHeader(WECHAT_PAY_TIMESTAMP).getValue();
95+
String nonce = response.getFirstHeader(WECHAT_PAY_NONCE).getValue();
9596
String body = getResponseBody(response);
9697
return timestamp + "\n"
9798
+ nonce + "\n"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.wechat.pay.contrib.apache.httpclient.constant;
2+
3+
/**
4+
* 微信支付HTTP请求头相关常量
5+
*
6+
* @author Eric.Lee
7+
* @date 2021/10/29
8+
*/
9+
public final class WechatPayHttpHeaders {
10+
11+
public static final String REQUEST_ID = "Request-ID";
12+
public static final String WECHAT_PAY_SERIAL = "Wechatpay-Serial";
13+
public static final String WECHAT_PAY_SIGNATURE = "Wechatpay-Signature";
14+
public static final String WECHAT_PAY_TIMESTAMP = "Wechatpay-Timestamp";
15+
public static final String WECHAT_PAY_NONCE = "Wechatpay-Nonce";
16+
17+
private WechatPayHttpHeaders() {
18+
// Don't allow instantiation
19+
}
20+
21+
}

src/test/java/com/wechat/pay/contrib/apache/httpclient/AutoUpdateVerifierTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.wechat.pay.contrib.apache.httpclient;
22

3+
import static org.apache.http.HttpStatus.SC_OK;
34
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertTrue;
56

@@ -72,7 +73,7 @@ public void getCertificateTest() throws Exception {
7273
HttpGet httpGet = new HttpGet(uriBuilder.build());
7374
httpGet.addHeader("Accept", "application/json");
7475
CloseableHttpResponse response = httpClient.execute(httpGet);
75-
assertEquals(200, response.getStatusLine().getStatusCode());
76+
assertEquals(SC_OK, response.getStatusLine().getStatusCode());
7677
try {
7778
HttpEntity entity = response.getEntity();
7879
// do something useful with the response body
@@ -98,7 +99,7 @@ public void uploadImageTest() throws Exception {
9899
.build();
99100

100101
try (CloseableHttpResponse response = httpClient.execute(request)) {
101-
assertEquals(200, response.getStatusLine().getStatusCode());
102+
assertEquals(SC_OK, response.getStatusLine().getStatusCode());
102103
HttpEntity entity1 = response.getEntity();
103104
// do something useful with the response body
104105
// and ensure it is fully consumed

src/test/java/com/wechat/pay/contrib/apache/httpclient/HttpClientBuilderTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.wechat.pay.contrib.apache.httpclient;
22

3+
import static org.apache.http.HttpHeaders.ACCEPT;
4+
import static org.apache.http.HttpStatus.SC_OK;
5+
import static org.apache.http.HttpStatus.SC_UNAUTHORIZED;
6+
import static org.apache.http.entity.ContentType.APPLICATION_JSON;
37
import static org.junit.Assert.assertEquals;
48
import static org.junit.Assert.assertTrue;
59

@@ -13,13 +17,11 @@
1317
import java.util.ArrayList;
1418
import java.util.function.Consumer;
1519
import org.apache.http.HttpEntity;
16-
import org.apache.http.HttpHeaders;
1720
import org.apache.http.client.methods.CloseableHttpResponse;
1821
import org.apache.http.client.methods.HttpGet;
1922
import org.apache.http.client.methods.HttpPost;
2023
import org.apache.http.client.methods.HttpUriRequest;
2124
import org.apache.http.client.utils.URIBuilder;
22-
import org.apache.http.entity.ContentType;
2325
import org.apache.http.entity.InputStreamEntity;
2426
import org.apache.http.entity.StringEntity;
2527
import org.apache.http.impl.client.CloseableHttpClient;
@@ -73,7 +75,7 @@ public void getCertificateTest() throws Exception {
7375

7476
HttpGet httpGet = new HttpGet(uriBuilder.build());
7577

76-
doSend(httpGet, null, response -> assertEquals(200, response.getStatusLine().getStatusCode()));
78+
doSend(httpGet, null, response -> assertEquals(SC_OK, response.getStatusLine().getStatusCode()));
7779

7880
}
7981

@@ -96,25 +98,25 @@ public void postNonRepeatableEntityTest() throws IOException {
9698

9799
final byte[] bytes = requestBody.getBytes(StandardCharsets.UTF_8);
98100
final InputStream stream = new ByteArrayInputStream(bytes);
99-
doSend(httpPost, new InputStreamEntity(stream, bytes.length, ContentType.APPLICATION_JSON),
100-
response -> assertTrue(response.getStatusLine().getStatusCode() != 401));
101+
doSend(httpPost, new InputStreamEntity(stream, bytes.length, APPLICATION_JSON),
102+
response -> assertTrue(response.getStatusLine().getStatusCode() != SC_UNAUTHORIZED));
101103
}
102104

103105
@Test
104106
public void postRepeatableEntityTest() throws IOException {
105107
HttpPost httpPost = new HttpPost(
106108
"https://api.mch.weixin.qq.com/v3/marketing/favor/users/oHkLxt_htg84TUEbzvlMwQzVDBqo/coupons");
107109

108-
doSend(httpPost, new StringEntity(requestBody, ContentType.APPLICATION_JSON),
109-
response -> assertTrue(response.getStatusLine().getStatusCode() != 401));
110+
doSend(httpPost, new StringEntity(requestBody, APPLICATION_JSON),
111+
response -> assertTrue(response.getStatusLine().getStatusCode() != SC_UNAUTHORIZED));
110112
}
111113

112114
protected void doSend(HttpUriRequest request, HttpEntity entity, Consumer<CloseableHttpResponse> responseCallback)
113115
throws IOException {
114116
if (entity != null && request instanceof HttpPost) {
115117
((HttpPost) request).setEntity(entity);
116118
}
117-
request.addHeader(HttpHeaders.ACCEPT, Headers.ACCEPT_APPLICATION_JSON);
119+
request.addHeader(ACCEPT, APPLICATION_JSON.getMimeType());
118120

119121
try (CloseableHttpResponse response = httpClient.execute(request)) {
120122
responseCallback.accept(response);

src/test/java/com/wechat/pay/contrib/apache/httpclient/RsaCryptoTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.wechat.pay.contrib.apache.httpclient;
22

3+
import static com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders.WECHAT_PAY_SERIAL;
4+
import static org.apache.http.HttpHeaders.ACCEPT;
5+
import static org.apache.http.HttpStatus.SC_BAD_REQUEST;
6+
import static org.apache.http.HttpStatus.SC_UNAUTHORIZED;
7+
import static org.apache.http.entity.ContentType.APPLICATION_JSON;
38
import static org.junit.Assert.assertTrue;
49

510
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
@@ -14,7 +19,6 @@
1419
import org.apache.http.HttpEntity;
1520
import org.apache.http.client.methods.CloseableHttpResponse;
1621
import org.apache.http.client.methods.HttpPost;
17-
import org.apache.http.entity.ContentType;
1822
import org.apache.http.entity.StringEntity;
1923
import org.apache.http.impl.client.CloseableHttpClient;
2024
import org.apache.http.util.EntityUtils;
@@ -79,14 +83,14 @@ public void postEncryptDataTest() throws Exception {
7983
+ " \"avatar\" : \"logo\",\n"
8084
+ " \"userid\" : \"robert\"\n"
8185
+ "}";
82-
StringEntity reqEntity = new StringEntity(data, ContentType.APPLICATION_JSON);
86+
StringEntity reqEntity = new StringEntity(data, APPLICATION_JSON);
8387
httpPost.setEntity(reqEntity);
84-
httpPost.addHeader(Headers.ACCEPT, Headers.ACCEPT_APPLICATION_JSON);
85-
httpPost.addHeader(Headers.WECHATPAY_SERIAL, "5157F09EFDC096DE15EBE81A47057A7232F1B8E1");
88+
httpPost.addHeader(ACCEPT, APPLICATION_JSON.getMimeType());
89+
httpPost.addHeader(WECHAT_PAY_SERIAL, "5157F09EFDC096DE15EBE81A47057A7232F1B8E1");
8690

8791
CloseableHttpResponse response = httpClient.execute(httpPost);
88-
assertTrue(response.getStatusLine().getStatusCode() != 401);
89-
assertTrue(response.getStatusLine().getStatusCode() != 400);
92+
assertTrue(response.getStatusLine().getStatusCode() != SC_UNAUTHORIZED);
93+
assertTrue(response.getStatusLine().getStatusCode() != SC_BAD_REQUEST);
9094
try {
9195
HttpEntity entity2 = response.getEntity();
9296
// do something useful with the response body

0 commit comments

Comments
 (0)