package com.tofly.common.core.configs;


import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

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

/**
 * @author tuonbed
 * @date 2019/10/30  10:00
 */
@Component
@WebFilter(urlPatterns = "/**", filterName = "ParamsFilter",dispatcherTypes=DispatcherType.REQUEST)
public class MyParamsFilter implements Filter {

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

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        MyParameterRequestWrapper parmsRequest = new MyParameterRequestWrapper((HttpServletRequest) servletRequest);

        if (MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(((HttpServletRequest) servletRequest).getHeader(HttpHeaders.CONTENT_TYPE))
        || parmsRequest.getParameterMap().size()>0
        ) {
            filterChain.doFilter(parmsRequest, servletResponse);
        }else{
            filterChain.doFilter(servletRequest, servletResponse);
        }
//        if(parmsRequest.getParameterMap().size()==0){
//            filterChain.doFilter(servletRequest, servletResponse);
//        }else{
//            filterChain.doFilter(parmsRequest, servletResponse);
//        }


    }

    @Override
    public void destroy() {

    }
}
