运行结果:
我是拦截器一1
我是拦截器2
我是拦截器3
我是最后一个拦截器 :在这里不进行向下传递,进行消耗
拦截器3响应返回
拦截器2响应返回
拦截器一响应返回
定义拦截器 Interceptor
public interface Interceptor {
Response intercept(Chain chain) throws IOException;
interface Chain {
Requestx request();
Response proceed(Requestx request) throws IOException;
/**
* Returns the connection the request will be executed on. This is only
* available in the chains of network interceptors; for application
* interceptors this is always null.
*/
@Nullable
Connection connection();
}
}
定义具体拦截器实现类
public class RealInterceptorChain implements Interceptor.Chain {
private final List<Interceptor> interceptors;
public int index;
private final Requestx request;
private int calls;
public RealInterceptorChain(List<Interceptor> interceptors, int index,
Requestx request) {
this.interceptors = interceptors;
this.index = index;
this.request = request;
}
@Override
public Requestx request() {
return request;
}
@Override
public Response proceed(Requestx request) throws IOException {
if (index >= interceptors.size())
throw new AssertionError();
calls++;
RealInterceptorChain next = new RealInterceptorChain(interceptors,
index + 1, request);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
if (index + 1 < interceptors.size() && next.calls != 1) {
throw new IllegalStateException("network interceptor "
+ interceptor + " must call proceed() exactly once");
}
return response;
}
@Override
@Nullable
public Connection connection() {
return new Connection();
}
}
定义的Response ,RequestX,Connection
public final class Response {
public Response() {
super();
}
}
public class Requestx {
public Requestx() {
}
}
public class Connection {
}
定义拦截器 InterceptorOne,InterceptorTwo,InterceptorThress,LastInterceptor
/**
* 1.传递下去 条件 2.消耗在当前类
*
* @author weichyang
*
*/
public class InterceptorOne implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
RealInterceptorChain chain2 = (RealInterceptorChain) chain;
System.out.println("我是拦截器一" + chain2.index);
Requestx requestx = chain.request();
Response response = chain.proceed(requestx); // 调用实现进入拦截器二
System.out.println("拦截器一响应返回");
return response;
}
}
public class InterceptorTwo implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
System.out.println("我是拦截器2");
Requestx requestx = chain.request();
Response response = chain.proceed(requestx);
System.out.println("拦截器2响应返回");
return response;
}
}
public class InterceptorThree implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
System.out.println("我是拦截器3");
Requestx requestx = chain.request();
Response response = chain.proceed(requestx);
System.out.println("拦截器3响应返回");
return response;
}
}
public class LastInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
System.out.println("我是最后一个拦截器 :在这里不进行向下传递,进行消耗");
return new Response();
}
}
定义拦截器调用入口
public class Client {
public static void main(String[] args) {
List<Interceptor> interceptors = new ArrayList();
interceptors.add(new InterceptorOne());
interceptors.add(new InterceptorTwo());
interceptors.add(new InterceptorThree());
interceptors.add(new LastInterceptor());
Requestx request = new Requestx();
RealInterceptorChain realInterceptorChain = new RealInterceptorChain(
interceptors, 0, request);
try {
realInterceptorChain.proceed(request);
} catch (IOException e) {
e.printStackTrace();
}
}
}