public class DeviceApi extends BaseDeviceAtlasApi
The DeviceAtlas device detection API provides a way to detect devices based on the HTTP headers. Using these headers, the API returns device properties such as screen width, screen height, is mobile, vendor, model etc. If you want to use the DeviceAtlas API in a servlet container then it is strongly recommended to use DeviceApiWeb instead of this library.
The DeviceApiWeb is preferred when you want to perform real-time detection of a user's device in a servlet container.
The DeviceApi is preferred when you want to perform device outside of a servlet container or from an off-line User-Agent list or header set.
getProperties(Map
To get the most accurate results: 1- Pass all of the HTTP headers. 2- Use the DeviceAtlas Client-side component and pass the result.
When you have limited data you can also use these alternatives:
getProperties(Map
getProperties(String userAgent, String clientSide).
getProperties(String userAgent).
Mapheaders = new HashMap (); headers.put("user-agent", "..."); headers.put("accept-language", "..."); // add any other available header... DeviceApi deviceApi = new DeviceApi(); try { DeviceApi.loadDataFromFile("/path/to/datafile.json"); // get all properties from the headers Properties props = DeviceApi.getProperties(headers); // .... use the properties .... if (props.contains("isMobilePhone", true)) { // it is a mobile phone } if (props.containsKey("model")) { String deviceModel = props.get("model").asString(); } if (props.containsKey("yearReleased")) { int deviceModel = props.get("yearReleased").asInteger(); } } catch (...
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
API_VERSION |
Constructor and Description |
---|
DeviceApi()
Constructs a DeviceApi instance with default config.
|
DeviceApi(Config config)
Constructs a DeviceApi instance with custom config.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
getApiVersion() |
java.lang.Integer |
getDataCreationTimestamp() |
int |
getDataRevision()
Deprecated.
use getDataCreationTimestamp() for comparing data generation
|
java.lang.String |
getDataVersion() |
java.lang.String |
getDeviceAtlasApiVersion() |
Properties |
getProperties(java.util.Map<java.lang.String,java.lang.String> headers)
Get the properties for a map of HTTP headers.
|
Properties |
getProperties(java.util.Map<java.lang.String,java.lang.String> headers,
java.lang.String clientSideProperties)
Get the properties for a set of HTTP headers merged with properties from the
Client-side component.
|
Properties |
getProperties(java.lang.String userAgent)
Get the properties for a User-Agent.
|
Properties |
getProperties(java.lang.String userAgent,
java.lang.String clientSideProperties)
Get the properties for a User-Agent merged with the client side properties
(provided by the DeviceAtlas Client-side component).
|
java.util.Set<PropertyName> |
getPropertyNames()
Get a set of all possible property names.
|
void |
loadDataFromClassPath(java.lang.String fileName)
Load the DeviceAtlas device detection data into the API from a JSON file form the
class path.
|
void |
loadDataFromFile(java.lang.String jsonDataFilePath)
Load the DeviceAtlas device detection data into the API from a JSON file.
|
void |
loadDataFromStream(java.io.InputStream inputStream)
Load the DeviceAtlas device detection data into the API from an input stream.
|
void |
setConfig(Config config)
Updates the DeviceApi instance with a new config.
|
downloadAndLoadDataFile, downloadAndLoadDataFile, getDetectionLookupCount, isDataFileReload, resetDetectionLookupCount
public static final java.lang.String API_VERSION
public DeviceApi()
Config
.Config
public DeviceApi(Config config)
config
- An instance of Config, you can change the DeviceAtlas API config
by creating an instance or Config and setting your custom config values and
then passing the instance to the DeviceApi constructor.Config
public void setConfig(Config config)
config
- An instance of Config, you can change the DeviceAtlas API config
by creating an instance or Config and setting your custom config values and
then passing the instance to this method.Config
public void loadDataFromClassPath(java.lang.String fileName) throws java.io.IOException, DataReadException, DataFileException, JsonException
fileName
- The JSON file-namejava.io.IOException
DataFileException
DataReadException
JsonException
public void loadDataFromFile(java.lang.String jsonDataFilePath) throws java.io.IOException, DataReadException, java.io.FileNotFoundException, JsonException
jsonDataFilePath
- Path to the JSON filejava.io.IOException
DataReadException
java.io.FileNotFoundException
JsonException
public void loadDataFromStream(java.io.InputStream inputStream) throws java.io.IOException, DataReadException, JsonException
inputStream
- Input streamjava.io.IOException
DataReadException
JsonException
public java.util.Set<PropertyName> getPropertyNames()
public static java.lang.String getApiVersion()
public java.lang.String getDataVersion()
public java.lang.Integer getDataCreationTimestamp()
@Deprecated public int getDataRevision()
public Properties getProperties(java.lang.String userAgent)
userAgent
- The User-Agent stringpublic Properties getProperties(java.lang.String userAgent, java.lang.String clientSideProperties) throws ClientPropertiesException
userAgent
- The User-Agent stringclientSideProperties
- The contents of the cookie containing the client side propertiesClientPropertiesException
public Properties getProperties(java.util.Map<java.lang.String,java.lang.String> headers)
headers
- Client's request HTTP headerspublic Properties getProperties(java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String clientSideProperties) throws ClientPropertiesException
headers
- Client's request HTTP headersclientSideProperties
- The contents of the cookie containing the client side propertiesClientPropertiesException
public java.lang.String getDeviceAtlasApiVersion()