package com.malk.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 请求拦截器: 继承 HandlerInterceptor 或实现 HandlerInterceptor
 */
@Component
public class RequestInterceptor extends HandlerInterceptorAdapter {

    // 指定类输出日志到指定文件夹
    private static final Logger logger = LoggerFactory.getLogger("point");

    // 请求拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("拦截器 ▷ 收到请求: {}", request.getServletPath());
        return super.preHandle(request, response, handler);
    }

    // 视图渲染
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.trace("拦截器 ▷ 视图渲染");
        super.postHandle(request, response, handler, modelAndView);
    }

    // 数据返回
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.trace("拦截器 ▷ 数据返回");
        super.afterCompletion(request, response, handler, ex);
    }
}
