Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: boundlessgeo/geoserver
...
head fork: boundlessgeo/geoserver
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
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
1  ...test/java/org/geoserver/monitor/DummyMonitor.java → ...main/java/org/geoserver/monitor/DummyMonitor.java
@@ -6,7 +6,6 @@
import java.util.Arrays;
import java.util.Collections;
-import java.util.Date;
import java.util.Iterator;
import java.util.List;
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;
}
View
50 src/community/monitoring/src/main/java/org/geoserver/monitor/MonitorFilter.java
@@ -6,7 +6,6 @@
import java.io.IOException;
import java.net.URLDecoder;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
@@ -33,6 +32,8 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
+import com.google.common.collect.ImmutableList;
+
public class MonitorFilter implements GeoServerFilter {
@@ -44,12 +45,15 @@
private final MessageTransport transporter;
private final ExecutorService postProcessExecutor;
+
+ private final List<RequestPostProcessor> postProcessors;
public MonitorFilter(Monitor monitor, MonitorRequestFilter requestFilter, MessageTransport transporter) {
this.monitor = monitor;
this.requestFilter = requestFilter;
this.transporter = transporter;
+ postProcessors = ImmutableList.copyOf(GeoServerExtensions.extensions(RequestPostProcessor.class));
postProcessExecutor = Executors.newCachedThreadPool();
if (monitor.isEnabled()) {
@@ -167,7 +171,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
monitor.complete();
- postProcessExecutor.execute(new PostProcessTask(monitor, data, req, resp, transporter));
+ postProcessExecutor.execute(new PostProcessTask(monitor, postProcessors, transporter, data,
+ req, resp));
if (error != null) {
if (error instanceof RuntimeException) {
@@ -223,15 +228,18 @@ String getHttpReferer(HttpServletRequest req) {
static class PostProcessTask implements Runnable {
- Monitor monitor;
- RequestData data;
- HttpServletRequest request;
- HttpServletResponse response;
+ private final Monitor monitor;
+ private final RequestData data;
+ private final HttpServletRequest request;
+ private final HttpServletResponse response;
private final MessageTransport transporter;
+ private final List<RequestPostProcessor> postProcessors;
- PostProcessTask(Monitor monitor, RequestData data, HttpServletRequest request,
- HttpServletResponse response, MessageTransport transporter) {
+ private PostProcessTask(Monitor monitor, List<RequestPostProcessor> postProcessors,
+ MessageTransport transporter, RequestData data, HttpServletRequest request,
+ HttpServletResponse response) {
this.monitor = monitor;
+ this.postProcessors = postProcessors;
this.data = data;
this.request = request;
this.response = response;
@@ -239,27 +247,17 @@ String getHttpReferer(HttpServletRequest req) {
}
public void run() {
- try {
- for (RequestPostProcessor p : GeoServerExtensions
- .extensions(RequestPostProcessor.class)) {
- try {
- p.run(data, request, response);
- }
- catch(Exception e) {
- LOGGER.log(Level.WARNING, "Post process task failed", e);
- }
+ for (RequestPostProcessor p : postProcessors) {
+ try {
+ p.run(data, request, response);
+ } catch (Exception e) {
+ LOGGER.log(Level.WARNING, "Post process task failed", e);
}
+ }
- monitor.postProcessed(data);
+ monitor.postProcessed(data);
- transporter.transport(Collections.singletonList(data));
- }
- finally {
- monitor = null;
- data = null;
- request = null;
- response = null;
- }
+ transporter.transport(Collections.singletonList(data));
}
}

No commit comments for this range

Something went wrong with that request. Please try again.