Package com.maxmind.geoip
Class LookupService
- java.lang.Object
-
- com.maxmind.geoip.LookupService
-
public class LookupService extends Object
Provides a lookup service for information based on an IP address. The location of a database file is supplied when creating a lookup service instance. The edition of the database determines what information is available about an IP address. See the DatabaseInfo class for further details.The following code snippet demonstrates looking up the country that an IP address is from:
// First, create a LookupService instance with the location of the database. LookupService lookupService = new LookupService("c:\\geoip.dat"); // Assume we have a String ipAddress (in dot-decimal form). Country country = lookupService.getCountry(ipAddress); System.out.println("The country is: " + country.getName()); System.out.println("The country code is: " + country.getCode());
In general, a single LookupService instance should be created and then reused repeatedly.Tip: Those deploying the GeoIP API as part of a web application may find it difficult to pass in a File to create the lookup service, as the location of the database may vary per deployment or may even be part of the web-application. In this case, the database should be added to the classpath of the web-app. For example, by putting it into the WEB-INF/classes directory of the web application. The following code snippet demonstrates how to create a LookupService using a database that can be found on the classpath:
String fileName = getClass().getResource("/GeoIP.dat").toExternalForm() .substring(6); LookupService lookupService = new LookupService(fileName);
- Author:
- Matt Tucker (matt@jivesoftware.com)
-
-
Field Summary
Fields Modifier and Type Field Description static int
GEOIP_CABLEDSL_SPEED
static int
GEOIP_CHECK_CACHE
static int
GEOIP_CORPORATE_SPEED
static int
GEOIP_DIALUP_SPEED
static int
GEOIP_INDEX_CACHE
static int
GEOIP_MEMORY_CACHE
static int
GEOIP_STANDARD
static int
GEOIP_UNKNOWN_SPEED
-
Constructor Summary
Constructors Constructor Description LookupService(File databaseFile)
Create a new lookup service using the specified database file.LookupService(File databaseFile, int options)
Create a new lookup service using the specified database file.LookupService(String databaseFile)
Create a new lookup service using the specified database file.LookupService(String databaseFile, int options)
Create a new lookup service using the specified database file.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
_check_mtime()
void
close()
Closes the lookup service.void
countryToIP(String country, Writer out)
I2P - Write all IPv4 address ranges for the given country to out.List<String>
getAllCountryCodes()
List<String>
getAllCountryNames()
Country
getCountry(long ipAddress)
Returns the country the IP address is in.Country
getCountry(String ipAddress)
Returns the country the IP address is in.Country
getCountry(InetAddress ipAddress)
Returns the country the IP address is in.Country
getCountryV6(String ipAddress)
Returns the country the IP address is in.Country
getCountryV6(InetAddress addr)
Returns the country the IP address is in.DatabaseInfo
getDatabaseInfo()
Returns information about the database.int
getID(long ipAddress)
int
getID(String ipAddress)
int
getID(InetAddress ipAddress)
Location
getLocation(long ipnum)
Location
getLocation(String str)
Location
getLocation(InetAddress addr)
Location
getLocationV6(String str)
Location
getLocationV6(InetAddress addr)
String
getOrg(long ipnum)
String
getOrg(String str)
String
getOrg(InetAddress addr)
String
getOrgV6(String str)
String
getOrgV6(InetAddress addr)
Region
getRegion(long ipnum)
Region
getRegion(String str)
Region
getRegion(InetAddress addr)
int
last_netmask()
void
netmask(int nm)
-
-
-
Field Detail
-
GEOIP_STANDARD
public static final int GEOIP_STANDARD
- See Also:
- Constant Field Values
-
GEOIP_MEMORY_CACHE
public static final int GEOIP_MEMORY_CACHE
- See Also:
- Constant Field Values
-
GEOIP_CHECK_CACHE
public static final int GEOIP_CHECK_CACHE
- See Also:
- Constant Field Values
-
GEOIP_INDEX_CACHE
public static final int GEOIP_INDEX_CACHE
- See Also:
- Constant Field Values
-
GEOIP_UNKNOWN_SPEED
public static final int GEOIP_UNKNOWN_SPEED
- See Also:
- Constant Field Values
-
GEOIP_DIALUP_SPEED
public static final int GEOIP_DIALUP_SPEED
- See Also:
- Constant Field Values
-
GEOIP_CABLEDSL_SPEED
public static final int GEOIP_CABLEDSL_SPEED
- See Also:
- Constant Field Values
-
GEOIP_CORPORATE_SPEED
public static final int GEOIP_CORPORATE_SPEED
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
LookupService
public LookupService(String databaseFile) throws IOException
Create a new lookup service using the specified database file.- Parameters:
databaseFile
- String representation of the database file.- Throws:
IOException
- if an error occured creating the lookup service from the database file.
-
LookupService
public LookupService(File databaseFile) throws IOException
Create a new lookup service using the specified database file.- Parameters:
databaseFile
- the database file.- Throws:
IOException
- if an error occured creating the lookup service from the database file.
-
LookupService
public LookupService(String databaseFile, int options) throws IOException
Create a new lookup service using the specified database file.- Parameters:
databaseFile
- String representation of the database file.options
- database flags to use when opening the database GEOIP_STANDARD read database from disk GEOIP_MEMORY_CACHE cache the database in RAM and read it from RAM- Throws:
IOException
- if an error occured creating the lookup service from the database file.
-
LookupService
public LookupService(File databaseFile, int options) throws IOException
Create a new lookup service using the specified database file.- Parameters:
databaseFile
- the database file.options
- database flags to use when opening the database GEOIP_STANDARD read database from disk GEOIP_MEMORY_CACHE cache the database in RAM and read it from RAM- Throws:
IOException
- if an error occured creating the lookup service from the database file.
-
-
Method Detail
-
close
public void close()
Closes the lookup service.
-
getAllCountryNames
public List<String> getAllCountryNames()
- Returns:
- The list of all known country names
-
getAllCountryCodes
public List<String> getAllCountryCodes()
- Returns:
- The list of all known country codes
-
getCountryV6
public Country getCountryV6(String ipAddress)
Returns the country the IP address is in.- Parameters:
ipAddress
- String version of an IPv6 address, i.e. "::127.0.0.1"- Returns:
- the country the IP address is from.
-
getCountry
public Country getCountry(String ipAddress)
Returns the country the IP address is in.- Parameters:
ipAddress
- String version of an IP address, i.e. "127.0.0.1"- Returns:
- the country the IP address is from.
-
getCountry
public Country getCountry(InetAddress ipAddress)
Returns the country the IP address is in.- Parameters:
ipAddress
- the IP address.- Returns:
- the country the IP address is from.
-
getCountryV6
public Country getCountryV6(InetAddress addr)
Returns the country the IP address is in.- Parameters:
addr
- the IP address as Inet6Address.- Returns:
- the country the IP address is from.
-
getCountry
public Country getCountry(long ipAddress)
Returns the country the IP address is in.- Parameters:
ipAddress
- the IP address in long format.- Returns:
- the country the IP address is from.
-
countryToIP
public void countryToIP(String country, Writer out) throws IOException
I2P - Write all IPv4 address ranges for the given country to out.- Parameters:
country
- two-letter case-insensitiveout
- caller must close- Throws:
IOException
- Since:
- 0.9.48
-
getID
public int getID(String ipAddress)
-
getID
public int getID(InetAddress ipAddress)
-
getID
public int getID(long ipAddress)
-
last_netmask
public int last_netmask()
-
netmask
public void netmask(int nm)
-
getDatabaseInfo
public DatabaseInfo getDatabaseInfo()
Returns information about the database.- Returns:
- database info.
-
_check_mtime
void _check_mtime()
-
getLocation
public Location getLocation(InetAddress addr)
-
getRegion
public Region getRegion(InetAddress addr)
-
getRegion
public Region getRegion(long ipnum)
-
getLocationV6
public Location getLocationV6(InetAddress addr)
-
getLocation
public Location getLocation(long ipnum)
-
getOrg
public String getOrg(InetAddress addr)
-
getOrg
public String getOrg(long ipnum)
-
getOrgV6
public String getOrgV6(InetAddress addr)
-
-