package com.yihu.hos.filter;

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 java.io.IOException;

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

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String path = httpServletRequest.getRequestURI();
        if (path.indexOf("/login") != -1
                || path.indexOf("/system/loginAction") != -1
                || path.indexOf("/error") != -1
                || path.indexOf("/monitor") != -1
                || path.indexOf("/test.jsp") != -1
                || path.indexOf("/logoutAction") != -1
                || path.indexOf(httpServletRequest.getContextPath() + "/static-dev") != -1
                || path.indexOf(httpServletRequest.getContextPath() + "/develop") != -1
                || path.indexOf(httpServletRequest.getContextPath() + "/rest") != -1
                || path.indexOf("swagger") != -1
                || path.indexOf(httpServletRequest.getContextPath() + "/v2/api-docs") != -1
                || path.indexOf("/tenant/down") != -1
                || path.indexOf(httpServletRequest.getContextPath() + "/mobile") != -1) {
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;
        }

        if (httpServletRequest.getSession(false) == null
                || httpServletRequest.getSession().getAttribute("userInfo")==null) {

            // AJAX REQUEST PROCESS
            if ("XMLHttpRequest".equalsIgnoreCase(httpServletRequest.getHeader("X-Requested-With"))) {
                httpServletResponse.setHeader("sessionStatus", "timeOut");
                httpServletResponse.getWriter().print("{}");
                httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/logoutAction");
                return;
            }
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/loginPage");
            return;
        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }

}
