mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-07 22:27:47 +08:00
🎨 #3128 【微信支付】提供扩展httpclientbuilder的能力
This commit is contained in:
parent
52c597a51a
commit
04bb23b75c
@ -0,0 +1,11 @@
|
|||||||
|
package com.github.binarywang.wxpay.config;
|
||||||
|
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="https://github.com/ifcute">dagewang</a>
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface HttpClientBuilderCustomizer {
|
||||||
|
void customize(HttpClientBuilder var1);
|
||||||
|
}
|
@ -27,6 +27,7 @@ import java.security.KeyStore;
|
|||||||
import java.security.PrivateKey;
|
import java.security.PrivateKey;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信支付配置
|
* 微信支付配置
|
||||||
@ -165,6 +166,11 @@ public class WxPayConfig {
|
|||||||
|
|
||||||
|
|
||||||
private CloseableHttpClient apiV3HttpClient;
|
private CloseableHttpClient apiV3HttpClient;
|
||||||
|
/**
|
||||||
|
* 支持扩展httpClientBuilder
|
||||||
|
*/
|
||||||
|
private HttpClientBuilderCustomizer httpClientBuilderCustomizer;
|
||||||
|
private HttpClientBuilderCustomizer apiV3HttpClientBuilderCustomizer;
|
||||||
/**
|
/**
|
||||||
* 私钥信息
|
* 私钥信息
|
||||||
*/
|
*/
|
||||||
@ -283,6 +289,10 @@ public class WxPayConfig {
|
|||||||
//初始化V3接口正向代理设置
|
//初始化V3接口正向代理设置
|
||||||
HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder, wxPayHttpProxy);
|
HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder, wxPayHttpProxy);
|
||||||
|
|
||||||
|
// 提供自定义wxPayV3HttpClientBuilder的能力
|
||||||
|
Optional.ofNullable(apiV3HttpClientBuilderCustomizer).ifPresent(e -> {
|
||||||
|
e.customize(wxPayV3HttpClientBuilder);
|
||||||
|
});
|
||||||
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
|
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
|
||||||
|
|
||||||
this.apiV3HttpClient = httpClient;
|
this.apiV3HttpClient = httpClient;
|
||||||
|
@ -28,6 +28,7 @@ import java.io.InputStream;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
@ -335,6 +336,12 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
httpClientBuilder.setDefaultCredentialsProvider(provider)
|
httpClientBuilder.setDefaultCredentialsProvider(provider)
|
||||||
.setProxy(new HttpHost(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()));
|
.setProxy(new HttpHost(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 提供自定义httpClientBuilder的能力
|
||||||
|
Optional.ofNullable(getConfig().getHttpClientBuilderCustomizer()).ifPresent(e -> {
|
||||||
|
e.customize(httpClientBuilder);
|
||||||
|
});
|
||||||
|
|
||||||
return httpClientBuilder;
|
return httpClientBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.github.binarywang.wxpay.config;
|
||||||
|
|
||||||
|
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||||
|
import com.github.binarywang.wxpay.service.WxPayService;
|
||||||
|
import com.github.binarywang.wxpay.testbase.CustomizedApiTestModule;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.testng.annotations.Guice;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="https://github.com/ifcute">dagewang</a>
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Test
|
||||||
|
@Guice(modules = CustomizedApiTestModule.class)
|
||||||
|
public class CustomizedWxPayConfigTest {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private WxPayService wxPayService;
|
||||||
|
|
||||||
|
public void testCustomizerHttpClient() {
|
||||||
|
try {
|
||||||
|
wxPayService.queryOrder("a", null);
|
||||||
|
} catch (WxPayException e) {
|
||||||
|
// ignore
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCustomizerV3HttpClient() {
|
||||||
|
try {
|
||||||
|
wxPayService.queryOrderV3("a", null);
|
||||||
|
} catch (WxPayException e) {
|
||||||
|
// ignore
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.github.binarywang.wxpay.testbase;
|
||||||
|
|
||||||
|
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||||
|
import com.github.binarywang.wxpay.service.WxPayService;
|
||||||
|
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
|
||||||
|
import com.google.inject.Binder;
|
||||||
|
import com.google.inject.Module;
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||||
|
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type Api test module.
|
||||||
|
*/
|
||||||
|
public class CustomizedApiTestModule implements Module {
|
||||||
|
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||||
|
private static final String TEST_CONFIG_XML = "test-config.xml";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(Binder binder) {
|
||||||
|
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) {
|
||||||
|
if (inputStream == null) {
|
||||||
|
throw new WxRuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成");
|
||||||
|
}
|
||||||
|
|
||||||
|
XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, inputStream);
|
||||||
|
config.setIfSaveApiData(true);
|
||||||
|
|
||||||
|
config.setApiV3HttpClientBuilderCustomizer((builder) -> {
|
||||||
|
builder.addInterceptorLast((HttpRequestInterceptor) (r, c) -> System.out.println("--------> V3 HttpRequestInterceptor ..."));
|
||||||
|
});
|
||||||
|
|
||||||
|
config.setHttpClientBuilderCustomizer((builder) -> {
|
||||||
|
builder.addInterceptorLast((HttpRequestInterceptor) (r, c) -> System.out.println("--------> HttpRequestInterceptor ..."));
|
||||||
|
});
|
||||||
|
|
||||||
|
WxPayService wxService = new WxPayServiceImpl();
|
||||||
|
wxService.setConfig(config);
|
||||||
|
|
||||||
|
binder.bind(WxPayService.class).toInstance(wxService);
|
||||||
|
binder.bind(WxPayConfig.class).toInstance(config);
|
||||||
|
} catch (IOException e) {
|
||||||
|
this.log.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private <T> T fromXml(Class<T> clazz, InputStream is) {
|
||||||
|
XStream xstream = XStreamInitializer.getInstance();
|
||||||
|
xstream.alias("xml", clazz);
|
||||||
|
xstream.processAnnotations(clazz);
|
||||||
|
return (T) xstream.fromXML(is);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user