Skip to content

Commit 7a9ba38

Browse files
authored
增加日志打印 (#133)
1 parent 55e4159 commit 7a9ba38

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
6565
CloseableHttpClient httpClient = builder.build();
6666

6767
// 后面跟使用Apache HttpClient一样
68-
ClosableHttpResponse response = httpClient.execute(...);
68+
CloseableHttpResponse response = httpClient.execute(...);
6969
```
7070

7171
参数说明:

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

+24-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.apache.http.HttpStatus.SC_OK;
66

77
import java.io.IOException;
8+
import java.util.Arrays;
89
import org.apache.http.HttpEntity;
910
import org.apache.http.HttpEntityEnclosingRequest;
1011
import org.apache.http.HttpException;
@@ -16,13 +17,17 @@
1617
import org.apache.http.conn.routing.HttpRoute;
1718
import org.apache.http.entity.BufferedHttpEntity;
1819
import org.apache.http.impl.execchain.ClientExecChain;
20+
import org.apache.http.util.EntityUtils;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
1923

2024
/**
2125
* @author xy-peng
2226
*/
2327
public class SignatureExec implements ClientExecChain {
2428

2529
private static final String WECHAT_PAY_HOST_NAME_SUFFIX = ".mch.weixin.qq.com";
30+
private static final Logger log = LoggerFactory.getLogger(SignatureExec.class);
2631
private final ClientExecChain mainExec;
2732
private final Credentials credentials;
2833
private final Validator validator;
@@ -41,7 +46,7 @@ protected void convertToRepeatableResponseEntity(CloseableHttpResponse response)
4146
}
4247

4348
protected void convertToRepeatableRequestEntity(HttpRequestWrapper request) throws IOException {
44-
if (request instanceof HttpEntityEnclosingRequest) {
49+
if (isEntityEnclosing(request)) {
4550
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
4651
if (entity != null) {
4752
((HttpEntityEnclosingRequest) request).setEntity(new BufferedHttpEntity(entity));
@@ -59,26 +64,41 @@ public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request
5964
}
6065
}
6166

67+
private boolean isEntityEnclosing(HttpRequestWrapper request) {
68+
return request instanceof HttpEntityEnclosingRequest;
69+
}
70+
71+
private boolean isUploadHttpPost(HttpRequestWrapper request) {
72+
return request.getOriginal() instanceof WechatPayUploadHttpPost;
73+
}
74+
6275
private CloseableHttpResponse executeWithSignature(HttpRoute route, HttpRequestWrapper request,
6376
HttpClientContext context,
6477
HttpExecutionAware execAware) throws IOException, HttpException {
6578
// 上传类不需要消耗两次故不做转换
66-
if (!(request.getOriginal() instanceof WechatPayUploadHttpPost)) {
79+
if (!isUploadHttpPost(request)) {
6780
convertToRepeatableRequestEntity(request);
6881
}
6982
// 添加认证信息
7083
request.addHeader(AUTHORIZATION, credentials.getSchema() + " " + credentials.getToken(request));
71-
7284
// 执行
7385
CloseableHttpResponse response = mainExec.execute(route, request, context, execAware);
74-
7586
// 对成功应答验签
7687
StatusLine statusLine = response.getStatusLine();
7788
if (statusLine.getStatusCode() >= SC_OK && statusLine.getStatusCode() < SC_MULTIPLE_CHOICES) {
7889
convertToRepeatableResponseEntity(response);
7990
if (!validator.validate(response)) {
8091
throw new HttpException("应答的微信支付签名验证失败");
8192
}
93+
} else {
94+
// 错误应答需要打日志
95+
log.error("应答的状态码不为200-299。status code[{}]\trequest headers[{}]", statusLine.getStatusCode(),
96+
Arrays.toString(request.getAllHeaders()));
97+
if (isEntityEnclosing(request) && !isUploadHttpPost(request)) {
98+
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
99+
String body = EntityUtils.toString(entity);
100+
log.error("应答的状态码不为200-299。request body[{}]", body);
101+
}
82102
}
83103
return response;
84104
}

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
import java.io.InputStream;
77
import java.net.URI;
88
import java.net.URLConnection;
9+
import org.apache.http.HttpEntity;
910
import org.apache.http.client.methods.HttpPost;
1011
import org.apache.http.entity.ContentType;
1112
import org.apache.http.entity.mime.HttpMultipartMode;
1213
import org.apache.http.entity.mime.MultipartEntityBuilder;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
1316

1417
/**
1518
* @author xy-peng
1619
*/
1720
public class WechatPayUploadHttpPost extends HttpPost {
1821

1922
private final String meta;
23+
private static final Logger log = LoggerFactory.getLogger(WechatPayUploadHttpPost.class);
2024

2125
private WechatPayUploadHttpPost(URI uri, String meta) {
2226
super(uri);
@@ -80,10 +84,12 @@ public WechatPayUploadHttpPost build() {
8084
WechatPayUploadHttpPost request = new WechatPayUploadHttpPost(uri, meta);
8185
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
8286
entityBuilder.setMode(HttpMultipartMode.RFC6532)
83-
.addBinaryBody("file", fileInputStream, fileContentType, fileName)
84-
.addTextBody("meta", meta, APPLICATION_JSON);
85-
request.setEntity(entityBuilder.build());
87+
.addTextBody("meta", meta, APPLICATION_JSON)
88+
.addBinaryBody("file", fileInputStream, fileContentType, fileName);
89+
HttpEntity entity = entityBuilder.build();
90+
request.setEntity(entity);
8691
request.addHeader(ACCEPT, APPLICATION_JSON.toString());
92+
log.debug("request content-type[{}]", entity.getContentType());
8793
return request;
8894
}
8995
}

Diff for: src/main/java/com/wechat/pay/contrib/apache/httpclient/auth/CertificatesVerifier.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
import java.util.List;
1414
import java.util.Map;
1515
import java.util.NoSuchElementException;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1618

1719
/**
1820
* @author xy-peng
1921
*/
2022
public class CertificatesVerifier implements Verifier {
2123

24+
private static final Logger log = LoggerFactory.getLogger(CertificatesVerifier.class);
2225
protected final HashMap<BigInteger, X509Certificate> certificates = new HashMap<>();
2326

2427
public CertificatesVerifier(List<X509Certificate> list) {
@@ -57,7 +60,11 @@ protected boolean verify(X509Certificate certificate, byte[] message, String sig
5760
public boolean verify(String serialNumber, byte[] message, String signature) {
5861
BigInteger val = new BigInteger(serialNumber, 16);
5962
X509Certificate cert = certificates.get(val);
60-
return cert != null && verify(cert, message, signature);
63+
if (cert == null) {
64+
log.error("找不到证书序列号对应的证书,序列号:{}", serialNumber);
65+
return false;
66+
}
67+
return verify(cert, message, signature);
6168
}
6269

6370
@Override

0 commit comments

Comments
 (0)