48
48
import java .util .stream .Collectors ;
49
49
50
50
public class GeoIPFilter implements Closeable {
51
+
52
+ // This exception could raise during the processing of datapoint with custom fields, check out
53
+ // for more details https://github.com/logstash-plugins/logstash-filter-geoip/issues/226
54
+ static class GeoIp2InvalidCustomFieldException extends GeoIp2Exception {
55
+ public GeoIp2InvalidCustomFieldException (Throwable cause ) {
56
+ super ("invalid custom field" , cause );
57
+ }
58
+ }
59
+
51
60
private static final Logger logger = LogManager .getLogger ();
52
61
private final String sourceField ;
53
62
private final String targetField ;
@@ -152,7 +161,7 @@ public boolean handleEvent(RubyEvent rubyEvent) {
152
161
throw new IllegalArgumentException ("Expected input field value to be String or List type" );
153
162
}
154
163
155
- if (ip .trim ().isEmpty ()){
164
+ if (ip .trim ().isEmpty ()) {
156
165
return false ;
157
166
}
158
167
@@ -224,7 +233,12 @@ private boolean applyGeoData(Map<Field, Object> geoData, Event event) {
224
233
}
225
234
226
235
private Map <Field ,Object > retrieveCityGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
227
- CityResponse response = databaseReader .city (ipAddress );
236
+ CityResponse response ;
237
+ try {
238
+ response = databaseReader .city (ipAddress );
239
+ } catch (NullPointerException e ) {
240
+ throw new GeoIp2InvalidCustomFieldException (e );
241
+ }
228
242
Country country = response .getCountry ();
229
243
City city = response .getCity ();
230
244
Location location = response .getLocation ();
@@ -337,7 +351,12 @@ private Map<Field,Object> retrieveCityGeoData(InetAddress ipAddress) throws GeoI
337
351
}
338
352
339
353
private Map <Field ,Object > retrieveCountryGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
340
- CountryResponse response = databaseReader .country (ipAddress );
354
+ CountryResponse response ;
355
+ try {
356
+ response = databaseReader .country (ipAddress );
357
+ } catch (NullPointerException e ) {
358
+ throw new GeoIp2InvalidCustomFieldException (e );
359
+ }
341
360
Country country = response .getCountry ();
342
361
Continent continent = response .getContinent ();
343
362
Map <Field , Object > geoData = new EnumMap <>(Field .class );
@@ -372,7 +391,12 @@ private Map<Field,Object> retrieveCountryGeoData(InetAddress ipAddress) throws G
372
391
}
373
392
374
393
private Map <Field , Object > retrieveIspGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
375
- IspResponse response = databaseReader .isp (ipAddress );
394
+ IspResponse response ;
395
+ try {
396
+ response = databaseReader .isp (ipAddress );
397
+ } catch (NullPointerException e ) {
398
+ throw new GeoIp2InvalidCustomFieldException (e );
399
+ }
376
400
377
401
Map <Field , Object > geoData = new EnumMap <>(Field .class );
378
402
for (Field desiredField : this .desiredFields ) {
@@ -411,7 +435,12 @@ private Map<Field, Object> retrieveIspGeoData(InetAddress ipAddress) throws GeoI
411
435
}
412
436
413
437
private Map <Field , Object > retrieveAsnGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
414
- AsnResponse response = databaseReader .asn (ipAddress );
438
+ AsnResponse response ;
439
+ try {
440
+ response = databaseReader .asn (ipAddress );
441
+ } catch (NullPointerException e ) {
442
+ throw new GeoIp2InvalidCustomFieldException (e );
443
+ }
415
444
Network network = response .getNetwork ();
416
445
417
446
Map <Field , Object > geoData = new EnumMap <>(Field .class );
@@ -444,7 +473,12 @@ private Map<Field, Object> retrieveAsnGeoData(InetAddress ipAddress) throws GeoI
444
473
}
445
474
446
475
private Map <Field , Object > retrieveDomainGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
447
- DomainResponse response = databaseReader .domain (ipAddress );
476
+ DomainResponse response ;
477
+ try {
478
+ response = databaseReader .domain (ipAddress );
479
+ } catch (NullPointerException e ) {
480
+ throw new GeoIp2InvalidCustomFieldException (e );
481
+ }
448
482
Map <Field , Object > geoData = new EnumMap <>(Field .class );
449
483
for (Field desiredField : this .desiredFields ) {
450
484
switch (desiredField ) {
@@ -459,7 +493,12 @@ private Map<Field, Object> retrieveDomainGeoData(InetAddress ipAddress) throws G
459
493
}
460
494
461
495
private Map <Field , Object > retrieveEnterpriseGeoData (InetAddress ipAddress ) throws GeoIp2Exception , IOException {
462
- EnterpriseResponse response = databaseReader .enterprise (ipAddress );
496
+ EnterpriseResponse response ;
497
+ try {
498
+ response = databaseReader .enterprise (ipAddress );
499
+ } catch (NullPointerException e ) {
500
+ throw new GeoIp2InvalidCustomFieldException (e );
501
+ }
463
502
464
503
Map <Field , Object > geoData = new EnumMap <>(Field .class );
465
504
Country country = response .getCountry ();
@@ -567,7 +606,12 @@ private Map<Field, Object> retrieveEnterpriseGeoData(InetAddress ipAddress) thro
567
606
}
568
607
569
608
private Map <Field , Object > retrieveAnonymousIpGeoData (final InetAddress ipAddress ) throws GeoIp2Exception , IOException {
570
- AnonymousIpResponse response = databaseReader .anonymousIp (ipAddress );
609
+ AnonymousIpResponse response ;
610
+ try {
611
+ response = databaseReader .anonymousIp (ipAddress );
612
+ } catch (NullPointerException e ) {
613
+ throw new GeoIp2InvalidCustomFieldException (e );
614
+ }
571
615
572
616
Map <Field , Object > geoData = new EnumMap <>(Field .class );
573
617
boolean isHostingProvider = response .isHostingProvider ();
0 commit comments