Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This code deals with random alphanumeric strings in the user agent. I.e. if Unknown
- * is returned this code will avoid the exception generated by trying to convert such
- * a string to integer or double.
- */
- package test;
- import fiftyone.mobile.detection.Match;
- import fiftyone.mobile.detection.Provider;
- import fiftyone.mobile.detection.entities.Property;
- import fiftyone.mobile.detection.factories.MemoryFactory;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author mike
- */
- public class Test {
- private static final String FIFTYONEDEGREES_FILE_LOCATION = "C:\\path\\to\\51Degrees.dat";
- private static final String INVALID_USER_AGENT= "Farnham";
- private static final String FOD_PROPERTY = "BrowserVersion";
- private static Provider provider;
- private static Property property;
- public static void main(String args[]){
- Test test51Degrees = new Test();
- System.out.println("Trying to create provider and match");
- test51Degrees.createProvider();
- }
- private static Property initProperty(String propertyName) {
- for (Property p : provider.dataSet.getProperties()) {
- try {
- if (p.getName().equals(propertyName))
- return p;
- } catch (IOException ex) {
- Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- return null;
- }
- public void createProvider() {
- try {
- //Initialise provider.
- provider = new Provider(MemoryFactory.create(FIFTYONEDEGREES_FILE_LOCATION));
- //Fing the property with name specified in FOD_PROPERTY in the properties list.
- property = initProperty(FOD_PROPERTY);
- Match match = provider.match(INVALID_USER_AGENT);
- //Check that the user agent is not a random alphanumeric string.
- if (match.getMethod().toString().equals("NONE")) {
- System.out.println("Useragent does not look real.");
- } else {
- //Check property has been sucessfully initialised.
- if (property != null) {
- //Fetch property name.
- String name = property.getName();
- //Fetch property value.
- if (match.getValues(name).toString().equals("Unknown") ||
- match.getValues(name).toString().equals("N/A")) {
- System.out.println("Value for property "+name+" is "+match.getValues(name).toString());
- } else {
- //Do something based on property type.
- switch(property.valueType) {
- case DOUBLE: System.out.println(name+" is " + match.getValues(name).toDouble() );
- break;
- case STRING: System.out.println(name+" is " + match.getValues(name).toString());
- break;
- case INT: Integer result = new Integer(match.getValues(name).toString());
- System.out.println(name+" is " + result);
- break;
- case BOOL: System.out.println(name+" is " + match.getValues(name).toBool());
- break;
- default: System.out.println("It's a java script value or something else.");
- break;
- }
- }
- } else {
- //Typo in the property name?
- System.out.println("Property "+FOD_PROPERTY+" could not be found in the dataset. Did you messpell the name of the property?");
- }
- }
- } catch(IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement