package com.jy.framework.utils;

import cn.hutool.extra.servlet.JakartaServletUtil;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerMapping;

import java.util.Map;

/**
 * 客户端工具类
 */
@Slf4j
@SuppressWarnings("unused")
public class ServletUtil extends JakartaServletUtil {

    /**
     * 获取匹配路径
     */
    public static String getBestMatching(HttpServletRequest request) {
        return request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString();
    }

    /**
     * 获取param
     */
    public static String getParam(HttpServletRequest request) {
        return JSONObject.toJSONString(ServletUtil.getParamMap(request));
    }

    /**
     * 获取variable
     */
    public static String getVariable(HttpServletRequest request) {
        return JSONObject.toJSONString(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE));
    }

    /**
     * 获取param
     */
    public static Map<String, Object> getBodyMap(HttpServletRequest request) {
        return JSONObject.parse(ServletUtil.getBody(request));
    }

}
