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

com.dahua.eco.base.spring.utils.RequestUtils Maven / Gradle / Ivy

package com.dahua.eco.base.spring.utils;

import com.dahua.eco.base.core.util.IPUtils;
import org.apache.tomcat.util.http.parser.AcceptLanguage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

public class RequestUtils {
    private static Logger logger = LoggerFactory.getLogger(RequestUtils.class);

    /**
     * 国际化地区参数,默认为中国大陆
     */
    private static Locale DEFAULT_LOCALE = Locale.CHINA;

    private static String DEFAULT_ACCEPT_LANGUAGE = "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7";


    /**
     * 获取request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null && attributes.getRequest() != null) {
            HttpServletRequest request = attributes.getRequest();
            return request;
        }

        return null;
    }

    /**
     * 从 request 中获取 accept-language 的配置字符串
     * 

* 如果获取失败,则返回默认值:"en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7" * * @return */ public static String getAcceptLanguage() { HttpServletRequest request = getRequest(); if (request == null) { return DEFAULT_ACCEPT_LANGUAGE; } String language = request.getHeader("accept-language"); if (StringUtils.isEmpty(language)) { return DEFAULT_ACCEPT_LANGUAGE; } return language; } /** * 获取Locale信息--国家、地区 *

* 默认先从request的header中拿到accept-language进行解析,如果没有,再从request中直接获取locale * * @return */ public static Locale getLocale() { Locale locale = null; HttpServletRequest request = getRequest(); if (request == null) { return DEFAULT_LOCALE; } String language = request.getHeader("accept-language"); if (StringUtils.hasText(language)) { List acceptLanguages = null; try { acceptLanguages = AcceptLanguage.parse(new StringReader(language)); if (!CollectionUtils.isEmpty(acceptLanguages)) { locale = acceptLanguages.get(0).getLocale(); } } catch (IOException e) { logger.error("parse accept-language from header failed, use default value instead"); } } else { locale = request.getLocale(); } return locale == null ? DEFAULT_LOCALE : locale; } /** * 根据请求的源ip,生成全局唯一的请求标识requestId *

* 生成规则如下: *
1、requestId = hex(long(ip)) + "-" + uuid *
2、将ip按照分段加权原则,转换成整型值value,再将value转换成小写的十六进制8位字符串 *
3、uuid为不包含"-"的32位小写字符串 *
4、示例:0a23a41f-efe6dd9f1fcc466283c6a6799203c27e * * @param sourceIp 源ip * * @return 41位的全局唯一的请求标识 */ public static String generateRequestId(String sourceIp) { // 获取ip对应的小写十六进制字符串 String hexString = IPUtils.convert2Hex(sourceIp, true, 8); // 生成不包含"-"的32位UUID字符串 String uuidString = UUID.randomUUID().toString().replace("-", "").toLowerCase(); return hexString + "-" + uuidString; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy