Skip to content

Commit

Permalink
Use proper value class instead of List<Double> in GeoIpResolverEngine…
Browse files Browse the repository at this point in the history
…#extractGeoLocationInformation()
  • Loading branch information
Jochen Schalanda committed Sep 6, 2016
1 parent c480a90 commit c7f9c11
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
Expand Up @@ -18,7 +18,7 @@


import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer; import com.codahale.metrics.Timer;
import com.google.common.collect.Lists; import com.google.auto.value.AutoValue;
import com.google.common.net.InetAddresses; import com.google.common.net.InetAddresses;
import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse; import com.maxmind.geoip2.model.CityResponse;
Expand All @@ -32,8 +32,8 @@
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;


import static com.codahale.metrics.MetricRegistry.name; import static com.codahale.metrics.MetricRegistry.name;
import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.base.Strings.isNullOrEmpty;
Expand Down Expand Up @@ -71,42 +71,35 @@ public boolean filter(Message message) {


for (Map.Entry<String, Object> field : message.getFields().entrySet()) { for (Map.Entry<String, Object> field : message.getFields().entrySet()) {
String key = field.getKey() + "_geolocation"; String key = field.getKey() + "_geolocation";
final List coordinates = extractGeoLocationInformation(field.getValue()); final Optional<Coordinates> coordinates = extractGeoLocationInformation(field.getValue());
if (coordinates.size() == 2) { // We will store the coordinates as a "lat,long" string
// We will store the coordinates as a "lat,long" string coordinates.ifPresent(c -> message.addField(key, c.latitude() + "" + c.longitude()));
final String stringGeoPoint = coordinates.get(1) + "," + coordinates.get(0);
message.addField(key, stringGeoPoint);
}
} }


return false; return false;
} }


protected List<Double> extractGeoLocationInformation(Object fieldValue) { protected Optional<Coordinates> extractGeoLocationInformation(Object fieldValue) {
final List<Double> coordinates = Lists.newArrayList();

if (!(fieldValue instanceof String) || isNullOrEmpty((String) fieldValue)) { if (!(fieldValue instanceof String) || isNullOrEmpty((String) fieldValue)) {
return coordinates; return Optional.empty();
} }


final String stringFieldValue = (String) fieldValue; final String stringFieldValue = (String) fieldValue;
final InetAddress ipAddress = this.getIpFromFieldValue(stringFieldValue); final InetAddress ipAddress = this.getIpFromFieldValue(stringFieldValue);
if (ipAddress == null) { if (ipAddress == null) {
return coordinates; return Optional.empty();
} }


try { try {
try (Timer.Context ignored = resolveTime.time()) { try (Timer.Context ignored = resolveTime.time()) {
final CityResponse response = databaseReader.city(ipAddress); final CityResponse response = databaseReader.city(ipAddress);
final Location location = response.getLocation(); final Location location = response.getLocation();
coordinates.add(location.getLongitude()); return Optional.of(Coordinates.create(location.getLatitude(), location.getLongitude()));
coordinates.add(location.getLatitude());
} }
} catch (Exception e) { } catch (Exception e) {
LOG.debug("Could not get location from IP {}", ipAddress.getHostAddress(), e); LOG.debug("Could not get location from IP {}", ipAddress.getHostAddress(), e);
return Optional.empty();
} }

return coordinates;
} }


protected InetAddress getIpFromFieldValue(String fieldValue) { protected InetAddress getIpFromFieldValue(String fieldValue) {
Expand All @@ -118,4 +111,15 @@ protected InetAddress getIpFromFieldValue(String fieldValue) {


return null; return null;
} }

@AutoValue
static abstract class Coordinates {
public abstract double latitude();

public abstract double longitude();

public static Coordinates create(double latitude, double longitude) {
return new AutoValue_GeoIpResolverEngine_Coordinates(latitude, longitude);
}
}
} }
Expand Up @@ -30,12 +30,14 @@
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;


import static com.codahale.metrics.MetricRegistry.name; import static com.codahale.metrics.MetricRegistry.name;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull; import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;


public class GeoIpResolverEngineTest { public class GeoIpResolverEngineTest {


Expand Down Expand Up @@ -88,10 +90,10 @@ public void trimFieldValueBeforeLookup() throws Exception {
public void extractGeoLocationInformation() throws Exception { public void extractGeoLocationInformation() throws Exception {
final GeoIpResolverEngine resolver = new GeoIpResolverEngine(config, metricRegistry); final GeoIpResolverEngine resolver = new GeoIpResolverEngine(config, metricRegistry);


List<Double> coordinates = resolver.extractGeoLocationInformation("1.2.3.4"); Optional<GeoIpResolverEngine.Coordinates> coordinates = resolver.extractGeoLocationInformation("1.2.3.4");
assertEquals(coordinates.size(), 2, "Should extract geo location information from public addresses"); assertTrue(coordinates.isPresent(), "Should extract geo location information from public addresses");
List<Double> coordinates2 = resolver.extractGeoLocationInformation("192.168.0.1"); Optional<GeoIpResolverEngine.Coordinates> coordinates2 = resolver.extractGeoLocationInformation("192.168.0.1");
assertEquals(coordinates2.size(), 0, "Should not extract geo location information from private addresses"); assertFalse(coordinates2.isPresent(), "Should not extract geo location information from private addresses");
} }


@Test @Test
Expand Down

0 comments on commit c7f9c11

Please sign in to comment.