mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add HttpInterceptor
This commit is contained in:
parent
e58c8c9d6c
commit
2818809ee8
@ -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)
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user