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

com.tlcsdm.log.base.BaseUtils Maven / Gradle / Ivy

There is a newer version: 1.6
Show newest version
package com.tlcsdm.log.base;

import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 常用方法封装
 *
 * @author: TangLiang
 * @date: 2021/4/14 15:16
 * @since: 1.0
 */
public class BaseUtils {

    public static String DATA = "data";
    public static String TRACE_ID = "traceId";

    /**
     * 类不能实例化
     */
    private BaseUtils() {
    }

    /**
     * 返回成功信息(用于加载对象)
     *
     * @param result 加载对象数据
     * @return java.util.Map
     */
    public static Map loadSuccess(Map result) {
        Map data = new HashMap<>(4);
        data.put(DATA, result);
        return success(data);
    }

    /**
     * 返回成功信息(用于保存返回id信息)
     *
     * @param id id
     * @return java.util.Map
     */
    public static Map saveSuccess(String id) {
        Map result = new HashMap<>(4);
        result.put("success", true);
        result.put(DATA, id);
        return result;
    }

    /**
     * 返回成功信息(用于存储过程方式的结果返回)
     *
     * @param result 数据集
     * @return java.util.Map
     */
    public static Map success(Map result) {
        result.put("success", true);
        return result;
    }

    /**
     * 返回成功信息(用于JdbcTemplate的结果返回)
     *
     * @param list 数据集
     * @return java.util.Map
     */
    public static  Map success(List list) {
        Map result = new HashMap<>(4);
        result.put("success", true);
        result.put(DATA, list);
        return result;
    }

    /**
     * 返回成功信息(用于JdbcTemplate的结果返回)
     *
     * @param list  数据集
     * @param total 数据集总数
     * @return java.util.Map
     */
    public static  Map success(List list, int total) {
        Map result = success(list);
        result.put("total", total);
        return result;
    }

    /**
     * 返回成功信息(用于JdbcTemplate的结果返回)
     *
     * @param list  数据集
     * @param total 数据集总数
     * @return java.util.Map
     */
    public static  Map success(List list, long total) {
        Map result = success(list);
        result.put("total", total);
        return result;
    }

    /**
     * 返回成功信息(用于存储过程方式的结果返回)
     *
     * @return java.util.Map
     */
    public static Map success() {
        return Collections.singletonMap("success", true);
    }

    /**
     * 返回失败信息(用于存储过程方式的结果返回)
     *
     * @param message 错误信息
     * @return java.util.Map
     */
    public static Map failed(String message) {
        Map result = new HashMap<>(4);
        result.put("success", false);
        result.put("message", message);
        return result;
    }

    /**
     * 返回失败信息(用于存储过程方式的结果返回)
     *
     * @param result  数据集
     * @param message 错误信息
     * @return java.util.Map
     */
    public static Map failed(Map result, String message) {
        result.put("success", false);
        result.put("message", message);
        return result;
    }

    /**
     * 获取请求客户端ip地址
     *
     * @param request request
     * @return 客户端ip地址
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-FORWARDED-FOR");
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            return "127.0.0.1";
        }
        //解决经过nginx转发, 配置了proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;带来的多ip的情况
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }

        return ip;
    }

    /**
     * 获取请求url路径
     *
     * @param request request
     * @return url路径
     */
    public static String getUrl(HttpServletRequest request) {
        String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getServletPath();
        if (request.getQueryString() != null) {
            url += "?" + request.getQueryString();
        }

        return url;
    }

    /**
     * 主机名
     *
     * @return 主机名
     */
    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "未知";
    }

    /**
     * 获取cookie值
     *
     * @param key     cookie键
     * @param request request
     * @return cookie值
     */
    public static String getCookieValue(String key, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (key.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return "";
    }

    public static int newHashMapWithExpectedSize(int expectedSize) {
        if (expectedSize < 3) {
            return 4;
        } else {
            return expectedSize < 1073741824 ? (int) ((float) expectedSize / 0.75F + 1.0F) : 2147483647;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy