package com.yihu.hos.filter;

import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.interceptor.LocalContext;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author lincl
 * @version 1.0
 * @created 2016/3/26
 */
@Component("loginFilter")
public class SessionOutTimeFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        String requestUri = request.getRequestURI();
        String contextPath = request.getContextPath();
        HttpSession session = request.getSession();

        if (requestUri.indexOf("/login") != -1
                || requestUri.indexOf("/tenantAutoFail") != -1
                || requestUri.indexOf("/tokenValidFail") != -1
                || requestUri.indexOf("/system/loginAction") != -1
                || requestUri.indexOf("/error") != -1
                || requestUri.indexOf("/test.jsp") != -1
                || requestUri.indexOf("/logoutAction") != -1
                || requestUri.indexOf(contextPath + "/static-dev") != -1
                || requestUri.indexOf(contextPath + "/develop") != -1
                || requestUri.indexOf(contextPath + "/rest") != -1
                || requestUri.indexOf(contextPath + "/process") != -1
                || requestUri.indexOf("swagger") != -1
                || requestUri.indexOf(contextPath + "/v2/api-docs") != -1
                || requestUri.indexOf("/tenant/delFile") != -1
                || requestUri.indexOf(contextPath + "/mobile") != -1) {
            filterChain.doFilter(request, response);
            return;
        }

        if (requestUri != null && !requestUri.contains("/oauth2")) {
            if (session == null || session.getAttribute("userInfo") == null) {
                // AJAX REQUEST PROCESS
                String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
                if ("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
                    response.setHeader("sessionStatus", "timeOut");
                    response.getWriter().print("{}");
                    response.sendRedirect(request.getContextPath() + "/logoutAction");
                    return;
                }
                request.getSession().setAttribute("attachment", attachment);
                response.sendRedirect(request.getContextPath() + "/" + attachment + "/loginPage");
                return;
            }
        } else if (requestUri.endsWith("/oauth2/autoLogin")) {
            // 从医疗云平台自动登录共享交换平台，则session永不过期。避免过期后返回到共享交换平台登录页。
            session.setMaxInactiveInterval(-1);
        }

        filterChain.doFilter(request, response);
    }

}
