package com.chushang.security.feign;

import com.chushang.common.core.constant.SecurityConstants;
import com.chushang.common.core.util.IPUtils;
import com.chushang.common.core.util.ServletUtils;
import com.chushang.common.core.util.StringUtils;
import com.chushang.security.utils.NonWebRequestAttributes;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 *
 */
@Component
public class FeignRequestInterceptor implements RequestInterceptor
{
    @Override
    public void apply(RequestTemplate requestTemplate)
    {
        RequestAttributes requestAttributes = ServletUtils.getRequestAttributes();
        // 不为空时
        if (Objects.nonNull(requestAttributes)){
            if (requestAttributes instanceof ServletRequestAttributes){
                HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
                Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);
                // 传递用户信息请求头，防止丢失
                String userId = headers.get(SecurityConstants.DETAILS_USER_ID);
                if (StringUtils.isNotEmpty(userId))
                {
                    requestTemplate.header(SecurityConstants.DETAILS_USER_ID, userId);
                }
                String userName = headers.get(SecurityConstants.DETAILS_USERNAME);
                if (StringUtils.isNotEmpty(userName))
                {
                    requestTemplate.header(SecurityConstants.DETAILS_USERNAME, userName);
                }
                String authentication = headers.get(SecurityConstants.AUTHORIZATION_HEADER);
                if (StringUtils.isNotEmpty(authentication))
                {
                    requestTemplate.header(SecurityConstants.AUTHORIZATION_HEADER, authentication);
                }
                // 配置客户端IP
                requestTemplate.header("X-Forwarded-For", IPUtils.clientIp(ServletUtils.getRequest()));
            }
        }
        // 此时的情况 应当不为 浏览器调用, 所以请求头可能为空
        else {
            RequestContextHolder.setRequestAttributes(new NonWebRequestAttributes(), Boolean.TRUE);
            HttpServletRequest httpRequest = this.getHttpServletRequestSafely();
            if (null != httpRequest && null != httpRequest.getAttribute("X-Request-No")) {
                requestTemplate.header("X-Request-No",
                        httpRequest.getAttribute("X-Request-No").toString());
            }
        }
    }

    public HttpServletRequest getHttpServletRequestSafely() {
        try {
            RequestAttributes requestAttributesSafely = this.getRequestAttributesSafely();
            return requestAttributesSafely instanceof ServletRequestAttributes ? ((ServletRequestAttributes)requestAttributesSafely).getRequest() : null;
        } catch (Exception var2) {
            return null;
        }
    }

    public RequestAttributes getRequestAttributesSafely() {
        RequestAttributes requestAttributes;
        try {
            requestAttributes = RequestContextHolder.currentRequestAttributes();
        } catch (IllegalStateException var3) {
            requestAttributes = new NonWebRequestAttributes();
        }
        return requestAttributes;
    }
}