5
5
import static org .apache .http .HttpStatus .SC_OK ;
6
6
7
7
import java .io .IOException ;
8
+ import java .util .Arrays ;
8
9
import org .apache .http .HttpEntity ;
9
10
import org .apache .http .HttpEntityEnclosingRequest ;
10
11
import org .apache .http .HttpException ;
16
17
import org .apache .http .conn .routing .HttpRoute ;
17
18
import org .apache .http .entity .BufferedHttpEntity ;
18
19
import org .apache .http .impl .execchain .ClientExecChain ;
20
+ import org .apache .http .util .EntityUtils ;
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,7 +46,7 @@ protected void convertToRepeatableResponseEntity(CloseableHttpResponse response)
41
46
}
42
47
43
48
protected void convertToRepeatableRequestEntity (HttpRequestWrapper request ) throws IOException {
44
- if (request instanceof HttpEntityEnclosingRequest ) {
49
+ if (isEntityEnclosing ( request ) ) {
45
50
HttpEntity entity = ((HttpEntityEnclosingRequest ) request ).getEntity ();
46
51
if (entity != null ) {
47
52
((HttpEntityEnclosingRequest ) request ).setEntity (new BufferedHttpEntity (entity ));
@@ -59,26 +64,41 @@ public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request
59
64
}
60
65
}
61
66
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
+
62
75
private CloseableHttpResponse executeWithSignature (HttpRoute route , HttpRequestWrapper request ,
63
76
HttpClientContext context ,
64
77
HttpExecutionAware execAware ) throws IOException , HttpException {
65
78
// 上传类不需要消耗两次故不做转换
66
- if (!(request . getOriginal () instanceof WechatPayUploadHttpPost )) {
79
+ if (!isUploadHttpPost (request )) {
67
80
convertToRepeatableRequestEntity (request );
68
81
}
69
82
// 添加认证信息
70
83
request .addHeader (AUTHORIZATION , credentials .getSchema () + " " + credentials .getToken (request ));
71
-
72
84
// 执行
73
85
CloseableHttpResponse response = mainExec .execute (route , request , context , execAware );
74
-
75
86
// 对成功应答验签
76
87
StatusLine statusLine = response .getStatusLine ();
77
88
if (statusLine .getStatusCode () >= SC_OK && statusLine .getStatusCode () < SC_MULTIPLE_CHOICES ) {
78
89
convertToRepeatableResponseEntity (response );
79
90
if (!validator .validate (response )) {
80
91
throw new HttpException ("应答的微信支付签名验证失败" );
81
92
}
93
+ } else {
94
+ // 错误应答需要打日志
95
+ log .error ("应答的状态码不为200-299。status code[{}]\t request 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
+ }
82
102
}
83
103
return response ;
84
104
}
0 commit comments