mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-27 20:14:52 +08:00
🎨 DefaultApacheHttpClientBuilder 允许添加自定义拦截器
This commit is contained in:
@ -4,6 +4,8 @@ import lombok.Data;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.apache.http.HttpResponseInterceptor;
|
||||||
import org.apache.http.auth.AuthScope;
|
import org.apache.http.auth.AuthScope;
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
import org.apache.http.client.CredentialsProvider;
|
import org.apache.http.client.CredentialsProvider;
|
||||||
@ -34,6 +36,8 @@ import java.security.KeyStoreException;
|
|||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
@ -93,6 +97,16 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
|
|||||||
*/
|
*/
|
||||||
private String userAgent;
|
private String userAgent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义请求拦截器
|
||||||
|
*/
|
||||||
|
private List<HttpRequestInterceptor> requestInterceptors = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义响应拦截器
|
||||||
|
*/
|
||||||
|
private List<HttpResponseInterceptor> responseInterceptors = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义重试策略
|
* 自定义重试策略
|
||||||
*/
|
*/
|
||||||
@ -229,6 +243,12 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
|
|||||||
httpClientBuilder.setUserAgent(this.userAgent);
|
httpClientBuilder.setUserAgent(this.userAgent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//添加自定义的请求拦截器
|
||||||
|
requestInterceptors.forEach(httpClientBuilder::addInterceptorFirst);
|
||||||
|
|
||||||
|
//添加自定义的响应拦截器
|
||||||
|
responseInterceptors.forEach(httpClientBuilder::addInterceptorLast);
|
||||||
|
|
||||||
this.closeableHttpClient = httpClientBuilder.build();
|
this.closeableHttpClient = httpClientBuilder.build();
|
||||||
prepared.set(true);
|
prepared.set(true);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,24 @@
|
|||||||
package me.chanjar.weixin.common.util.http.apache;
|
package me.chanjar.weixin.common.util.http.apache;
|
||||||
|
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.apache.http.HttpResponseInterceptor;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
|
import org.apache.http.client.protocol.HttpClientContext;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.apache.http.protocol.HttpCoreContext;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class DefaultApacheHttpClientBuilderTest {
|
public class DefaultApacheHttpClientBuilderTest {
|
||||||
@Test
|
@Test
|
||||||
@ -38,6 +48,47 @@ public class DefaultApacheHttpClientBuilderTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHttpClientWithInterceptor() throws Exception {
|
||||||
|
DefaultApacheHttpClientBuilder builder = DefaultApacheHttpClientBuilder.get();
|
||||||
|
|
||||||
|
|
||||||
|
List<String> interceptorOrder = new ArrayList<>();
|
||||||
|
|
||||||
|
HttpRequestInterceptor requestInterceptor1 = (request, context) -> {
|
||||||
|
context.setAttribute("interceptor_called", "requestInterceptor1");
|
||||||
|
interceptorOrder.add("requestInterceptor1");
|
||||||
|
};
|
||||||
|
|
||||||
|
HttpRequestInterceptor requestInterceptor2 = (request, context) -> {
|
||||||
|
interceptorOrder.add("requestInterceptor2");
|
||||||
|
};
|
||||||
|
|
||||||
|
HttpResponseInterceptor responseInterceptor1 = (response, context) -> {
|
||||||
|
interceptorOrder.add("responseInterceptor1");
|
||||||
|
};
|
||||||
|
|
||||||
|
HttpResponseInterceptor responseInterceptor2 = (response, context) -> {
|
||||||
|
interceptorOrder.add("responseInterceptor2");
|
||||||
|
};
|
||||||
|
|
||||||
|
builder.setRequestInterceptors(Stream.of(requestInterceptor1, requestInterceptor2).collect(Collectors.toList()));
|
||||||
|
builder.setResponseInterceptors(Stream.of(responseInterceptor1, responseInterceptor2).collect(Collectors.toList()));
|
||||||
|
|
||||||
|
try (CloseableHttpClient client = builder.build()) {
|
||||||
|
HttpUriRequest request = new HttpGet("http://localhost:8080");
|
||||||
|
HttpContext context = HttpClientContext.create();
|
||||||
|
try (CloseableHttpResponse resp = client.execute(request, context)) {
|
||||||
|
Assert.assertEquals("requestInterceptor1", context.getAttribute("interceptor_called"), "成功调用 requestInterceptor1 并向 content 中写入了数据");
|
||||||
|
|
||||||
|
// 测试拦截器执行顺序
|
||||||
|
Assert.assertEquals("requestInterceptor1", interceptorOrder.get(0));
|
||||||
|
Assert.assertEquals("requestInterceptor2", interceptorOrder.get(1));
|
||||||
|
Assert.assertEquals("responseInterceptor1", interceptorOrder.get(2));
|
||||||
|
Assert.assertEquals("responseInterceptor2", interceptorOrder.get(3));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class TestThread extends Thread {
|
public static class TestThread extends Thread {
|
||||||
private CloseableHttpClient client;
|
private CloseableHttpClient client;
|
||||||
|
|||||||
Reference in New Issue
Block a user