4
4
import static org .apache .http .HttpStatus .SC_MULTIPLE_CHOICES ;
5
5
import static org .apache .http .HttpStatus .SC_OK ;
6
6
7
+ import java .io .ByteArrayOutputStream ;
7
8
import java .io .IOException ;
9
+ import java .util .Arrays ;
8
10
import org .apache .http .HttpEntity ;
9
11
import org .apache .http .HttpEntityEnclosingRequest ;
10
12
import org .apache .http .HttpException ;
16
18
import org .apache .http .conn .routing .HttpRoute ;
17
19
import org .apache .http .entity .BufferedHttpEntity ;
18
20
import org .apache .http .impl .execchain .ClientExecChain ;
21
+ import org .slf4j .Logger ;
22
+ import org .slf4j .LoggerFactory ;
19
23
20
24
/**
21
25
* @author xy-peng
22
26
*/
23
27
public class SignatureExec implements ClientExecChain {
24
28
25
29
private static final String WECHAT_PAY_HOST_NAME_SUFFIX = ".mch.weixin.qq.com" ;
30
+ private static final Logger log = LoggerFactory .getLogger (SignatureExec .class );
26
31
private final ClientExecChain mainExec ;
27
32
private final Credentials credentials ;
28
33
private final Validator validator ;
@@ -41,11 +46,9 @@ protected void convertToRepeatableResponseEntity(CloseableHttpResponse response)
41
46
}
42
47
43
48
protected void convertToRepeatableRequestEntity (HttpRequestWrapper request ) throws IOException {
44
- if (request instanceof HttpEntityEnclosingRequest ) {
45
- HttpEntity entity = ((HttpEntityEnclosingRequest ) request ).getEntity ();
46
- if (entity != null ) {
47
- ((HttpEntityEnclosingRequest ) request ).setEntity (new BufferedHttpEntity (entity ));
48
- }
49
+ HttpEntity entity = getRequestEntity (request );
50
+ if (entity != null ) {
51
+ ((HttpEntityEnclosingRequest ) request ).setEntity (new BufferedHttpEntity (entity ));
49
52
}
50
53
}
51
54
@@ -59,26 +62,45 @@ public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request
59
62
}
60
63
}
61
64
65
+ private HttpEntity getRequestEntity (HttpRequestWrapper request ) {
66
+ HttpEntity entity = null ;
67
+ if (request instanceof HttpEntityEnclosingRequest ) {
68
+ entity = ((HttpEntityEnclosingRequest ) request ).getEntity ();
69
+ }
70
+ return entity ;
71
+ }
72
+
73
+ private boolean isUploadHttpPost (HttpRequestWrapper request ) {
74
+ return request .getOriginal () instanceof WechatPayUploadHttpPost ;
75
+ }
76
+
62
77
private CloseableHttpResponse executeWithSignature (HttpRoute route , HttpRequestWrapper request ,
63
78
HttpClientContext context ,
64
79
HttpExecutionAware execAware ) throws IOException , HttpException {
65
80
// 上传类不需要消耗两次故不做转换
66
- if (!(request . getOriginal () instanceof WechatPayUploadHttpPost )) {
81
+ if (!isUploadHttpPost (request )) {
67
82
convertToRepeatableRequestEntity (request );
68
83
}
69
84
// 添加认证信息
70
85
request .addHeader (AUTHORIZATION , credentials .getSchema () + " " + credentials .getToken (request ));
71
-
72
86
// 执行
73
87
CloseableHttpResponse response = mainExec .execute (route , request , context , execAware );
74
-
75
88
// 对成功应答验签
76
89
StatusLine statusLine = response .getStatusLine ();
77
90
if (statusLine .getStatusCode () >= SC_OK && statusLine .getStatusCode () < SC_MULTIPLE_CHOICES ) {
78
91
convertToRepeatableResponseEntity (response );
79
92
if (!validator .validate (response )) {
80
93
throw new HttpException ("应答的微信支付签名验证失败" );
81
94
}
95
+ } else {
96
+ // 错误应答需要打日志
97
+ log .debug ("request header[{}]" , Arrays .toString (request .getAllHeaders ()));
98
+ HttpEntity entity = getRequestEntity (request );
99
+ if (entity != null && !isUploadHttpPost (request )) {
100
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
101
+ entity .writeTo (outputStream );
102
+ log .debug ("request body[{}]" , outputStream .toString ("UTF-8" ));
103
+ }
82
104
}
83
105
return response ;
84
106
}
0 commit comments