55import static org .apache .http .HttpStatus .SC_OK ;
66
77import java .io .IOException ;
8+ import java .util .Arrays ;
89import org .apache .http .HttpEntity ;
910import org .apache .http .HttpEntityEnclosingRequest ;
1011import org .apache .http .HttpException ;
1617import org .apache .http .conn .routing .HttpRoute ;
1718import org .apache .http .entity .BufferedHttpEntity ;
1819import 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 */
2327public 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[{}]\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+ }
82102 }
83103 return response ;
84104 }
0 commit comments