All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.majingjing.http.client.handler.chain.RealHttpChain Maven / Gradle / Ivy

The newest version!
package cn.majingjing.http.client.handler.chain;

import cn.majingjing.http.client.exception.HttpClientException;
import cn.majingjing.http.client.handler.HttpHandler;
import cn.majingjing.http.client.interceptor.HttpInterceptor;
import cn.majingjing.http.client.request.HttpRequest;
import cn.majingjing.http.client.response.HttpResponse;

import java.util.Collections;
import java.util.List;

/**
 * 真实调用链
 *
 * @author MaMarion
 * @date 2020/5/1
 */
public class RealHttpChain implements HttpChain {

    private HttpRequest httpRequest;

    private HttpHandler httpHandler;
    private List httpInterceptorList;

    public RealHttpChain(HttpHandler httpHandler, List httpInterceptorList) {
        if (httpHandler == null) {
            throw new NullPointerException("HttpHandler is null");
        }
        this.httpHandler = httpHandler;
        if (httpInterceptorList != null) {
            this.httpInterceptorList = httpInterceptorList;
        } else {
            this.httpInterceptorList = Collections.emptyList();
        }
    }

    /**
     * 当前调用链层级
     */
    private int location = 0;

    @Override
    public HttpResponse proceed(HttpRequest httpRequest) throws HttpClientException {
        if (httpHandler == null) {
            throw new NullPointerException("httpRequest is null");
        }
        this.httpRequest = httpRequest;
        if (location == httpInterceptorList.size()) {
            HttpResponse httpResponse = httpHandler.handleRequest(this.httpRequest);
            return httpResponse;
        } else {
            return httpInterceptorList.get(location++).intercept(this);
        }
    }

    @Override
    public HttpRequest getHttpRequest() {
        return httpRequest;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy