diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f8bd61f0..370dee44b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ * 【core 】 ActualTypeMapperPool增加getStrKeyMap方法(pr#447@Gitee) * 【core 】 TreeUtil增加walk方法(pr#1932@Gitee) * 【crypto 】 SmUtil增加sm3WithSalt(pr#454@Gitee) +* 【http 】 增加HttpInterceptor(issue#I4H1ZV@Gitee) ### 🐞Bug修复 * 【core 】 修复UrlBuilder.addPath歧义问题(issue#1912@Github) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpInterceptor.java b/hutool-http/src/main/java/cn/hutool/http/HttpInterceptor.java new file mode 100644 index 000000000..308cc85dc --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/HttpInterceptor.java @@ -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 { + private final List interceptors = new LinkedList<>(); + + + @Override + public Chain addChain(HttpInterceptor element) { + interceptors.add(element); + return this; + } + + @Override + public Iterator iterator() { + return interceptors.iterator(); + } + } +} diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java index d5750e000..e0135e77a 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -88,6 +88,11 @@ public class HttpRequest extends HttpBase { 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 { */ private SSLSocketFactory ssf; - /** - * 请求前的处理器,用于在请求前重新编辑请求,类似于拦截器 - */ - private Consumer consumer; - /** * 构造,URL编码默认使用UTF-8 * @@ -274,8 +274,7 @@ public class HttpRequest extends HttpBase { * @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 { } /** - * 设置请求前的处理器,用于在请求前重新编辑请求,类似于拦截器 + * 设置拦截器,用于在请求前重新编辑请求 * - * @param consumer 请求前的处理器,用于在请求前重新编辑请求,类似于拦截器 + * @param interceptor 拦截器实现 * @since 5.7.16 */ - public void setConsumer(Consumer consumer) { - this.consumer = consumer; + public void addInterceptor(HttpInterceptor interceptor) { + this.interceptors.addChain(interceptor); } /** @@ -964,7 +963,7 @@ public class HttpRequest extends HttpBase { * @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 { /** * 执行Reuqest请求 * - * @param isAsync 是否异步 + * @param isAsync 是否异步 + * @param interceptors 拦截器列表 * @return this */ - private HttpResponse doExecute(boolean isAsync, Consumer 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