Advertisement
nerru86

51Degrees device detection dealing with fake user agents

Apr 10th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. /*
  2.  * This code deals with random alphanumeric strings in the user agent. I.e. if Unknown
  3.  * is returned this code will avoid the exception generated by trying to convert such
  4.  * a string to integer or double.
  5.  */
  6. package test;
  7.  
  8. import fiftyone.mobile.detection.Match;
  9. import fiftyone.mobile.detection.Provider;
  10. import fiftyone.mobile.detection.entities.Property;
  11. import fiftyone.mobile.detection.factories.MemoryFactory;
  12. import java.io.IOException;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17.  *
  18.  * @author mike
  19.  */
  20. public class Test {
  21.  
  22.     private static final String FIFTYONEDEGREES_FILE_LOCATION = "C:\\path\\to\\51Degrees.dat";
  23.     private static final String INVALID_USER_AGENT= "Farnham";
  24.     private static final String FOD_PROPERTY = "BrowserVersion";
  25.     private static Provider provider;
  26.     private static Property property;
  27.    
  28.     public static void main(String args[]){
  29.         Test test51Degrees = new Test();
  30.         System.out.println("Trying to create provider and match");
  31.         test51Degrees.createProvider();
  32.     }
  33.  
  34.     private static Property initProperty(String propertyName) {
  35.         for (Property p : provider.dataSet.getProperties()) {
  36.             try {
  37.                 if (p.getName().equals(propertyName))
  38.                     return p;
  39.             } catch (IOException ex) {
  40.                 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  41.             }
  42.         }
  43.         return null;
  44.     }
  45.    
  46.     public void createProvider() {
  47.         try {
  48.             //Initialise provider.
  49.             provider = new Provider(MemoryFactory.create(FIFTYONEDEGREES_FILE_LOCATION));
  50.            
  51.             //Fing the property with name specified in FOD_PROPERTY in the properties list.
  52.             property = initProperty(FOD_PROPERTY);
  53.            
  54.             Match match = provider.match(INVALID_USER_AGENT);
  55.             //Check that the user agent is not a random alphanumeric string.
  56.             if (match.getMethod().toString().equals("NONE")) {
  57.                 System.out.println("Useragent does not look real.");
  58.             } else {
  59.                 //Check property has been sucessfully initialised.
  60.                 if (property != null) {
  61.                     //Fetch property name.
  62.                     String name = property.getName();
  63.                     //Fetch property value.
  64.                     if (match.getValues(name).toString().equals("Unknown") ||
  65.                             match.getValues(name).toString().equals("N/A")) {
  66.                         System.out.println("Value for property "+name+" is "+match.getValues(name).toString());
  67.                     } else {
  68.                         //Do something based on property type.
  69.                         switch(property.valueType) {
  70.                             case DOUBLE: System.out.println(name+" is " + match.getValues(name).toDouble() );
  71.                                 break;
  72.                             case STRING: System.out.println(name+" is " + match.getValues(name).toString());
  73.                                 break;
  74.                             case INT: Integer result = new Integer(match.getValues(name).toString());
  75.                                 System.out.println(name+" is " + result);
  76.                                 break;
  77.                             case BOOL: System.out.println(name+" is " + match.getValues(name).toBool());
  78.                                 break;
  79.                             default: System.out.println("It's a java script value or something else.");
  80.                                 break;
  81.                         }
  82.                     }
  83.                 } else {
  84.                     //Typo in the property name?
  85.                     System.out.println("Property "+FOD_PROPERTY+" could not be found in the dataset. Did you messpell the name of the property?");
  86.                 }
  87.             }
  88.         } catch(IOException ioe) {
  89.             ioe.printStackTrace();
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement