package cn.com.qmth.stmms.common.interceptor;

import java.io.IOException;
import java.util.Map;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.com.qmth.stmms.common.session.model.StmmsSession;
import cn.com.qmth.stmms.common.session.service.SessionService;
import cn.com.qmth.stmms.common.utils.RequestUtils;

/**
 * web访问基础拦截器，用于构造基础session对象
 *
 * @author LS
 */
public class SessionInterceptor extends HandlerInterceptorAdapter {

    private static Logger log = LoggerFactory.getLogger(SessionInterceptor.class);

    @Autowired
    private SessionService sessionService;

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

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        try {
            sessionService.put(request, response, RequestUtils.getSession(request));
        } catch (Exception e) {
            log.error("stmms session save faile!", e);
        }
    }

    /**
     * 强制失效session，执行请求跳转
     *
     * @param request
     * @param response
     * @param redirectURI
     * @return
     * @throws IOException
     */
    protected boolean sessionExpire(HttpServletRequest request, HttpServletResponse response, String redirectURI)
            throws IOException {
        StmmsSession session = RequestUtils.getSession(request);
        session.setInvalid(true);
        sessionService.put(request, response, session);
        response.sendRedirect(request.getContextPath() + redirectURI);
        return false;
    }

    /**
     * 强制失效session，执行请求跳转
     *
     * @param request
     * @param response
     * @param redirectURI
     * @return
     * @throws IOException
     */
    protected boolean sessionExpireAjax(HttpServletRequest request, HttpServletResponse response, String redirectURI)
            throws IOException {
        StmmsSession session = RequestUtils.getSession(request);
        session.setInvalid(true);
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, redirectURI);
        sessionService.put(request, response, session);
        return false;
    }

    /**
     * 强制执行请求跳转
     *
     * @param request
     * @param response
     * @param redirectURI
     * @return
     * @throws IOException
     */
    protected boolean redirect(HttpServletRequest request, HttpServletResponse response, String redirectURI)
            throws IOException {
        response.sendRedirect(request.getContextPath() + redirectURI);
        return false;
    }

    /**
     * 初始化session对象
     *
     * @param request
     */
    protected void buildSession(HttpServletRequest request) {
        StmmsSession session = sessionService.get(request);
        RequestUtils.setSession(request, session);
        for (Map.Entry<String, String> entry : session.getParameters()) {
            request.setAttribute(entry.getKey(), entry.getValue());
        }
    }

}
