package me.chyxion.tigon.webmvc.test.interceptor;

import lombok.extern.slf4j.Slf4j;
import me.chyxion.tigon.webmvc.TigonHandlerInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Mar 19, 2017 12:48:04
 */
@Slf4j
@Service
public class TimeHandlerInterceptor implements TigonHandlerInterceptor {

    @Override
    public String[] includePathPatterns() {
        return new String[] {"/**"};
    }

    @Override
    public String[] excludePathPatterns() {
        return new String[0];
    }

    @Override
    public PathMatcher pathMatcher() {
        return null;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        request.setAttribute("Begin", System.currentTimeMillis());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.err.println("Time: " + request.getAttribute("Begin"));
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
