mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-08 22:58:05 +08:00
add GlobalInterceptor
This commit is contained in:
parent
63ad6de20d
commit
6427d6fb13
@ -47,6 +47,7 @@
|
|||||||
* 【db 】 DruidDataSource构建时支持自定义参数(issue#I4ZKCW@Gitee)
|
* 【db 】 DruidDataSource构建时支持自定义参数(issue#I4ZKCW@Gitee)
|
||||||
* 【poi 】 ExcelWriter增加addImg重载(issue#2218@Github)
|
* 【poi 】 ExcelWriter增加addImg重载(issue#2218@Github)
|
||||||
* 【bloomFilter】 增加FuncFilter
|
* 【bloomFilter】 增加FuncFilter
|
||||||
|
* 【http 】 增加GlobalInterceptor(issue#2217)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复ObjectUtil.hasNull传入null返回true的问题(pr#555@Gitee)
|
* 【core 】 修复ObjectUtil.hasNull传入null返回true的问题(pr#555@Gitee)
|
||||||
|
@ -113,7 +113,7 @@ public enum GlobalHeaders {
|
|||||||
* @param isOverride 是否覆盖已有值
|
* @param isOverride 是否覆盖已有值
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public GlobalHeaders header(String name, String value, boolean isOverride) {
|
synchronized public GlobalHeaders header(String name, String value, boolean isOverride) {
|
||||||
if (null != name && null != value) {
|
if (null != name && null != value) {
|
||||||
final List<String> values = headers.get(name.trim());
|
final List<String> values = headers.get(name.trim());
|
||||||
if (isOverride || CollectionUtil.isEmpty(values)) {
|
if (isOverride || CollectionUtil.isEmpty(values)) {
|
||||||
@ -192,7 +192,7 @@ public enum GlobalHeaders {
|
|||||||
* @param name Header名
|
* @param name Header名
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public GlobalHeaders removeHeader(String name) {
|
synchronized public GlobalHeaders removeHeader(String name) {
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
headers.remove(name.trim());
|
headers.remove(name.trim());
|
||||||
}
|
}
|
||||||
@ -224,7 +224,7 @@ public enum GlobalHeaders {
|
|||||||
* @return this
|
* @return this
|
||||||
* @since 5.7.13
|
* @since 5.7.13
|
||||||
*/
|
*/
|
||||||
public GlobalHeaders clearHeaders() {
|
synchronized public GlobalHeaders clearHeaders() {
|
||||||
this.headers.clear();
|
this.headers.clear();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package cn.hutool.http;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局的拦截器
|
||||||
|
*
|
||||||
|
* @author looly
|
||||||
|
* @since 5.8.0
|
||||||
|
*/
|
||||||
|
public enum GlobalInterceptor {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
private final HttpInterceptor.Chain interceptors = new HttpInterceptor.Chain();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置拦截器,用于在请求前重新编辑请求
|
||||||
|
*
|
||||||
|
* @param interceptor 拦截器实现
|
||||||
|
*/
|
||||||
|
synchronized public GlobalInterceptor addInterceptor(HttpInterceptor interceptor) {
|
||||||
|
this.interceptors.addChain(interceptor);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
synchronized public GlobalInterceptor clear(){
|
||||||
|
interceptors.clear();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 复制过滤器列表
|
||||||
|
* @return {@link cn.hutool.http.HttpInterceptor.Chain}
|
||||||
|
*/
|
||||||
|
HttpInterceptor.Chain getCopied(){
|
||||||
|
final HttpInterceptor.Chain copied = new HttpInterceptor.Chain();
|
||||||
|
for (HttpInterceptor interceptor : this.interceptors) {
|
||||||
|
copied.addChain(interceptor);
|
||||||
|
}
|
||||||
|
return copied;
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,6 @@ public interface HttpInterceptor {
|
|||||||
class Chain implements cn.hutool.core.lang.Chain<HttpInterceptor, Chain> {
|
class Chain implements cn.hutool.core.lang.Chain<HttpInterceptor, Chain> {
|
||||||
private final List<HttpInterceptor> interceptors = new LinkedList<>();
|
private final List<HttpInterceptor> interceptors = new LinkedList<>();
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Chain addChain(HttpInterceptor element) {
|
public Chain addChain(HttpInterceptor element) {
|
||||||
interceptors.add(element);
|
interceptors.add(element);
|
||||||
@ -40,5 +39,16 @@ public interface HttpInterceptor {
|
|||||||
public Iterator<HttpInterceptor> iterator() {
|
public Iterator<HttpInterceptor> iterator() {
|
||||||
return interceptors.iterator();
|
return interceptors.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空
|
||||||
|
*
|
||||||
|
* @return this
|
||||||
|
* @since 5.8.0
|
||||||
|
*/
|
||||||
|
public Chain clear() {
|
||||||
|
interceptors.clear();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -206,7 +206,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
|||||||
/**
|
/**
|
||||||
* 请求前的拦截器,用于在请求前重新编辑请求
|
* 请求前的拦截器,用于在请求前重新编辑请求
|
||||||
*/
|
*/
|
||||||
private final HttpInterceptor.Chain interceptors = new HttpInterceptor.Chain();
|
private final HttpInterceptor.Chain interceptors = GlobalInterceptor.INSTANCE.getCopied();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认连接超时
|
* 默认连接超时
|
||||||
@ -967,8 +967,9 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
|||||||
* @param interceptor 拦截器实现
|
* @param interceptor 拦截器实现
|
||||||
* @since 5.7.16
|
* @since 5.7.16
|
||||||
*/
|
*/
|
||||||
public void addInterceptor(HttpInterceptor interceptor) {
|
public HttpRequest addInterceptor(HttpInterceptor interceptor) {
|
||||||
this.interceptors.addChain(interceptor);
|
this.interceptors.addChain(interceptor);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -167,4 +167,17 @@ public class HttpRequestTest {
|
|||||||
execute = HttpRequest.get(url).setMaxRedirectCount(1).execute();
|
execute = HttpRequest.get(url).setMaxRedirectCount(1).execute();
|
||||||
Console.log(execute.getStatus(), execute.header(Header.LOCATION));
|
Console.log(execute.getStatus(), execute.header(Header.LOCATION));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void addInterceptorTest() {
|
||||||
|
HttpUtil.createGet("https://hutool.cn").addInterceptor(Console::log).execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void addGlobalInterceptorTest() {
|
||||||
|
GlobalInterceptor.INSTANCE.addInterceptor(Console::log);
|
||||||
|
HttpUtil.createGet("https://hutool.cn").execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user