Skip to content

Commit 2e53205

Browse files
ifcutezhongjun96
authored andcommitted
🎨 binarywang#3128 【微信支付】提供扩展httpclientbuilder的能力
1 parent 85eabc6 commit 2e53205

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.binarywang.wxpay.config;
2+
3+
import org.apache.http.impl.client.HttpClientBuilder;
4+
5+
/**
6+
* @author <a href="https://github.com/ifcute">dagewang</a>
7+
*/
8+
@FunctionalInterface
9+
public interface HttpClientBuilderCustomizer {
10+
void customize(HttpClientBuilder var1);
11+
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.security.PrivateKey;
2828
import java.security.cert.X509Certificate;
2929
import java.util.Base64;
30+
import java.util.Optional;
3031

3132
/**
3233
* 微信支付配置
@@ -165,6 +166,11 @@ public class WxPayConfig {
165166

166167

167168
private CloseableHttpClient apiV3HttpClient;
169+
/**
170+
* 支持扩展httpClientBuilder
171+
*/
172+
private HttpClientBuilderCustomizer httpClientBuilderCustomizer;
173+
private HttpClientBuilderCustomizer apiV3HttpClientBuilderCustomizer;
168174
/**
169175
* 私钥信息
170176
*/
@@ -283,6 +289,10 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
283289
//初始化V3接口正向代理设置
284290
HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder, wxPayHttpProxy);
285291

292+
// 提供自定义wxPayV3HttpClientBuilder的能力
293+
Optional.ofNullable(apiV3HttpClientBuilderCustomizer).ifPresent(e -> {
294+
e.customize(wxPayV3HttpClientBuilder);
295+
});
286296
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
287297

288298
this.apiV3HttpClient = httpClient;

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.charset.StandardCharsets;
2929
import java.util.Base64;
3030
import java.util.Objects;
31+
import java.util.Optional;
3132

3233
/**
3334
* <pre>
@@ -335,6 +336,12 @@ private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayEx
335336
httpClientBuilder.setDefaultCredentialsProvider(provider)
336337
.setProxy(new HttpHost(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()));
337338
}
339+
340+
// 提供自定义httpClientBuilder的能力
341+
Optional.ofNullable(getConfig().getHttpClientBuilderCustomizer()).ifPresent(e -> {
342+
e.customize(httpClientBuilder);
343+
});
344+
338345
return httpClientBuilder;
339346
}
340347

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.binarywang.wxpay.config;
2+
3+
import com.github.binarywang.wxpay.exception.WxPayException;
4+
import com.github.binarywang.wxpay.service.WxPayService;
5+
import com.github.binarywang.wxpay.testbase.CustomizedApiTestModule;
6+
import com.google.inject.Inject;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.testng.annotations.Guice;
9+
import org.testng.annotations.Test;
10+
11+
/**
12+
* @author <a href="https://github.com/ifcute">dagewang</a>
13+
*/
14+
@Slf4j
15+
@Test
16+
@Guice(modules = CustomizedApiTestModule.class)
17+
public class CustomizedWxPayConfigTest {
18+
19+
@Inject
20+
private WxPayService wxPayService;
21+
22+
public void testCustomizerHttpClient() {
23+
try {
24+
wxPayService.queryOrder("a", null);
25+
} catch (WxPayException e) {
26+
// ignore
27+
e.printStackTrace();
28+
}
29+
}
30+
31+
public void testCustomizerV3HttpClient() {
32+
try {
33+
wxPayService.queryOrderV3("a", null);
34+
} catch (WxPayException e) {
35+
// ignore
36+
e.printStackTrace();
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.binarywang.wxpay.testbase;
2+
3+
import com.github.binarywang.wxpay.config.WxPayConfig;
4+
import com.github.binarywang.wxpay.service.WxPayService;
5+
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
6+
import com.google.inject.Binder;
7+
import com.google.inject.Module;
8+
import com.thoughtworks.xstream.XStream;
9+
import me.chanjar.weixin.common.error.WxRuntimeException;
10+
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
11+
import org.apache.http.HttpRequestInterceptor;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
18+
/**
19+
* The type Api test module.
20+
*/
21+
public class CustomizedApiTestModule implements Module {
22+
private final Logger log = LoggerFactory.getLogger(this.getClass());
23+
private static final String TEST_CONFIG_XML = "test-config.xml";
24+
25+
@Override
26+
public void configure(Binder binder) {
27+
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) {
28+
if (inputStream == null) {
29+
throw new WxRuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成");
30+
}
31+
32+
XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, inputStream);
33+
config.setIfSaveApiData(true);
34+
35+
config.setApiV3HttpClientBuilderCustomizer((builder) -> {
36+
builder.addInterceptorLast((HttpRequestInterceptor) (r, c) -> System.out.println("--------> V3 HttpRequestInterceptor ..."));
37+
});
38+
39+
config.setHttpClientBuilderCustomizer((builder) -> {
40+
builder.addInterceptorLast((HttpRequestInterceptor) (r, c) -> System.out.println("--------> HttpRequestInterceptor ..."));
41+
});
42+
43+
WxPayService wxService = new WxPayServiceImpl();
44+
wxService.setConfig(config);
45+
46+
binder.bind(WxPayService.class).toInstance(wxService);
47+
binder.bind(WxPayConfig.class).toInstance(config);
48+
} catch (IOException e) {
49+
this.log.error(e.getMessage(), e);
50+
}
51+
}
52+
53+
@SuppressWarnings("unchecked")
54+
private <T> T fromXml(Class<T> clazz, InputStream is) {
55+
XStream xstream = XStreamInitializer.getInstance();
56+
xstream.alias("xml", clazz);
57+
xstream.processAnnotations(clazz);
58+
return (T) xstream.fromXML(is);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)