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) throws IOException, ServletException {
		HttpServletRequest httpRequst = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		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
				){
				if (head != null && (head.equalsIgnoreCase("XMLHttpRequest"))) {
					Object adminUser = httpRequst.getSession().getAttribute("userInfo");
					if (adminUser == null) {
						httpResponse.setHeader("sessionstatus", "timeout");
						httpResponse.sendError(999); 
						return;
					}
				}
			}
			filter.doFilter(request, response);
		} catch (Exception e) {}
	}
}
