package https.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.common.AnnexXML;
import com.common.BlankUtil;
import com.common.Node;
public class CRTFilter implements Filter{
public static HttpSession sessionWebApp;
public static String xmlFilePath=null;
public static String webXMLPath=null;
public void destroy() {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)servletRequest;
HttpServletResponse response=(HttpServletResponse)servletResponse;
HttpSession session=request.getSession(false);
sessionWebApp=session;
String xmlFilePath=null;
if(!BlankUtil.isBlank(request)){
if(!BlankUtil.isBlank(request)){
HttpSession httpSession=request.getSession();
xmlFilePath = request.getRealPath("WEB-INF/classes/annexInternet.xml");
if(BlankUtil.isBlank(httpSession.getAttribute("xmlFilePath"))){
httpSession.setAttribute("xmlFilePath", xmlFilePath);
}
this.xmlFilePath=xmlFilePath;
if(BlankUtil.isBlank(this.webXMLPath)){
this.webXMLPath = request.getRealPath("WEB-INF/web.xml");
}
}
}
ServletContext application=session.getServletContext();
String port="";//端口号
String IP="";//IP地址,域名如localhost
String requestHost=request.getHeader("host");
String[] args=requestHost.split(":");
if(args.length==1){
port="80";
IP=args[0];
}else {
port=args[1];
IP=args[0];
}
/**获取URL地址,比如:https://www.congguan.com:8443*/
String url=request.getScheme()+"://";
url+=request.getHeader("host");
url+=request.getRequestURI();
if(request.getQueryString()!=null)
url+="?"+request.getQueryString();
session.setAttribute("userIP", IP);//这个是给HibernateFilter判断使用的
//同一个session不能够可以连接两个系统
/*if(!BlankUtil.isBlank(session.getAttribute("IP")) && !BlankUtil.isBlank(session.getAttribute("IP2"))){
session.removeAttribute("IP2");
session.removeAttribute("IP1");
}*/
Node cayNode=AnnexXML.getCayNode();
if(BlankUtil.isBlank(session.getAttribute("cayNode"))){
while(true){
if(!BlankUtil.isBlank(cayNode) && !BlankUtil.isBlank(cayNode.getUrl())){
session.setAttribute("cayNode", cayNode);
break;
}
if(BlankUtil.isBlank(cayNode) || BlankUtil.isBlank(cayNode.getUrl())){
cayNode=AnnexXML.getCayNode();
}
}
}
Node cgNode=AnnexXML.getCgNode();
if(BlankUtil.isBlank(session.getAttribute("cgNode"))){
while(true){
if(!BlankUtil.isBlank(cgNode) && !BlankUtil.isBlank(cgNode.getUrl())){
session.setAttribute("cgNode", cgNode);
break;
}
if(BlankUtil.isBlank(cgNode) || BlankUtil.isBlank(cgNode.getUrl())){
cgNode=AnnexXML.getCgNode();
}
}
}
//System.out.println(session.isNew());
Node sessionCayNode=(Node)session.getAttribute("cayNode");
Node sessionCgNode=(Node)session.getAttribute("cgNode");
if(sessionCayNode.getUrl().equals(url.trim())){
session.removeAttribute("IP");
}
if(sessionCgNode.getUrl().equals(url)){
session.removeAttribute("IP2");
}
if(IP.equals(cayNode.getIp()) && BlankUtil.isBlank(session.getAttribute("IP"))){
session.setAttribute("IP", cayNode.getIp());
String redirect=cayNode.getIndexPage();
session.removeAttribute("IP2");
response.sendRedirect(redirect);
}
else if(IP.equals(cgNode.getIp()) && BlankUtil.isBlank(session.getAttribute("IP2"))){
session.setAttribute("IP2", cgNode.getIp());
String redirect=cgNode.getIndexPage();
session.removeAttribute("IP");
response.sendRedirect(redirect);
}
/* if(IP.equals("cg") && BlankUtil.isBlank(session.getAttribute("IP2"))){
session.setAttribute("IP2", "cg");
String redirect="indexs.jsp";
response.sendRedirect(redirect);
}*/
filterChain.doFilter(servletRequest, servletResponse);
}
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CRTFilter init...................");
}
}