cn.hutool.http.GlobalInterceptor Maven / Gradle / Ivy
The newest version!
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 - 2025 Weber Informatics LLC | Privacy Policy