package com.mortals.xhx.base.framework.filter;

import cn.hutool.core.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 请求过滤链
 *
 * @author: zxfei
 * @date: 2022/4/20 14:52
 */
//@Component
@Slf4j
public class RequestFilter extends OncePerRequestFilter implements Filter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            request = new ContentCachingRequestWrapper(request);
            filterChain.doFilter(request, response);
        } catch (Exception e) {
            throw e;
        } finally {
            //清理ThreadLocal
            MDC.clear();
        }
    }

    public static void main(String[] args) {
        int totalPage = PageUtil.totalPage(1001, 1000);

        System.out.println("totalPage:" + totalPage);

        int start = PageUtil.getStart(2, 1000);
        System.out.println("start:" + start);
    }


}
