package cn.pluss.platform.interceptor.filter;

import cn.pluss.platform.interceptor.wrapper.RequestWrapper;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

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

/**
 * 用于替换默认的 HttpServletRequest
 * @author Djh
 */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class WrapFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//        String contentType = httpServletRequest.getContentType();
//        String method = "multipart/form-data";

//        if (contentType != null && contentType.contains(method)) {
//            // 将转化后的 request 放入过滤链中
//            httpServletRequest = new StandardServletMultipartResolver().resolveMultipart(httpServletRequest);
//        }


        ServletRequest requestWrapper = new RequestWrapper(httpServletRequest);
        filterChain.doFilter(requestWrapper, servletResponse);

//        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
    }
}
