add HttpInterceptor

This commit is contained in:
Looly 2021-11-06 01:49:34 +08:00
parent e58c8c9d6c
commit 2818809ee8
3 changed files with 63 additions and 16 deletions

View File

@ -25,6 +25,7 @@
* 【core 】 ActualTypeMapperPool增加getStrKeyMap方法pr#447@Gitee
* 【core 】 TreeUtil增加walk方法pr#1932@Gitee
* 【crypto 】 SmUtil增加sm3WithSaltpr#454@Gitee
* 【http 】 增加HttpInterceptorissue#I4H1ZV@Gitee
### 🐞Bug修复
* 【core 】 修复UrlBuilder.addPath歧义问题issue#1912@Github

View File

@ -0,0 +1,44 @@
package cn.hutool.http;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* Http拦截器接口通过实现此接口完成请求发起前对请求的编辑工作
*
* @author looly
* @since 5.7.16
*/
@FunctionalInterface
public interface HttpInterceptor {
/**
* 处理请求
*
* @param request 请求
*/
void process(HttpRequest request);
/**
* 拦截器链
*
* @author looly
* @since 5.7.16
*/
class Chain implements cn.hutool.core.lang.Chain<HttpInterceptor, Chain> {
private final List<HttpInterceptor> interceptors = new LinkedList<>();
@Override
public Chain addChain(HttpInterceptor element) {
interceptors.add(element);
return this;
}
@Override
public Iterator<HttpInterceptor> iterator() {
return interceptors.iterator();
}
}
}

View File

@ -88,6 +88,11 @@ public class HttpRequest extends HttpBase<HttpRequest> {
private UrlBuilder url;
private URLStreamHandler urlHandler;
private Method method = Method.GET;
/**
* 请求前的拦截器用于在请求前重新编辑请求
*/
private final HttpInterceptor.Chain interceptors = new HttpInterceptor.Chain();
/**
* 默认连接超时
*/
@ -147,11 +152,6 @@ public class HttpRequest extends HttpBase<HttpRequest> {
*/
private SSLSocketFactory ssf;
/**
* 请求前的处理器用于在请求前重新编辑请求类似于拦截器
*/
private Consumer<HttpRequest> consumer;
/**
* 构造URL编码默认使用UTF-8
*
@ -274,8 +274,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
* @since 4.1.8
*/
public HttpRequest setUrl(String url) {
this.url = UrlBuilder.ofHttp(url, this.charset);
return this;
return setUrl(UrlBuilder.ofHttp(url, this.charset));
}
/**
@ -925,13 +924,13 @@ public class HttpRequest extends HttpBase<HttpRequest> {
}
/**
* 设置请求前的处理用于在请求前重新编辑请求类似于拦截器
* 设置拦截用于在请求前重新编辑请求
*
* @param consumer 请求前的处理器用于在请求前重新编辑请求类似于拦截器
* @param interceptor 拦截器实现
* @since 5.7.16
*/
public void setConsumer(Consumer<HttpRequest> consumer) {
this.consumer = consumer;
public void addInterceptor(HttpInterceptor interceptor) {
this.interceptors.addChain(interceptor);
}
/**
@ -964,7 +963,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
* @return this
*/
public HttpResponse execute(boolean isAsync) {
return doExecute(isAsync, this.consumer);
return doExecute(isAsync, this.interceptors);
}
/**
@ -1057,12 +1056,15 @@ public class HttpRequest extends HttpBase<HttpRequest> {
/**
* 执行Reuqest请求
*
* @param isAsync 是否异步
* @param isAsync 是否异步
* @param interceptors 拦截器列表
* @return this
*/
private HttpResponse doExecute(boolean isAsync, Consumer<HttpRequest> consumer) {
if (null != consumer) {
consumer.accept(this);
private HttpResponse doExecute(boolean isAsync, HttpInterceptor.Chain interceptors) {
if (null != interceptors) {
for (HttpInterceptor interceptor : interceptors) {
interceptor.process(this);
}
}
// 初始化URL