package cn.com.victorysoft.business.filter;

import cn.com.victorysoft.business.util.StringUtils;

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

/**
 * 参数验证，包括unitcode、month、month1、month2、testdate
 *
 * @author stareast
 */
public class ParamFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String unitcode = request.getParameter("unitcode");
        String month = request.getParameter("month");
        String month1 = request.getParameter("month1");
        String month2 = request.getParameter("month2");
        String testdate = request.getParameter("testdate");
        // unitcode必须为8位
        if ((StringUtils.isNotEmpty(unitcode) && (unitcode.length() < 8 || unitcode.length() > 10)) ||
                (StringUtils.isNotEmpty(month) && month.length() != 6) ||
                (StringUtils.isNotEmpty(month1) && month1.length() != 6) ||
                (StringUtils.isNotEmpty(month2) && month2.length() != 6) ||
                (StringUtils.isNotEmpty(testdate) && testdate.length() != 10)) {
            response.setContentType("text/plain; charset=GBK");
            response.setStatus(400);
            response.getWriter().write("非法参数!");
            return;
        }
        filterChain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void destroy() {

    }
}
