package com.blueland.bpm.manage.util;

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;

import org.apache.commons.lang3.StringUtils;

public class CheckUserLoginFilter implements Filter {

	public CheckUserLoginFilter() {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		boolean flag = false;
		String spath = request.getServletPath();
		String userId = request.getHeader("iv-user");
		userId = "admin";
		if (StringUtils.isEmpty(userId)) {
			if (spath.indexOf("/depute/create") != -1 || spath.indexOf("/trace/") != -1
					|| spath.indexOf("resource.do") != -1 || spath.indexOf("login.do") != -1
					|| spath.indexOf("logout.do") != -1 || spath.indexOf("login.jsp") != -1
					|| spath.indexOf("applicationMgr/syncApp.do") != -1
					|| spath.indexOf("role/sync/saveOrUpdate.do") != -1
					|| spath.indexOf("role/sync/saveUserrole.do") != -1
					|| spath.indexOf("activityInfo/getActForm.do") != -1
					|| spath.indexOf("role/sync/delRole.do") != -1
					|| spath.indexOf("procDefMgr") != -1
					|| spath.indexOf("workflow/trace") != -1
					|| spath.indexOf("flow/monitor") != -1
					|| spath.indexOf("procDefModelMgr") != -1) {
				flag = true;
			} else {
				try {
					if (BaseUser.getInstance().getSessionUser((HttpServletRequest) request) != null)
						flag = true;
				} catch (Exception e) {
				}
			}
		} else {
			flag = true;
		}

		if (flag)
			filterChain.doFilter(request, response);
		else
			response.sendRedirect(request.getSession().getServletContext().getContextPath() + "/login.jsp");
	}

	public void init(FilterConfig filterconfig) throws ServletException {
	}
}
