package com.bs.common.filter;

import com.alibaba.fastjson2.JSONObject;
import com.bs.common.utils.ServletUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;
import java.util.Map;

/**
 * Repeatable 过滤器
 *
 * @author ruoyi
 */
public class PortFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory.getLogger(PortFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        Map paramMap = request.getParameterMap();
        //获取请求参数
        String queryString = ServletUtils.getRequest().getQueryString();
        //同一次请求，放同一个log。info 中，
        //防止同时请求多个接口，把同一个请求信息冲散，不便查看
        LOGGER.info("\r\n请求的链接:" + ServletUtils.getRequest().getRequestURL().toString()
        +"\r\n"+"请求方式:" + ServletUtils.getRequest().getMethod()
        +"\r\n"+"请求的接口是:" + ServletUtils.getRequest().getRequestURI()
        +"\r\n"+"请求参数map:{}", ServletUtils.getParamMap(request)
        +"\r\n"+"请求参数map:{}", new JSONObject(paramMap)
        +"\r\n"+"请求参数:{}",queryString);

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
