public class CarrierApi extends BaseDeviceAtlasApi
The main class for the Carrier Detection API. Used to load the data file and perform lookups using IPv4 and IPv6 addresses. If you want to use the DeviceAtlas Carrier API in a servlet container then it is strongly recommended to use CarrierApiWeb instead of this library.
Please note that it is advisable to load only a single instance of this class to avoid multiple loadings of the data file. This can be done in a Web Container by storing a reference to it in the ServletContext.
String ip = "62.40.34.220"; CarrierApi carrierApi = new CarrierApi(); carrierApi.loadDataFromFile("/path/to/sample.dat"); // get all properties Properties props = carrierApi.getProperties(ip); // .... use the properties .... if (props.containsKey("networkOperator")) { Property property = (Property)props.get("networkOperator"); String operatorName = property.asString(); System.out.println("networkOperator: "+operatorName); } // get a single property Property mccProp = carrierApi.getProperty(ip, "mcc"); if (mccProp != null) { String mcc = mccProp.asString(); System.out.println("MCC: " + mcc); }
Please see the code in the Examples directory for additional samples.
Modifier and Type | Field and Description |
---|---|
static java.lang.String[] |
HEADERS_TO_CHECK
A list of HTTP headers to choose the original client IP address from.
|
Constructor and Description |
---|
CarrierApi() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
getApiVersion()
Returns the version of the Carrier API.
|
java.lang.String |
getDataFileCopyright()
Returns the data file copyright text.
|
java.lang.String |
getDataFileCreationDate()
Returns the data file creation date in ISO8601 format.
|
java.lang.String |
getDataFileVersion()
Returns the version of the data file.
|
java.lang.String |
getDeviceAtlasApiVersion() |
java.lang.String |
getIp(java.util.Map keyVals)
Get the most suitable IP address from the given keyVals set of HTTP
headers.
|
Properties |
getProperties(java.util.Map keyVals)
Get the Carrier properties for a given Map of HTTP Headers.
|
Properties |
getProperties(java.lang.String ip)
Get the Carrier properties for a given IP address.
|
Property |
getProperty(java.util.Map keyVals,
java.lang.String propertyName)
Try to obtain a specific property for a given Map of HTTP Headers.
|
Property |
getProperty(java.lang.String ip,
java.lang.String propertyName)
Try to obtain a specific property for a given IP address.
|
java.util.Set |
getPropertyNames()
A Set of all the possible property names.
|
static boolean |
isPublicIp(java.lang.String ipStr)
An IP address is considered public if it is not in any of the following
ranges:
|
void |
loadDataFromClassPath(java.lang.String fileName)
Load the data file from the class path.
|
void |
loadDataFromFile(java.lang.String path)
Load the data file from the provided path.
|
void |
loadDataFromStream(java.io.InputStream inputStream)
Load the data from an input stream.
|
downloadAndLoadDataFile, downloadAndLoadDataFile, getDetectionLookupCount, isDataFileReload, resetDetectionLookupCount
public static final java.lang.String[] HEADERS_TO_CHECK
public void loadDataFromFile(java.lang.String path) throws DataLoadingException
path
- The path on disk to the data file.DataLoadingException
- thrown when there is a problem with the data file.public void loadDataFromClassPath(java.lang.String fileName) throws DataLoadingException
fileName
- The name of the file on the class pathDataLoadingException
- thrown when there is a problem with the data file.public void loadDataFromStream(java.io.InputStream inputStream) throws DataLoadingException
inputStream
- Input streamDataLoadingException
- thrown when there is a problem with the data file.public java.lang.String getDataFileCopyright()
public java.lang.String getDataFileCreationDate()
public java.lang.String getDataFileVersion()
public static java.lang.String getApiVersion()
public Properties getProperties(java.lang.String ip)
ip
- The IP address to find carrier properties for.public Properties getProperties(java.util.Map keyVals)
Get the Carrier properties for a given Map of HTTP Headers. The API will choose the most appropriate IP to use.
The following headers are checked in order to find the most suitable IP address.
keyVals
- A map of HTTP header names to HTTP header values. Both must be Strings.public Property getProperty(java.lang.String ip, java.lang.String propertyName) throws InvalidPropertyNameException
Try to obtain a specific property for a given IP address.
Note: if multiple properties are needed for the same IP it is more efficient to call getProperties() once rather than repeated calls to getProperty().
ip
- The IP address to find carrier properties for.propertyName
- The name of the property to returnInvalidPropertyNameException
- Thrown if the property name does not exist.public Property getProperty(java.util.Map keyVals, java.lang.String propertyName) throws InvalidPropertyNameException
Try to obtain a specific property for a given Map of HTTP Headers. The API will choose the most appropriate IP to use.
Note: if multiple properties are needed for the same IP it is more efficient to call getProperties() once rather than repeated calls to getProperty().
keyVals
- A map of HTTP header name to HTTP header value. Both must be Strings.propertyName
- The name of the property to returnInvalidPropertyNameException
- Thrown if the property name does not exist.public java.util.Set getPropertyNames()
public java.lang.String getIp(java.util.Map keyVals)
keyVals
- A map of HTTP header name to HTTP header valuepublic static boolean isPublicIp(java.lang.String ipStr)
1) any local address IP: 0 2) a local loopback address range: 127/8 3) a site local address i.e. IP is in any of the ranges: range: 10/8 range: 172.16/12 range: 192.168/16 4) a link local address range: 169.254/16
ipStr
- The IP address to checkpublic java.lang.String getDeviceAtlasApiVersion()
Copyright © 2023 DeviceAtlas Limited. All rights reserved.