Skip to content

Commit 3942ed7

Browse files
committed
Add some utility functions for handling Maxmind geoip results (elastic#125153)
1 parent a4d0288 commit 3942ed7

File tree

1 file changed

+43
-32
lines changed

1 file changed

+43
-32
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ protected Map<String, Object> transform(final CityResponse response) {
209209
switch (property) {
210210
case IP -> data.put("ip", response.getTraits().getIpAddress());
211211
case COUNTRY_IN_EUROPEAN_UNION -> {
212-
if (country.getIsoCode() != null) {
213-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
214-
data.put("country_in_european_union", country.isInEuropeanUnion());
212+
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
213+
if (isInEuropeanUnion != null) {
214+
data.put("country_in_european_union", isInEuropeanUnion);
215215
}
216216
}
217217
case COUNTRY_ISO_CODE -> {
@@ -239,12 +239,8 @@ protected Map<String, Object> transform(final CityResponse response) {
239239
}
240240
}
241241
case REGION_ISO_CODE -> {
242-
// ISO 3166-2 code for country subdivisions.
243-
// See iso.org/iso-3166-country-codes.html
244-
String countryIso = country.getIsoCode();
245-
String subdivisionIso = subdivision.getIsoCode();
246-
if (countryIso != null && subdivisionIso != null) {
247-
String regionIsoCode = countryIso + "-" + subdivisionIso;
242+
String regionIsoCode = regionIsoCode(country, subdivision);
243+
if (regionIsoCode != null) {
248244
data.put("region_iso_code", regionIsoCode);
249245
}
250246
}
@@ -270,7 +266,7 @@ protected Map<String, Object> transform(final CityResponse response) {
270266
Double latitude = location.getLatitude();
271267
Double longitude = location.getLongitude();
272268
if (latitude != null && longitude != null) {
273-
Map<String, Object> locationObject = new HashMap<>();
269+
Map<String, Object> locationObject = HashMap.newHashMap(2);
274270
locationObject.put("lat", latitude);
275271
locationObject.put("lon", longitude);
276272
data.put("location", locationObject);
@@ -288,9 +284,9 @@ protected Map<String, Object> transform(final CityResponse response) {
288284
}
289285
}
290286
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
291-
if (registeredCountry.getIsoCode() != null) {
292-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
293-
data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion());
287+
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
288+
if (isInEuropeanUnion != null) {
289+
data.put("registered_country_in_european_union", isInEuropeanUnion);
294290
}
295291
}
296292
case REGISTERED_COUNTRY_ISO_CODE -> {
@@ -353,9 +349,9 @@ protected Map<String, Object> transform(final CountryResponse response) {
353349
switch (property) {
354350
case IP -> data.put("ip", response.getTraits().getIpAddress());
355351
case COUNTRY_IN_EUROPEAN_UNION -> {
356-
if (country.getIsoCode() != null) {
357-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
358-
data.put("country_in_european_union", country.isInEuropeanUnion());
352+
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
353+
if (isInEuropeanUnion != null) {
354+
data.put("country_in_european_union", isInEuropeanUnion);
359355
}
360356
}
361357
case COUNTRY_ISO_CODE -> {
@@ -383,9 +379,9 @@ protected Map<String, Object> transform(final CountryResponse response) {
383379
}
384380
}
385381
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
386-
if (registeredCountry.getIsoCode() != null) {
387-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
388-
data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion());
382+
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
383+
if (isInEuropeanUnion != null) {
384+
data.put("registered_country_in_european_union", isInEuropeanUnion);
389385
}
390386
}
391387
case REGISTERED_COUNTRY_ISO_CODE -> {
@@ -480,9 +476,9 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
480476
}
481477
}
482478
case COUNTRY_IN_EUROPEAN_UNION -> {
483-
if (country.getIsoCode() != null) {
484-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
485-
data.put("country_in_european_union", country.isInEuropeanUnion());
479+
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
480+
if (isInEuropeanUnion != null) {
481+
data.put("country_in_european_union", isInEuropeanUnion);
486482
}
487483
}
488484
case COUNTRY_ISO_CODE -> {
@@ -510,12 +506,8 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
510506
}
511507
}
512508
case REGION_ISO_CODE -> {
513-
// ISO 3166-2 code for country subdivisions.
514-
// See iso.org/iso-3166-country-codes.html
515-
String countryIso = country.getIsoCode();
516-
String subdivisionIso = subdivision.getIsoCode();
517-
if (countryIso != null && subdivisionIso != null) {
518-
String regionIsoCode = countryIso + "-" + subdivisionIso;
509+
String regionIsoCode = regionIsoCode(country, subdivision);
510+
if (regionIsoCode != null) {
519511
data.put("region_iso_code", regionIsoCode);
520512
}
521513
}
@@ -547,7 +539,7 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
547539
Double latitude = location.getLatitude();
548540
Double longitude = location.getLongitude();
549541
if (latitude != null && longitude != null) {
550-
Map<String, Object> locationObject = new HashMap<>();
542+
Map<String, Object> locationObject = HashMap.newHashMap(2);
551543
locationObject.put("lat", latitude);
552544
locationObject.put("lon", longitude);
553545
data.put("location", locationObject);
@@ -639,9 +631,9 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
639631
}
640632
}
641633
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
642-
if (registeredCountry.getIsoCode() != null) {
643-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
644-
data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion());
634+
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
635+
if (isInEuropeanUnion != null) {
636+
data.put("registered_country_in_european_union", isInEuropeanUnion);
645637
}
646638
}
647639
case REGISTERED_COUNTRY_ISO_CODE -> {
@@ -776,4 +768,23 @@ private RESPONSE lookup(final Reader reader, final String ipAddress) throws IOEx
776768
*/
777769
protected abstract Map<String, Object> transform(RESPONSE response);
778770
}
771+
772+
@Nullable
773+
private static Boolean isInEuropeanUnion(com.maxmind.geoip2.record.Country country) {
774+
// isInEuropeanUnion is a lowercase-b boolean so it cannot be null, but it really only makes sense for us to return a value
775+
// for this if there's actually a real country here, as opposed to an empty null-object country, so we check for an iso code first
776+
return (country.getIsoCode() == null) ? null : country.isInEuropeanUnion();
777+
}
778+
779+
@Nullable
780+
private static String regionIsoCode(final com.maxmind.geoip2.record.Country country, final Subdivision subdivision) {
781+
// ISO 3166-2 code for country subdivisions, see https://www.iso.org/iso-3166-country-codes.html
782+
final String countryIso = country.getIsoCode();
783+
final String subdivisionIso = subdivision.getIsoCode();
784+
if (countryIso != null && subdivisionIso != null) {
785+
return countryIso + "-" + subdivisionIso;
786+
} else {
787+
return null;
788+
}
789+
}
779790
}

0 commit comments

Comments
 (0)