package com.liangjian.dataplatform.member.interceptor;

import com.liangjian.dataplatform.member.entity.token.DynamicParamGetter;
import com.liangjian.dataplatform.member.util.dynamic.DynamicParams;
import com.liangjian.dataplatform.member.util.dynamic.DynamicParamsWorker;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * Created by huangziqi on 2019/12/17
 *
 * 用于解析Get请求时携带的参数，动态查询的拦截器
 */
@Component
public class DynamicParamAnalyser implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getMethod().equals("GET")) {
            DynamicParams params = DynamicParamsWorker.analyze(request);
            DynamicParamGetter.DynamicParams.set(params);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        DynamicParamGetter.DynamicParams.remove();
    }
}
