Class GeoIpLookup

java.lang.Object
com.mimecast.robin.smtp.security.GeoIpLookup

public class GeoIpLookup extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    private static String
     
    private static final org.apache.logging.log4j.Logger
     
    private static com.maxmind.geoip2.DatabaseReader
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor for utility class.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static void
    ensureReader(String databasePath)
    Initializes or re-initializes the database reader if the path has changed.
    static String
    getCountryCode(String ipAddress, String databasePath)
    Returns the ISO 3166-1 alpha-2 country code for the given IP address.
    (package private) static void
    setReader(com.maxmind.geoip2.DatabaseReader testReader)
    Injects a pre-built DatabaseReader for use in tests.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • log

      private static final org.apache.logging.log4j.Logger log
    • reader

      private static volatile com.maxmind.geoip2.DatabaseReader reader
    • loadedPath

      private static volatile String loadedPath
  • Constructor Details

    • GeoIpLookup

      private GeoIpLookup()
      Private constructor for utility class.
  • Method Details

    • getCountryCode

      public static String getCountryCode(String ipAddress, String databasePath)
      Returns the ISO 3166-1 alpha-2 country code for the given IP address.

      Initializes the database reader from databasePath on first call (or when the path changes). Returns null if 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 .mmdb file.
      Returns:
      ISO 3166-1 alpha-2 country code (e.g., "US"), or null if not found.
    • ensureReader

      private static void ensureReader(String databasePath)
      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-built DatabaseReader for use in tests.

      This avoids the need for an actual .mmdb file during unit testing.

      Parameters:
      testReader - The mock or pre-built database reader to use, or null to reset.