package com.app.framework.core.utils.request;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Request工具类
 */
public class RequestUtil {
    public static final String REQUEST_AJAX_HEADER = "X-Requested-With";
    public static final String XML_HTTP_REQUEST = "XMLHttpRequest";

    private static String RequestServer = null;

    /**
     * SpringMvc下获取request
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取BasePath
     *
     * @return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
     */
    public static String getBasePath(HttpServletRequest request) {
        if (RequestServer == null) {
            String port = request.getServerPort() + "";
            port = "80".equals(port) ? "" : (":" + port);
            String requestServer = request.getScheme() + "://" + request.getServerName() + port;
            RequestServer = requestServer + request.getContextPath();
        }
        return RequestServer;
    }

    /**
     * 获取IP地址
     *
     * 使用Nginx等反向代理软件， 则不能通过request.getRemoteAddr()获取IP地址
     * 如果使用了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP地址，X-Forwarded-For中第一个非unknown的有效IP字符串，则为真实IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        String Xip = request.getHeader("X-Real-IP");
        String XFor = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = XFor.indexOf(",");
            if (index != -1) {
                return XFor.substring(0, index);
            } else {
                return XFor;
            }
        }
        XFor = Xip;
        if (StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)) {
            return XFor;
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(XFor) ? "127.0.0.1" : XFor;
    }


    /**
     * 判断是否是Ajax请求
     *
     * @param request 请求
     * @return true or false
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        return request.getHeader(REQUEST_AJAX_HEADER) != null && request.getHeader(REQUEST_AJAX_HEADER).equalsIgnoreCase(XML_HTTP_REQUEST);
    }

}
