運(yùn)行結(jié)果:
我是攔截器一1
我是攔截器2
我是攔截器3
我是最后一個(gè)攔截器 :在這里不進(jìn)行向下傳遞,進(jìn)行消耗
攔截器3響應(yīng)返回
攔截器2響應(yīng)返回
攔截器一響應(yīng)返回
定義攔截器 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();
}
}
定義具體攔截器實(shí)現(xiàn)類
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.消耗在當(dāng)前類
*
* @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); // 調(diào)用實(shí)現(xiàn)進(jìn)入攔截器二
System.out.println("攔截器一響應(yīng)返回");
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響應(yīng)返回");
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響應(yīng)返回");
return response;
}
}
public class LastInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
System.out.println("我是最后一個(gè)攔截器 :在這里不進(jìn)行向下傳遞,進(jìn)行消耗");
return new Response();
}
}
定義攔截器調(diào)用入口
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();
}
}
}