Skip to content

Commit d12a662

Browse files
authored
Use a custom cache record for EnterpriseResponse (#125809)
1 parent 37f6ebe commit d12a662

File tree

1 file changed

+153
-109
lines changed

1 file changed

+153
-109
lines changed

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

Lines changed: 153 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -512,121 +512,167 @@ protected Map<String, Object> transform(final DomainResponse response) {
512512
}
513513
}
514514

515-
static class Enterprise extends AbstractBase<EnterpriseResponse, EnterpriseResponse> {
515+
record CacheableEnterpriseResponse(
516+
Integer countryConfidence,
517+
Boolean isInEuropeanUnion,
518+
String countryIsoCode,
519+
String countryName,
520+
String continentCode,
521+
String continentName,
522+
String regionIsoCode,
523+
String regionName,
524+
Integer cityConfidence,
525+
String cityName,
526+
String timezone,
527+
Double latitude,
528+
Double longitude,
529+
Integer accuracyRadius,
530+
String postalCode,
531+
Integer postalConfidence,
532+
Long asn,
533+
String organizationName,
534+
boolean isHostingProvider,
535+
boolean isTorExitNode,
536+
boolean isAnonymousVpn,
537+
boolean isAnonymous,
538+
boolean isPublicProxy,
539+
boolean isResidentialProxy,
540+
String domain,
541+
String isp,
542+
String ispOrganization,
543+
String mobileCountryCode,
544+
String mobileNetworkCode,
545+
String userType,
546+
String connectionType,
547+
Boolean registeredCountryIsInEuropeanUnion,
548+
String registeredCountryIsoCode,
549+
String registeredCountryName
550+
) {}
551+
552+
static class Enterprise extends AbstractBase<EnterpriseResponse, Result<CacheableEnterpriseResponse>> {
516553
Enterprise(final Set<Database.Property> properties) {
517554
super(properties, EnterpriseResponse.class, EnterpriseResponse::new);
518555
}
519556

520557
@Override
521-
protected EnterpriseResponse cacheableRecord(EnterpriseResponse response) {
522-
return response;
558+
protected Result<CacheableEnterpriseResponse> cacheableRecord(EnterpriseResponse response) {
559+
final com.maxmind.geoip2.record.Country country = response.getCountry();
560+
final Continent continent = response.getContinent();
561+
final Subdivision subdivision = response.getMostSpecificSubdivision();
562+
final com.maxmind.geoip2.record.City city = response.getCity();
563+
final Location location = response.getLocation();
564+
final Postal postal = response.getPostal();
565+
final com.maxmind.geoip2.record.Country registeredCountry = response.getRegisteredCountry();
566+
final Traits traits = response.getTraits();
567+
568+
return new Result<>(
569+
new CacheableEnterpriseResponse(
570+
country.getConfidence(),
571+
isInEuropeanUnion(country),
572+
country.getIsoCode(),
573+
country.getName(),
574+
continent.getCode(),
575+
continent.getName(),
576+
regionIsoCode(country, subdivision),
577+
subdivision.getName(),
578+
city.getConfidence(),
579+
city.getName(),
580+
location.getTimeZone(),
581+
location.getLatitude(),
582+
location.getLongitude(),
583+
location.getAccuracyRadius(),
584+
postal.getCode(),
585+
postal.getConfidence(),
586+
traits.getAutonomousSystemNumber(),
587+
traits.getAutonomousSystemOrganization(),
588+
traits.isHostingProvider(),
589+
traits.isTorExitNode(),
590+
traits.isAnonymousVpn(),
591+
traits.isAnonymous(),
592+
traits.isPublicProxy(),
593+
traits.isResidentialProxy(),
594+
traits.getDomain(),
595+
traits.getIsp(),
596+
traits.getOrganization(),
597+
traits.getMobileCountryCode(),
598+
traits.getMobileNetworkCode(),
599+
traits.getUserType(),
600+
traits.getConnectionType() == null ? null : traits.getConnectionType().toString(),
601+
isInEuropeanUnion(registeredCountry),
602+
registeredCountry.getIsoCode(),
603+
registeredCountry.getName()
604+
),
605+
traits.getIpAddress(),
606+
traits.getNetwork().toString()
607+
);
523608
}
524609

525610
@Override
526-
protected Map<String, Object> transform(final EnterpriseResponse response) {
527-
com.maxmind.geoip2.record.Country country = response.getCountry();
528-
com.maxmind.geoip2.record.Country registeredCountry = response.getRegisteredCountry();
529-
com.maxmind.geoip2.record.City city = response.getCity();
530-
Location location = response.getLocation();
531-
Continent continent = response.getContinent();
532-
Subdivision subdivision = response.getMostSpecificSubdivision();
533-
Postal postal = response.getPostal();
534-
535-
Long asn = response.getTraits().getAutonomousSystemNumber();
536-
String organizationName = response.getTraits().getAutonomousSystemOrganization();
537-
Network network = response.getTraits().getNetwork();
538-
539-
String isp = response.getTraits().getIsp();
540-
String ispOrganization = response.getTraits().getOrganization();
541-
String mobileCountryCode = response.getTraits().getMobileCountryCode();
542-
String mobileNetworkCode = response.getTraits().getMobileNetworkCode();
543-
544-
boolean isHostingProvider = response.getTraits().isHostingProvider();
545-
boolean isTorExitNode = response.getTraits().isTorExitNode();
546-
boolean isAnonymousVpn = response.getTraits().isAnonymousVpn();
547-
boolean isAnonymous = response.getTraits().isAnonymous();
548-
boolean isPublicProxy = response.getTraits().isPublicProxy();
549-
boolean isResidentialProxy = response.getTraits().isResidentialProxy();
550-
551-
String userType = response.getTraits().getUserType();
552-
553-
String domain = response.getTraits().getDomain();
554-
555-
ConnectionTypeResponse.ConnectionType connectionType = response.getTraits().getConnectionType();
611+
protected Map<String, Object> transform(final Result<CacheableEnterpriseResponse> result) {
612+
CacheableEnterpriseResponse response = result.result();
556613

557614
Map<String, Object> data = new HashMap<>();
558615
for (Database.Property property : this.properties) {
559616
switch (property) {
560-
case IP -> data.put("ip", response.getTraits().getIpAddress());
617+
case IP -> data.put("ip", result.ip());
561618
case COUNTRY_CONFIDENCE -> {
562-
Integer countryConfidence = country.getConfidence();
563-
if (countryConfidence != null) {
564-
data.put("country_confidence", countryConfidence);
619+
if (response.countryConfidence != null) {
620+
data.put("country_confidence", response.countryConfidence);
565621
}
566622
}
567623
case COUNTRY_IN_EUROPEAN_UNION -> {
568-
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
569-
if (isInEuropeanUnion != null) {
570-
data.put("country_in_european_union", isInEuropeanUnion);
624+
if (response.isInEuropeanUnion != null) {
625+
data.put("country_in_european_union", response.isInEuropeanUnion);
571626
}
572627
}
573628
case COUNTRY_ISO_CODE -> {
574-
String countryIsoCode = country.getIsoCode();
575-
if (countryIsoCode != null) {
576-
data.put("country_iso_code", countryIsoCode);
629+
if (response.countryIsoCode != null) {
630+
data.put("country_iso_code", response.countryIsoCode);
577631
}
578632
}
579633
case COUNTRY_NAME -> {
580-
String countryName = country.getName();
581-
if (countryName != null) {
582-
data.put("country_name", countryName);
634+
if (response.countryName != null) {
635+
data.put("country_name", response.countryName);
583636
}
584637
}
585638
case CONTINENT_CODE -> {
586-
String continentCode = continent.getCode();
587-
if (continentCode != null) {
588-
data.put("continent_code", continentCode);
639+
if (response.continentCode != null) {
640+
data.put("continent_code", response.continentCode);
589641
}
590642
}
591643
case CONTINENT_NAME -> {
592-
String continentName = continent.getName();
593-
if (continentName != null) {
594-
data.put("continent_name", continentName);
644+
if (response.continentName != null) {
645+
data.put("continent_name", response.continentName);
595646
}
596647
}
597648
case REGION_ISO_CODE -> {
598-
String regionIsoCode = regionIsoCode(country, subdivision);
599-
if (regionIsoCode != null) {
600-
data.put("region_iso_code", regionIsoCode);
649+
if (response.regionIsoCode != null) {
650+
data.put("region_iso_code", response.regionIsoCode);
601651
}
602652
}
603653
case REGION_NAME -> {
604-
String subdivisionName = subdivision.getName();
605-
if (subdivisionName != null) {
606-
data.put("region_name", subdivisionName);
654+
if (response.regionName != null) {
655+
data.put("region_name", response.regionName);
607656
}
608657
}
609658
case CITY_CONFIDENCE -> {
610-
Integer cityConfidence = city.getConfidence();
611-
if (cityConfidence != null) {
612-
data.put("city_confidence", cityConfidence);
659+
if (response.cityConfidence != null) {
660+
data.put("city_confidence", response.cityConfidence);
613661
}
614662
}
615663
case CITY_NAME -> {
616-
String cityName = city.getName();
617-
if (cityName != null) {
618-
data.put("city_name", cityName);
664+
if (response.cityName != null) {
665+
data.put("city_name", response.cityName);
619666
}
620667
}
621668
case TIMEZONE -> {
622-
String locationTimeZone = location.getTimeZone();
623-
if (locationTimeZone != null) {
624-
data.put("timezone", locationTimeZone);
669+
if (response.timezone != null) {
670+
data.put("timezone", response.timezone);
625671
}
626672
}
627673
case LOCATION -> {
628-
Double latitude = location.getLatitude();
629-
Double longitude = location.getLongitude();
674+
Double latitude = response.latitude;
675+
Double longitude = response.longitude;
630676
if (latitude != null && longitude != null) {
631677
Map<String, Object> locationObject = HashMap.newHashMap(2);
632678
locationObject.put("lat", latitude);
@@ -635,103 +681,101 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
635681
}
636682
}
637683
case ACCURACY_RADIUS -> {
638-
Integer accuracyRadius = location.getAccuracyRadius();
639-
if (accuracyRadius != null) {
640-
data.put("accuracy_radius", accuracyRadius);
684+
if (response.accuracyRadius != null) {
685+
data.put("accuracy_radius", response.accuracyRadius);
641686
}
642687
}
643688
case POSTAL_CODE -> {
644-
if (postal.getCode() != null) {
645-
data.put("postal_code", postal.getCode());
689+
if (response.postalCode != null) {
690+
data.put("postal_code", response.postalCode);
646691
}
647692
}
648693
case POSTAL_CONFIDENCE -> {
649-
if (postal.getConfidence() != null) {
650-
data.put("postal_confidence", postal.getConfidence());
694+
if (response.postalConfidence != null) {
695+
data.put("postal_confidence", response.postalConfidence);
651696
}
652697
}
653698
case ASN -> {
654-
if (asn != null) {
655-
data.put("asn", asn);
699+
if (response.asn != null) {
700+
data.put("asn", response.asn);
656701
}
657702
}
658703
case ORGANIZATION_NAME -> {
659-
if (organizationName != null) {
660-
data.put("organization_name", organizationName);
704+
if (response.organizationName != null) {
705+
data.put("organization_name", response.organizationName);
661706
}
662707
}
663708
case NETWORK -> {
664-
if (network != null) {
665-
data.put("network", network.toString());
709+
if (result.network() != null) {
710+
data.put("network", result.network());
666711
}
667712
}
668713
case HOSTING_PROVIDER -> {
669-
data.put("hosting_provider", isHostingProvider);
714+
data.put("hosting_provider", response.isHostingProvider);
670715
}
671716
case TOR_EXIT_NODE -> {
672-
data.put("tor_exit_node", isTorExitNode);
717+
data.put("tor_exit_node", response.isTorExitNode);
673718
}
674719
case ANONYMOUS_VPN -> {
675-
data.put("anonymous_vpn", isAnonymousVpn);
720+
data.put("anonymous_vpn", response.isAnonymousVpn);
676721
}
677722
case ANONYMOUS -> {
678-
data.put("anonymous", isAnonymous);
723+
data.put("anonymous", response.isAnonymous);
679724
}
680725
case PUBLIC_PROXY -> {
681-
data.put("public_proxy", isPublicProxy);
726+
data.put("public_proxy", response.isPublicProxy);
682727
}
683728
case RESIDENTIAL_PROXY -> {
684-
data.put("residential_proxy", isResidentialProxy);
729+
data.put("residential_proxy", response.isResidentialProxy);
685730
}
686731
case DOMAIN -> {
687-
if (domain != null) {
688-
data.put("domain", domain);
732+
if (response.domain != null) {
733+
data.put("domain", response.domain);
689734
}
690735
}
691736
case ISP -> {
692-
if (isp != null) {
693-
data.put("isp", isp);
737+
if (response.isp != null) {
738+
data.put("isp", response.isp);
694739
}
695740
}
696741
case ISP_ORGANIZATION_NAME -> {
697-
if (ispOrganization != null) {
698-
data.put("isp_organization_name", ispOrganization);
742+
if (response.ispOrganization != null) {
743+
data.put("isp_organization_name", response.ispOrganization);
699744
}
700745
}
701746
case MOBILE_COUNTRY_CODE -> {
702-
if (mobileCountryCode != null) {
703-
data.put("mobile_country_code", mobileCountryCode);
747+
if (response.mobileCountryCode != null) {
748+
data.put("mobile_country_code", response.mobileCountryCode);
704749
}
705750
}
706751
case MOBILE_NETWORK_CODE -> {
707-
if (mobileNetworkCode != null) {
708-
data.put("mobile_network_code", mobileNetworkCode);
752+
if (response.mobileNetworkCode != null) {
753+
data.put("mobile_network_code", response.mobileNetworkCode);
709754
}
710755
}
711756
case USER_TYPE -> {
712-
if (userType != null) {
713-
data.put("user_type", userType);
757+
if (response.userType != null) {
758+
data.put("user_type", response.userType);
714759
}
715760
}
716761
case CONNECTION_TYPE -> {
717-
if (connectionType != null) {
718-
data.put("connection_type", connectionType.toString());
762+
if (response.connectionType != null) {
763+
data.put("connection_type", response.connectionType);
719764
}
720765
}
721766
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
722-
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
723-
if (isInEuropeanUnion != null) {
724-
data.put("registered_country_in_european_union", isInEuropeanUnion);
767+
if (response.registeredCountryIsInEuropeanUnion != null) {
768+
data.put("registered_country_in_european_union", response.registeredCountryIsInEuropeanUnion);
725769
}
726770
}
727771
case REGISTERED_COUNTRY_ISO_CODE -> {
728-
if (registeredCountry.getIsoCode() != null) {
729-
data.put("registered_country_iso_code", registeredCountry.getIsoCode());
772+
if (response.registeredCountryIsoCode != null) {
773+
data.put("registered_country_iso_code", response.registeredCountryIsoCode);
730774
}
731775
}
732776
case REGISTERED_COUNTRY_NAME -> {
733-
if (registeredCountry.getName() != null) {
734-
data.put("registered_country_name", registeredCountry.getName());
777+
if (response.registeredCountryName != null) {
778+
data.put("registered_country_name", response.registeredCountryName);
735779
}
736780
}
737781
}

0 commit comments

Comments
 (0)