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

top.jfunc.http.interceptor.RequestIdInterceptor Maven / Gradle / Ivy

package top.jfunc.http.interceptor;

import top.jfunc.common.utils.CommonUtil;
import top.jfunc.http.request.HttpRequest;

import java.io.IOException;

/**
 * 用于生成一个RequestId放到请求头中,key默认为"RequestId"
 * @author xiongshiyan at 2020/1/20 , contact me with email [email protected] or phone 15208384257
 */
public class RequestIdInterceptor implements Interceptor {
    public static final String REQUEST_ID = "RequestId";

    /**
     * header key
     */
    private String requestIdKey;

    public RequestIdInterceptor(String requestIdKey) {
        this.requestIdKey = requestIdKey;
    }

    public RequestIdInterceptor() {
        this.requestIdKey = REQUEST_ID;
    }

    @Override
    public HttpRequest onBefore(HttpRequest httpRequest) throws IOException {
        httpRequest.addHeader(getRequestIdKey() , generateRequestId(httpRequest));
        return httpRequest;
    }

    protected String generateRequestId(HttpRequest httpRequest){
        return CommonUtil.randomString(16);
    }


    public String getRequestIdKey() {
        return requestIdKey;
    }

    public void setRequestIdKey(String requestIdKey) {
        this.requestIdKey = requestIdKey;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy