Class DeviceApiWeb

All Implemented Interfaces:
DeviceAtlasApi

public class DeviceApiWeb extends DeviceApi

A small extension to the core DeviceApi to permit passing of an javax.servlet.http.HttpServletRequest object.

When detecting a user's device in 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 (especially 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 web pages.

The getProperties() method in this class extracts the most suitable headers from a passed javax.servlet.http.HttpServletRequest object.

Example usage:

 DeviceApiWeb deviceApi = new DeviceApiWeb();
 // loading data is expensive and slow - it is recommended to store loaded API in the servlet context
 try {
     deviceApi.loadDataFromFile("/path/to/datafile.json"); 

     // get all properties from the javax.servlet.http.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 (...
 
  • Constructor Details

    • DeviceApiWeb

      public DeviceApiWeb()
      Constructs a DeviceApiWeb instance with default config. You can see the default config here: Config.
      See Also:
    • DeviceApiWeb

      public DeviceApiWeb(Config config)
      Constructs a DeviceApiWeb instance with custom config.
      Parameters:
      config - An instance of Config, you can change the DeviceAtlas API config by creating an instance or Config and setting your custom config values then passing the instance to the DeviceApiWeb constructor.
      See Also:
  • Method Details

    • getProperties

      public Properties getProperties(javax.servlet.http.HttpServletRequest request) throws ClientPropertiesException
      Get the Device properties for the given javax.servlet.http.HttpServletRequest object. The most suitable headers are extracted and used for detection and property lookup.
      If the Client-side component has been used and the client side properties exists in the client side cookie, then the client side properties will be included in the result and the device detection for certain devices such as iPhone models will be more accurate.
      You can change the API's config using Config. Depending on the config the properties may include User-Agent dynamic properties and language/locale properties.
      Parameters:
      request - The request object to find the most suitable headers.
      Returns:
      The found properties
      Throws:
      ClientPropertiesException