public class DeviceApi extends Object
The DeviceAtlas device detection API provides a way to detect devices based on the HTTP headers. Using the 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 get the properties from a real-time detection on user's device in a servlet container.
The DeviceApi is preferred when you want to get the properties outside a servlet container or from an off-line user-agent list or header set.
getProperties(Map
To get the most accurate results: 1- Pass the whole HTTP headers. 2- Use the DeviceAtlas client-side-component and pass the result.
When you have limited data you can also use this 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(); } } catch (...
Modifier and Type | Field and Description |
---|---|
static String |
API_VERSION
Api version
|
Constructor and Description |
---|
DeviceApi()
Constructs a DeviceApi instance with default configs.
|
DeviceApi(Config config)
Constructs a DeviceApi instance with custom configs.
|
Modifier and Type | Method and Description |
---|---|
static String |
getApiVersion()
Get DeviceApi version.
|
Integer |
getDataCreationTimestamp()
Get the device data creation (JSON file) timestamp.
|
int |
getDataRevision()
Get the device data generation revision.
|
String |
getDataVersion()
Get the device data (JSON file) version.
|
Properties |
getProperties(Map<String,String> headers)
Get the properties for a set of HTTP headers.
|
Properties |
getProperties(Map<String,String> headers,
String clientSideProperties)
Get the properties for a set of HTTP headers merged with properties from the
client side component.
|
Properties |
getProperties(String userAgent)
Get the properties for a User-Agent.
|
Properties |
getProperties(String userAgent,
String clientSideProperties)
Get the properties for a User-Agent merged with the client-side properties
(provided by the DeviceAtlas client side component).
|
Set<PropertyName> |
getPropertyNames()
Get a set of all possible property names.
|
void |
loadDataFromClassPath(String fileName)
Load the DeviceAtlas device detection data into the API from a JSON file form the
class path.
|
void |
loadDataFromFile(String jsonDataFilePath)
Load the DeviceAtlas device detection data into the API from a JSON file.
|
void |
loadDataFromStream(InputStream inputStream)
Load the DeviceAtlas device detection data into the API from an input stream.
|
public static final String API_VERSION
public DeviceApi()
Config
.Config
public DeviceApi(Config config)
config
- An instance of Config, you can change the DeviceAtlas API configs
by creating an instance or Config and setting your custom config values
then passing the instance to the DeviceApi constructor.Config
public void loadDataFromClassPath(String fileName) throws IOException, DataReadException, DataFileException, JsonException
fileName
- The JSON file-nameIOException
DataFileException
DataReadException
JsonException
public void loadDataFromFile(String jsonDataFilePath) throws IOException, DataReadException, FileNotFoundException, JsonException
jsonDataFilePath
- Path to the JSON fileIOException
DataFileException
DataReadException
JsonException
FileNotFoundException
public void loadDataFromStream(InputStream inputStream) throws IOException, DataReadException, JsonException
inputStream
- Input streamIOException
DataReadException
JsonException
public Set<PropertyName> getPropertyNames()
public static String getApiVersion()
public String getDataVersion()
public Integer getDataCreationTimestamp()
public int getDataRevision()
public Properties getProperties(String userAgent)
userAgent
- The User-Agent stringpublic Properties getProperties(String userAgent, String clientSideProperties) throws ClientPropertiesException
userAgent
- The User-Agent stringclientSideProperties
- The contents of the cookie containing the client side propertiesClientPropertiesException
public Properties getProperties(Map<String,String> headers)
headers
- Client's request HTTP headerspublic Properties getProperties(Map<String,String> headers, String clientSideProperties) throws ClientPropertiesException
headers
- Client's request HTTP headersclientSideProperties
- The contents of the cookie containing the client side propertiesClientPropertiesException
Copyright © 2021 DeviceAtlas Limited. All rights reserved.