package cn.base.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AppSessionOutFilter implements Filter {
	protected String encoding = null;

	public void init(FilterConfig filterConfig) {}

	public void destroy() {}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter)  {

		HttpServletRequest httpRequst = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		String path=httpRequst.getContextPath();
		String head = httpRequst.getHeader("x-requested-with");
		String remoteAddr = httpRequst.getRequestURI();
		try {

			if (remoteAddr.indexOf("userloginValidation.do") == -1
				&& remoteAddr.indexOf("selectUserRole.do") == -1
				&& remoteAddr.indexOf("getMenuGroupList.do") == -1
				&& remoteAddr.indexOf("selectUser.do") == -1
				&& remoteAddr.indexOf("listXtbbxxByPage.do") == -1
				&& remoteAddr.indexOf("restartAweb.do") == -1
					&& remoteAddr.indexOf("index.jsp") == -1
					&& remoteAddr.indexOf("login.jsp") == -1
				){
//				if (head != null && (head.equalsIgnoreCase("XMLHttpRequest"))) {
					Object adminUser = httpRequst.getSession().getAttribute("userInfo");
					if (adminUser == null) {
						httpResponse.setHeader("sessionstatus", "timeout");
						httpResponse.setContentType("text/html;utf-8");
						httpResponse.setCharacterEncoding("utf-8");
						httpResponse.sendRedirect(path+"/index.jsp");
						return ;
					}else {
						filter.doFilter(request, response);
					}
				//}
			}
			else {
				filter.doFilter(request, response);
			}
		} catch (Exception e) {}
	}
}
