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

cn.majingjing.http.client.request.HttpRequest Maven / Gradle / Ivy

package cn.majingjing.http.client.request;


import cn.majingjing.http.client.HttpClient;
import cn.majingjing.http.client.config.HttpClientConfig;
import cn.majingjing.http.client.interceptor.HttpInterceptor;
import cn.majingjing.http.client.util.HttpConstant;
import cn.majingjing.http.client.util.ObjectUtils;
import cn.majingjing.http.client.util.StringUtils;

import java.util.*;

/**
 * @author MaMarion
 * @date 2020/5/1
 */
public class HttpRequest {

    private List interceptors;
    /**
     * 请求方式
     */
    private String method;
    /**
     * 请求地址
     */
    private String url;
    /**
     * 存储请求头信息
     */
    private Map headers;
    /**
     * url上的请求参数
     */
    private String param;
    /**
     * 请求体内容
     */
    private byte[] body;
    /**
     * 连接超时时间 (默认10s)
     */
    private int connectTimeout;
    /**
     * 流读取超时时间 (默认10s)
     */
    private int readTimeout;
    /**
     * 流写入超时时间
     */
    private int writeTimeout;
    /**
     * 失败最大重试次数 (默认不重试)
     */
    private int maxRetry;

    HttpRequest(BaseRequest request) {
        HttpClientConfig globalConfig = HttpClient.Instance.getConfig();
        this.method = request.method;
        this.url = request.url;
        this.body = request.body;

        //合并globalConfig数据
        this.headers = new HashMap<>(globalConfig.getHeaders());
        if (request.headers != null && !request.headers.isEmpty()) {
            this.headers.putAll(request.headers);
        }
        this.connectTimeout = ObjectUtils.getValueOrDefault(request.connectTimeout, globalConfig.getConnectTimeout());
        this.readTimeout = ObjectUtils.getValueOrDefault(request.readTimeout, globalConfig.getReadTimeout());
        this.writeTimeout = ObjectUtils.getValueOrDefault(request.writeTimeout, globalConfig.getWriteTimeout());
        this.maxRetry = ObjectUtils.getValueOrDefault(request.maxRetry, globalConfig.getMaxRetry());
        this.interceptors = request.interceptors;

        if(StringUtils.isNotEmpty(request.traceId)){
            this.headers.put(HttpConstant.TRACE_ID_NAME,request.traceId);
        }
    }

    public String getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }

    public Map getHeaders() {
        return headers;
    }

    public String getParam() {
        return param;
    }

    public byte[] getBody() {
        return body;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public int getWriteTimeout() {
        return writeTimeout;
    }

    public int getMaxRetry() {
        return maxRetry;
    }

    public List getInterceptors() {
        return interceptors;
    }

    public String getTraceId(){
        return headers.get(HttpConstant.TRACE_ID_NAME);
    }

    @Override
    public String toString() {
        return "method=" + method + '\n' +
                "url=" + url + '\n' +
                "headers=" + headers +
                "body=" + StringUtils.bytesToString(body) + '\n' +
                "connectTimeout=" + connectTimeout + '\n' +
                "readTimeout=" + readTimeout + '\n' +
                "writeTimeout=" + writeTimeout + '\n' +
                "maxRetry=" + maxRetry + '\n'
                ;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy