package com.elab.log.utils;

import com.elab.core.utils.StringUtils;

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

/**
 * @describe ：请求工具类
 * @Author ： liukx
 * @time ： 2019/1/25 - 11:46
 */
public class RequestUtils {

    /**
     * 获取客户端编号
     *
     * @param req
     * @return
     */
    public static String getClientIP(HttpServletRequest req) {
        return StringUtils.getPriorityNotNull(req.getHeader("remoteip"), req.getHeader("x-forwarded-for"), req.getRemoteAddr());
    }

    /**
     * 获取客户端设备
     *
     * @param req
     * @return
     */
    public static String getUserAgent(HttpServletRequest req) {
        String userAgent = req.getHeader("user-agent");
        return userAgent;
    }

    /**
     * 获取客户端来源
     *
     * @param req
     * @return
     */
    public static String getReferer(HttpServletRequest req) {
        String referer = req.getHeader("referer");
        return referer;
    }

    /**
     * 获取请求中的操作系统
     *
     * @param req
     * @return
     */
    public static String getSystem(HttpServletRequest req) {
        String userAgent = getUserAgent(req);
        if (StringUtils.isNotEmpty(userAgent)) {
            if (userAgent.indexOf(CatMsgConstants.AGENT_ANDROID) > 0) {
                return CatMsgConstants.AGENT_ANDROID;
            } else if (userAgent.indexOf(CatMsgConstants.AGENT_IPHONE) > 0) {
                return CatMsgConstants.AGENT_IPHONE;
            }
        }
        return userAgent;
    }

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>(1);
        map.put("1", "2");
        map.put("1", "2");
        map.put("1", "2");
        map.put("1", "2");
        map.put("1", "2");
        map.put("1", "2");
    }

}
