|
1 | 1 | package com.wechat.pay.contrib.apache.httpclient; |
2 | 2 |
|
| 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 | + |
| 7 | +import java.io.IOException; |
3 | 8 | import org.apache.http.HttpEntity; |
4 | 9 | import org.apache.http.HttpEntityEnclosingRequest; |
5 | 10 | import org.apache.http.HttpException; |
|
12 | 17 | import org.apache.http.entity.BufferedHttpEntity; |
13 | 18 | import org.apache.http.impl.execchain.ClientExecChain; |
14 | 19 |
|
15 | | -import java.io.IOException; |
16 | | - |
| 20 | +/** |
| 21 | + * @author xy-peng |
| 22 | + */ |
17 | 23 | public class SignatureExec implements ClientExecChain { |
18 | | - final ClientExecChain mainExec; |
19 | | - final Credentials credentials; |
20 | | - final Validator validator; |
21 | 24 |
|
22 | | - SignatureExec(Credentials credentials, Validator validator, ClientExecChain mainExec) { |
23 | | - this.credentials = credentials; |
24 | | - this.validator = validator; |
25 | | - this.mainExec = mainExec; |
26 | | - } |
| 25 | + private static final String WECHAT_PAY_HOST_NAME_SUFFIX = ".mch.weixin.qq.com"; |
| 26 | + private final ClientExecChain mainExec; |
| 27 | + private final Credentials credentials; |
| 28 | + private final Validator validator; |
27 | 29 |
|
28 | | - protected void convertToRepeatableResponseEntity(CloseableHttpResponse response) |
29 | | - throws IOException { |
30 | | - HttpEntity entity = response.getEntity(); |
31 | | - if (entity != null) { |
32 | | - response.setEntity(new BufferedHttpEntity(entity)); |
| 30 | + protected SignatureExec(Credentials credentials, Validator validator, ClientExecChain mainExec) { |
| 31 | + this.credentials = credentials; |
| 32 | + this.validator = validator; |
| 33 | + this.mainExec = mainExec; |
33 | 34 | } |
34 | | - } |
35 | 35 |
|
36 | | - protected void convertToRepeatableRequestEntity(HttpRequestWrapper request) throws IOException { |
37 | | - if (request instanceof HttpEntityEnclosingRequest) { |
38 | | - HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity(); |
39 | | - if (entity != null) { |
40 | | - ((HttpEntityEnclosingRequest) request).setEntity(new BufferedHttpEntity(entity)); |
41 | | - } |
| 36 | + protected void convertToRepeatableResponseEntity(CloseableHttpResponse response) throws IOException { |
| 37 | + HttpEntity entity = response.getEntity(); |
| 38 | + if (entity != null) { |
| 39 | + response.setEntity(new BufferedHttpEntity(entity)); |
| 40 | + } |
42 | 41 | } |
43 | | - } |
44 | 42 |
|
45 | | - @Override |
46 | | - public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, |
47 | | - HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException { |
48 | | - if (request.getTarget().getHostName().endsWith(".mch.weixin.qq.com")) { |
49 | | - return executeWithSignature(route, request, context, execAware); |
50 | | - } else { |
51 | | - return mainExec.execute(route, request, context, execAware); |
| 43 | + 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 | + } |
52 | 50 | } |
53 | | - } |
54 | 51 |
|
55 | | - private CloseableHttpResponse executeWithSignature(HttpRoute route, HttpRequestWrapper request, |
56 | | - HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException { |
57 | | - // 上传类不需要消耗两次故不做转换 |
58 | | - if (!(request.getOriginal() instanceof WechatPayUploadHttpPost)) { |
59 | | - convertToRepeatableRequestEntity(request); |
| 52 | + @Override |
| 53 | + public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext context, |
| 54 | + HttpExecutionAware execAware) throws IOException, HttpException { |
| 55 | + if (request.getTarget().getHostName().endsWith(WECHAT_PAY_HOST_NAME_SUFFIX)) { |
| 56 | + return executeWithSignature(route, request, context, execAware); |
| 57 | + } else { |
| 58 | + return mainExec.execute(route, request, context, execAware); |
| 59 | + } |
60 | 60 | } |
61 | | - // 添加认证信息 |
62 | | - request.addHeader("Authorization", |
63 | | - credentials.getSchema() + " " + credentials.getToken(request)); |
64 | 61 |
|
65 | | - // 执行 |
66 | | - CloseableHttpResponse response = mainExec.execute(route, request, context, execAware); |
| 62 | + private CloseableHttpResponse executeWithSignature(HttpRoute route, HttpRequestWrapper request, |
| 63 | + HttpClientContext context, |
| 64 | + HttpExecutionAware execAware) throws IOException, HttpException { |
| 65 | + // 上传类不需要消耗两次故不做转换 |
| 66 | + if (!(request.getOriginal() instanceof WechatPayUploadHttpPost)) { |
| 67 | + convertToRepeatableRequestEntity(request); |
| 68 | + } |
| 69 | + // 添加认证信息 |
| 70 | + request.addHeader(AUTHORIZATION, credentials.getSchema() + " " + credentials.getToken(request)); |
| 71 | + |
| 72 | + // 执行 |
| 73 | + CloseableHttpResponse response = mainExec.execute(route, request, context, execAware); |
67 | 74 |
|
68 | | - // 对成功应答验签 |
69 | | - StatusLine statusLine = response.getStatusLine(); |
70 | | - if (statusLine.getStatusCode() >= 200 && statusLine.getStatusCode() < 300) { |
71 | | - convertToRepeatableResponseEntity(response); |
72 | | - if (!validator.validate(response)) { |
73 | | - throw new HttpException("应答的微信支付签名验证失败"); |
74 | | - } |
| 75 | + // 对成功应答验签 |
| 76 | + StatusLine statusLine = response.getStatusLine(); |
| 77 | + if (statusLine.getStatusCode() >= SC_OK && statusLine.getStatusCode() < SC_MULTIPLE_CHOICES) { |
| 78 | + convertToRepeatableResponseEntity(response); |
| 79 | + if (!validator.validate(response)) { |
| 80 | + throw new HttpException("应答的微信支付签名验证失败"); |
| 81 | + } |
| 82 | + } |
| 83 | + return response; |
75 | 84 | } |
76 | | - return response; |
77 | | - } |
78 | 85 | } |
0 commit comments