package org.strangeforest.tcb.stats.web;

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

import org.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.actuate.metrics.*;
import org.springframework.stereotype.*;

import com.google.api.client.repackaged.com.google.common.base.*;
import com.maxmind.geoip2.*;
import com.maxmind.geoip2.exception.*;
import com.maxmind.geoip2.model.*;
import com.neovisionaries.i18n.*;

@Component
public class GeoIPFilter implements Filter {

	private InputStream db;
	private DatabaseReader reader;
	@Autowired private CounterService counterService;

	private static Logger LOGGER = LoggerFactory.getLogger(GeoIPFilter.class);

	@Override public void init(FilterConfig filterConfig) throws ServletException {
		try {
			db = getClass().getResourceAsStream("/geolite/GeoLite2-Country.mmdb");
			reader = new DatabaseReader.Builder(db).build();
		}
		catch (IOException ex) {
			LOGGER.error("Error initializing GeoIP database.", ex);
		}
	}

	@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		if (reader != null) {
			String remoteAddr = ((HttpServletRequest)request).getHeader("X-Forwarded-For");
			if (Strings.isNullOrEmpty(remoteAddr))
				remoteAddr = request.getRemoteAddr();
			try {
				CountryResponse country = reader.country(InetAddress.getByName(remoteAddr));
				if (country != null) {
					String isoCode = country.getCountry().getIsoCode();
					if (!Strings.isNullOrEmpty(isoCode)) {
						CountryCode code = CountryCode.getByCode(isoCode);
						counterService.increment("counter.country." + (code != null ? code.getName() : isoCode));
					}
				}
			}
			catch (AddressNotFoundException ex) {
				LOGGER.debug("Error geo-locating country.", ex);
			}
			catch (GeoIp2Exception ex) {
				LOGGER.error("Error geo-locating country.", ex);
			}
		}
		chain.doFilter(request, response);
	}

	@Override public void destroy() {
		if (reader != null) {
			try {
				try {
					reader.close();
				}
				finally {
					db.close();
				}
			}
			catch (IOException ex) {
				LOGGER.error("Error closing GeoIP database.", ex);
			}
		}
	}
}

@@ -466,42 +466,8 @@
 l.*;
-%0Aimport com.neovisionaries.i18n.*;
 %0A%0A@C
@@ -1184,16 +1184,26 @@
 null) %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09Strin
@@ -1280,16 +1280,17 @@
 r%22);%0A%09%09%09
+%09
 if (Stri
@@ -1320,16 +1320,17 @@
 eAddr))%0A
+%09
 %09%09%09%09remo
@@ -1362,29 +1362,139 @@
 eAddr();%0A%09%09%09
-try %7B
+%09int commaPos = remoteAddr.indexOf(',');%0A%09%09%09%09if (commaPos %3E 0)%0A%09%09%09%09%09remoteAddr = remoteAddr.substring(0, commaPos);
 %0A%09%09%09%09Country
@@ -1602,22 +1602,26 @@
 %09String 
-isoCod
+countryNam
 e = coun
@@ -1644,14 +1644,11 @@
 .get
-IsoCod
+Nam
 e();
@@ -1684,76 +1684,21 @@
 pty(
-isoCode)) %7B%0A%09%09%09%09%09%09CountryCode code = CountryCode.getByCode(isoCode);
+countryName))
 %0A%09%09%09
@@ -1750,58 +1750,21 @@
 %22 + 
-(
 co
-de != null ? code.getName() : isoCode));%0A%09%09%09%09%09%7D
+untryName);
 %0A%09%09%09
@@ -1884,14 +1884,8 @@
 ch (
-GeoIp2
 Exce

Fix GeoIPFilter