package com.hotent.base.request;

import com.hotent.base.util.ThreadLocalCleanUtil;
import com.hotent.base.util.ThreadMsgUtil;
import com.hotent.base.util.string.StringPool;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 当controller执行完后立即清空当前线程的线程变量，避免线程变量污染
 * @author jason
 */
@Component
public class CleanResourcesInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,Exception ex) throws Exception {
        // 清空线程变量  controller运行结束后执行清空线程变量方法
        Integer enter = ThreadMsgUtil.getEnter();
        if(null == enter || enter <= 0){
            ThreadLocalCleanUtil.cleanAll();
        }

    }
}
