package com.changhu.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Objects;

/**
 * @author 20252
 * @createTime 2024/11/19 下午3:07
 * @desc BodyWrapperFilter...
 */
@Slf4j
public class BodyWrapperFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("进入filter:{}", servletRequest.getRemoteAddr());
        ServletRequest requestWrapper = null;
        if (servletRequest instanceof HttpServletRequest) {
            requestWrapper = new CustomHttpServletRequestWrapper((HttpServletRequest) servletRequest);
        }
        filterChain.doFilter(Objects.requireNonNullElse(requestWrapper, servletRequest), servletResponse);

    }
}
