Package com.mimecast.robin.smtp.security
Class GeoIpLookup
java.lang.Object
com.mimecast.robin.smtp.security.GeoIpLookup
Performs country lookups using an offline MaxMind GeoLite2 database.
The DatabaseReader is initialized lazily on first use and is shared
across all calls. Use setReader(DatabaseReader) to inject a mock reader
in tests without requiring an actual .mmdb file.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static Stringprivate static final org.apache.logging.log4j.Loggerprivate static com.maxmind.geoip2.DatabaseReader -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidensureReader(String databasePath) Initializes or re-initializes the database reader if the path has changed.static StringgetCountryCode(String ipAddress, String databasePath) Returns the ISO 3166-1 alpha-2 country code for the given IP address.(package private) static voidsetReader(com.maxmind.geoip2.DatabaseReader testReader) Injects a pre-builtDatabaseReaderfor use in tests.
-
Field Details
-
log
private static final org.apache.logging.log4j.Logger log -
reader
private static volatile com.maxmind.geoip2.DatabaseReader reader -
loadedPath
-
-
Constructor Details
-
GeoIpLookup
private GeoIpLookup()Private constructor for utility class.
-
-
Method Details
-
getCountryCode
Returns the ISO 3166-1 alpha-2 country code for the given IP address.Initializes the database reader from
databasePathon first call (or when the path changes). Returnsnullif the database is unavailable, the IP cannot be resolved, or the IP is not found in the database.- Parameters:
ipAddress- The IP address to look up.databasePath- Path to the MaxMind GeoLite2-Country.mmdbfile.- Returns:
- ISO 3166-1 alpha-2 country code (e.g., "US"), or
nullif not found.
-
ensureReader
Initializes or re-initializes the database reader if the path has changed.- Parameters:
databasePath- Path to the MaxMind database file.
-
setReader
static void setReader(com.maxmind.geoip2.DatabaseReader testReader) Injects a pre-builtDatabaseReaderfor use in tests.This avoids the need for an actual
.mmdbfile during unit testing.- Parameters:
testReader- The mock or pre-built database reader to use, ornullto reset.
-