public class DeviceApiWeb extends DeviceApi
A small extension to the core DeviceApi to permit passing of an HttpServletRequest object. The DeviceApiWeb, the DeviceApi and the DeviceAtlas common jar files must be included.
When detecting user's device as real-time on a website in a servlet container it is strongly recommend to use the DeviceApiWeb library and getProperties as shown bellow.
The getProperties of DeviceApiWeb, automatically detects the availability of the client-side properties and will use them for detection. If you want to have a more accurate detection (specially for apple devices) or have the client side properties included in the property sets, simply include the DeviceAtlas client-side component JavaScript library in the pages.
The getProperties() method in this class extracts the most suitable headers from a passed HttpServletRequest object.
<% DeviceApiWeb deviceApi = new DeviceApiWeb(); // loading data can be slow - it is recommended to store loaded API in the // ServletContext or wrap in a Singleton. try { deviceApi.loadDataFromFile("/path/to/datafile.json"); // get all properties from the HTTPServletRequest object Properties properties = deviceApi.getProperties(request); // .... use the properties .... if (properties.contains("mobileDevice", true)) { if (properties.containsKey("model")) { String deviceModel = properties.get("model").asString(); } } } catch (... %>
API_VERSION
Constructor and Description |
---|
DeviceApiWeb()
Constructs a DeviceApiWeb instance with default configs.
|
DeviceApiWeb(Config config)
Constructs a DeviceApiWeb instance with custom configs.
|
Modifier and Type | Method and Description |
---|---|
Properties |
getProperties(javax.servlet.http.HttpServletRequest request)
Get the Device properties for the given HttpServletRequest object.
|
getApiVersion, getDataCreationTimestamp, getDataRevision, getDataVersion, getProperties, getProperties, getProperties, getProperties, getPropertyNames, loadDataFromClassPath, loadDataFromFile, loadDataFromStream
public DeviceApiWeb()
Config
.Config
public DeviceApiWeb(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 Properties getProperties(javax.servlet.http.HttpServletRequest request) throws ClientPropertiesException
Config
.
Depending on the configs the properties may include user-agent dynamic
properties and language/locale properties.request
- The request object to find the most suitable headers.ClientPropertiesException
Copyright © 2021 DeviceAtlas Limited. All rights reserved.