Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

hook up geoip lookup post processor

  • Loading branch information...
commit 006085853e3c3efe9c9cd1759893dc44aa82a68c 1 parent 11defbe
@rmarianski rmarianski authored
View
10 src/community/monitoring/src/main/java/applicationContext.xml
@@ -146,10 +146,12 @@
</bean>
<bean id="monitorReverseDNSPostProcessor" class="org.geoserver.monitor.ReverseDNSPostProcessor">
</bean>
- <!-- uncomment when GeoLiteCity.dat is in place -->
-<!-- <bean id="monitorGeoIPPostProcessor" class="org.geoserver.monitor.GeoIPPostProcessor"> -->
-<!-- <constructor-arg ref="resourceLoader"/> -->
-<!-- </bean> -->
+ <bean id="monitorGeoIPPostProcessor" class="org.geoserver.monitor.GeoIPPostProcessor">
+ <constructor-arg ref="resourceLoader" index="0" />
+ <constructor-arg value="GeoLiteCity.dat" index="1" />
+ <!-- lru cache size -->
+ <constructor-arg value="100" index="2" />
+ </bean>
<!-- audit listener -->
<bean id="auditor" class="org.geoserver.monitor.AuditLogger">
View
91 src/community/monitoring/src/main/java/org/geoserver/monitor/GeoIPPostProcessor.java
@@ -6,13 +6,13 @@
import java.io.File;
import java.io.IOException;
-import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.commons.collections.map.LRUMap;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geotools.util.logging.Logging;
@@ -22,70 +22,65 @@
public class GeoIPPostProcessor implements RequestPostProcessor {
static Logger LOGGER = Logging.getLogger("org.geoserver.montior");
-
- /**
- * cached geoip lookup service
- */
- static LookupService geoIPLookup;
-
- //TODO: cache by IP address
-
- GeoServerResourceLoader loader;
- AtomicBoolean warned = new AtomicBoolean(false);
-
- public GeoIPPostProcessor(GeoServerResourceLoader loader) {
- this.loader = loader;
+
+ private final LRUMap cache;
+
+ private final LookupService geoIPLookup;
+
+ private static final String MONITOR_PATH = "monitoring";
+
+ public GeoIPPostProcessor(GeoServerResourceLoader loader, String lookupDBFileName, int cacheSize) {
+ geoIPLookup = lookupGeoIPDatabase(loader, lookupDBFileName);
+ cache = new LRUMap(cacheSize);
}
-
+
public void run(RequestData data, HttpServletRequest request, HttpServletResponse response) {
- if (data.getRemoteAddr() == null) {
- LOGGER.info("Request data did not contain ip address. Unable to perform GeoIP lookup.");
- return;
- }
-
- if (geoIPLookup == null) {
- synchronized (this) {
- if (geoIPLookup == null) {
- geoIPLookup = lookupGeoIPDatabase();
- }
- }
- }
-
if (geoIPLookup == null) {
return;
}
-
- Location loc = geoIPLookup.getLocation(data.getRemoteAddr());
- if (loc == null) {
- LOGGER.fine("Unable to obtain location for " + data.getRemoteAddr());
+
+ String remoteAddr = data.getRemoteAddr();
+ if (remoteAddr == null) {
+ LOGGER.info("Request data did not contain ip address. Unable to perform GeoIP lookup.");
return;
}
-
+
+ Location loc = null;
+ // we also cache the fact that we didn't find the remoteAddr
+ if (cache.containsKey(remoteAddr)) {
+ loc = (Location) cache.get(remoteAddr);
+ if (loc == null) {
+ return;
+ }
+ } else {
+ loc = geoIPLookup.getLocation(remoteAddr);
+ cache.put(remoteAddr, loc);
+ if (loc == null) {
+ LOGGER.fine("Unable to obtain location for " + remoteAddr);
+ return;
+ }
+ }
+
data.setRemoteCountry(loc.countryName);
data.setRemoteCity(loc.city);
data.setRemoteLat(loc.latitude);
data.setRemoteLon(loc.longitude);
}
-
- LookupService lookupGeoIPDatabase() {
+
+ private static LookupService lookupGeoIPDatabase(GeoServerResourceLoader loader, String filename) {
try {
- File f = loader.find("monitoring", "GeoLiteCity.dat");
+ File f = loader.find(MONITOR_PATH, filename);
if (f != null) {
return new LookupService(f);
}
-
- if (!warned.get()) {
- warned.set(true);
-
- String path =
- new File(loader.getBaseDirectory(), "monitoring/GeoLiteCity.dat").getAbsolutePath();
- LOGGER.warning("GeoIP database " + path + " is not available. " +
- "Please install the file to enable GeoIP lookups.");
- }
+
+ // monitor db not found, log and return null
+ File monitorDir = new File(loader.getBaseDirectory(), MONITOR_PATH);
+ String path = new File(monitorDir, filename).getAbsolutePath();
+ LOGGER.warning("GeoIP database " + path + " is not available. "
+ + "Please install the file to enable GeoIP lookups.");
return null;
-
- }
- catch (IOException e) {
+ } catch (IOException e) {
LOGGER.log(Level.WARNING, "Error occured looking up GeoIP database", e);
return null;
}
Please sign in to comment.
Something went wrong with that request. Please try again.