cn.hutool.http.GlobalInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.http;
/**
* 全局的拦截器
* 包括请求拦截器和响应拦截器
*
* @author looly
* @since 5.8.0
*/
public enum GlobalInterceptor {
INSTANCE;
private final HttpInterceptor.Chain requestInterceptors = new HttpInterceptor.Chain<>();
private final HttpInterceptor.Chain responseInterceptors = new HttpInterceptor.Chain<>();
/**
* 设置拦截器,用于在请求前重新编辑请求
*
* @param interceptor 拦截器实现
* @return this
*/
synchronized public GlobalInterceptor addRequestInterceptor(HttpInterceptor interceptor) {
this.requestInterceptors.addChain(interceptor);
return this;
}
/**
* 设置拦截器,用于在响应读取后完成编辑或读取
*
* @param interceptor 拦截器实现
* @return this
*/
synchronized public GlobalInterceptor addResponseInterceptor(HttpInterceptor interceptor) {
this.responseInterceptors.addChain(interceptor);
return this;
}
/**
* 清空请求和响应拦截器
*
* @return this
*/
public GlobalInterceptor clear() {
clearRequest();
clearResponse();
return this;
}
/**
* 清空请求拦截器
*
* @return this
*/
synchronized public GlobalInterceptor clearRequest() {
requestInterceptors.clear();
return this;
}
/**
* 清空响应拦截器
*
* @return this
*/
synchronized public GlobalInterceptor clearResponse() {
responseInterceptors.clear();
return this;
}
/**
* 复制请求过滤器列表
*
* @return {@link cn.hutool.http.HttpInterceptor.Chain}
*/
HttpInterceptor.Chain getCopiedRequestInterceptor() {
final HttpInterceptor.Chain copied = new HttpInterceptor.Chain<>();
for (HttpInterceptor interceptor : this.requestInterceptors) {
copied.addChain(interceptor);
}
return copied;
}
/**
* 复制响应过滤器列表
*
* @return {@link cn.hutool.http.HttpInterceptor.Chain}
*/
HttpInterceptor.Chain getCopiedResponseInterceptor() {
final HttpInterceptor.Chain copied = new HttpInterceptor.Chain<>();
for (HttpInterceptor interceptor : this.responseInterceptors) {
copied.addChain(interceptor);
}
return copied;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy