package com.yaoyicloud.config;

import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

public class SessionInterceptor implements HandlerInterceptor {

    public static final Map<String, HttpSession> SESSION_MAP = new HashMap<>();

    @Override
    public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
        @NonNull Object handler)
        throws Exception {
        String relationId = request.getHeader("relationId");
        String mysession = request.getHeader("X-Session-Id");
        if (mysession == null) {
            HttpSession session = request.getSession(true);
            SESSION_MAP.put(relationId, session);
        }
        return true;
    }

    @Override
    public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
        @NonNull Object handler,
        @Nullable ModelAndView modelAndView) throws Exception {
        // 这里可以添加请求处理后的逻辑，目前示例中没有具体操作
    }

    @Override
    public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
        @NonNull Object handler, @Nullable Exception ex)
        throws Exception {
        // 这里可以添加请求完成后的逻辑，目前示例中没有具体操作
    }

}
