Class CarrierApiWeb

All Implemented Interfaces:
DeviceAtlasApi

public class CarrierApiWeb extends CarrierApi

A small extension to the core CarrierApi to permit passing of a javax.servlet.http.HttpServletRequest object.

The getProperties() and getProperty() methods in this class extract the most suitable client IP from a passed javax.servlet.http.HttpServletRequest object.

Example usage:


 <%
 CarrierApiWeb carrierApi = new CarrierApiWeb();
 // loading data can be slow - it is recommended to store loaded API in the
 // ServletContext or wrap in a Singleton.
 carrierApi.loadDataFromFile("/path/to/sample.dat");

 // get all properties from the javax.servlet.http.HttpServletRequest object
 Properties props = carrierApi.getProperties(request);

 // .... use the properties ....
 if(props.containsKey("mobileNetworkOperator") {
     String operatorName = props.get("mobileNetworkOperator").asString();
 }

 // get a single property
 Property countryProp = carrierApi.getProperty("countryCode");
 if(countryProp != null) {
    String countryCode = countryProp.asString();
 }

 
  • Constructor Details

    • CarrierApiWeb

      public CarrierApiWeb()
  • Method Details

    • getProperties

      public Properties getProperties(javax.servlet.http.HttpServletRequest request)
      Get the Carrier properties for the given javax.servlet.http.HttpServletRequest object. The most suitable client IP is extracted and used to lookup the Carrier properties.
      Parameters:
      request - The request object to find the most suitable IP from.
      Returns:
      The found properties or null of no properties found.
    • getProperty

      public Property getProperty(javax.servlet.http.HttpServletRequest request, String propertyName)
      Gets a specific Property for the IP found in the javax.servlet.http.HttpServletRequest object. The most suitable client IP is extracted from the javax.servlet.http.HttpServletRequest object. Note: if multiple properties are needed for the same IP it is more efficient to call getProperties() once rather than repeated calls to getProperty().
      Parameters:
      request - The request object to find the most suitable IP from.
      propertyName - The name of the property to return
      Returns:
      The Property or null if no property found.
      Throws:
      InvalidPropertyNameException - Thrown if the property name does not exist.