package com.dianping.cat.servlet;

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 org.unidal.lookup.util.StringUtils;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Metric;

public class CdnFilter implements Filter {

	private static final String DI_LIAN = "DiLian";

	private static final String WANG_SU = "WangSu";

	private static final String TENG_XUN = "TengXun";

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
	      ServletException {
		try {
			HttpServletRequest httpServletRequest = (HttpServletRequest) request;
			String vip = queryVip(httpServletRequest);
			String sourceIp = querySourceIp(httpServletRequest);

			if (StringUtils.isNotEmpty(sourceIp) && StringUtils.isNotEmpty(vip)) {
				Metric metric = Cat.getProducer().newMetric("cdn", vip + ":" + sourceIp);

				metric.setStatus("C");
				metric.addData(String.valueOf(1));
			}
		} catch (Exception e) {
			Cat.logError(e);
		}
		chain.doFilter(request, response);
	}

	private String querySourceIp(HttpServletRequest request) {
		return filterXForwardedForIP(request.getHeader("x-forwarded-for"));
	}

	private String queryVip(HttpServletRequest request) {
		String serverName = request.getServerName();

		if (serverName != null) {
			if (serverName.contains("s1.dpfile.com")) {
				return DI_LIAN;
			}
			if (serverName.contains("i1.dpfile.com") || serverName.contains("i3.dpfile.com")
			      || serverName.contains("t2.dpfile.com")) {
				return DI_LIAN;
			}
			if (serverName.contains("s2.dpfile.com")) {
				return WANG_SU;
			}
			if (serverName.contains("i2.dpfile.com") || serverName.contains("t1.dpfile.com")
			      || serverName.contains("t3.dpfile.com")) {
				return WANG_SU;
			}
			if (serverName.contains("s3.dpfile.com")) {
				return TENG_XUN;
			}
		}
		return null;
	}

	private String filterXForwardedForIP(String ip) {
		if (ip == null || ip.trim().length() == 0) {
			return null;
		} else {
			String[] subIps = ip.split(",");
			for (int i = 0; i < subIps.length; i++) {
				String subIp = subIps[i];
				if (subIp == null || subIp.trim().length() == 0) {
					continue;
				} else {
					subIp = subIp.trim();
					if (subIp.startsWith("192.168.") || subIp.startsWith("10.") || "127.0.0.1".equals(subIp)) {
						continue;
					} else if (subIp.startsWith("172.")) {
						String[] iptabs = subIp.split("\\.");
						int tab2 = Integer.parseInt(iptabs[1]);

						if (tab2 >= 16 && tab2 <= 31) {
							continue;
						} else {
							return subIp;
						}
					} else {
						return subIp;
					}
				}
			}
			return null;
		}
	}

	@Override
	public void destroy() {
	}

}

@@ -2347,16 +2347,67 @@
 t(%22,%22);%0A
+%09%09%09int length = subIps.length;%0A%09%09%09int index = -1;%0A%0A
 %09%09%09for (
@@ -2421,23 +2421,16 @@
  0; i %3C 
-subIps.
 length; 
@@ -2897,36 +2897,47 @@
 se %7B%0A%09%09%09%09%09%09%09
-return subIp
+index = i;%0A%09%09%09%09%09%09%09break
 ;%0A%09%09%09%09%09%09%7D%0A%09%09
@@ -2958,40 +2958,138 @@
 %09%09%09%09
-return subIp;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A
+index = i;%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09if (index %3E -1 && index + 1 %3C= length) %7B%0A%09%09%09%09return subIps%5Bindex + 1%5D;%0A%09%09%09%7D else %7B%0A%09
 %09%09%09r
@@ -3102,17 +3102,23 @@
 null;%0A%09%09
-%7D
+%09%7D%0A%09%09%7D%0A
 %0A%09%7D%0A%0A%09@O

modify the cdnfilter