import com.lvym.common.constant.OneConsts;
import com.lvym.common.exception.RequestPathInvalidException;
import com.lvym.core.strategy.OneStrategy;
import org.springframework.core.annotation.Order;

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

/**
 * 校验请求 path 是否合法
 */
@Order(OneConsts.PATH_CHECK_FILTER_ORDER)
public class PathCheckFilterForServlet implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

       // 校验本次请求 path 是否合法
       try {
          HttpServletRequest req = (HttpServletRequest) request;
          //校验请求path
          。。。。
          //自定义异常
       } catch (RequestPathInvalidException e) {
          if(OneStrategy.instance.requestPathInvalidHandle == null) {
             response.setContentType("text/plain; charset=utf-8");
             response.getWriter().print(e.getMessage());
             response.getWriter().flush();
            } else {
             OneStrategy.instance.requestPathInvalidHandle.run(e, request, response);
            }
            return;
        }
       
       // 向下执行
       chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }
    
    @Override
    public void destroy() {
    }

    
    
}