package com.cool.store.config;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * @Author: JCccc
 * @Date: 2022-6-12 10:35
 * @Description:
 */
@Component
@Order(2)
public class BodyWrapperFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;
        if (org.springframework.util.StringUtils.hasLength(servletRequest.getContentType()) && servletRequest.getContentType().contains("multipart/form-data")) {
            filterChain.doFilter(servletRequest, servletResponse);
        }else {
            if (servletRequest instanceof HttpServletRequest) {
                requestWrapper = new CustomHttpServletRequestWrapper((HttpServletRequest) servletRequest);
            }
            if (requestWrapper == null) {
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
                filterChain.doFilter(requestWrapper, servletResponse);
            }
        }
    }
}