ankushs92 / Browscap4j

Finally a Java library for the Browscap project.
MIT License
10 stars 5 forks source link
browscap java-library user-agent

No longer maintained

This library is no longer maintained, and hasn't been for some time now. Use blueconic's great library. Its fast and has everything you need.

Browscap4j

travis!

A simple to use Java library for the Browscap project.Uses the browscap.csv file , which can be found here

GET

With maven :


<dependency>
    <groupId>in.ankushs</groupId>
    <artifactId>browscap4j</artifactId>
    <version>2.0-PRE</version>
</dependency>

Or gradle:


compile group: 'in.ankushs', name: 'browscap4j', version: '2.0-PRE'

The Javadocs for the latest release can be found here

USAGE

Note : Browscap4j uses Java 8 .

In order to get Browser capabilities, you need to first provide Browscap4j with the path of the csv file, like so :

File csvFile = new File(PATH_TO_BROWSCAP_CSV);
Browscap browscap = new Browscap(csvFile);

Once the data is loaded from the file into memory , any subsequent invocation of the above code would not re-load the data .

Next,just fetch the data for a User agent String ,like so :

String userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25";

BrowserCapabilities browserCapabilities = browscap.lookup(userAgent); 
String browser = browserCapabilities.getBrowser();
String deviceBrandName = browserCapabilities.getDeviceBrandName(); 
String deviceCodeName = browserCapabilities.getDeviceCodeName();
String deviceName = browserCapabilities.getDeviceName();
String deviceType = browserCapabilities.getDeviceType();
String platform = browserCapabilities.getPlatform();
String platformMaker = browserCapabilities.getPlatformMaker();
String platformVersion = browserCapabilities.getPlatformVersion();
boolean isMobile = browserCapabilities.isMobile();
boolean isTablet = browserCapabilities.isTablet();

System.out.println("Browser ::: " + browser);
System.out.println("deviceBrandName ::: " + deviceBrandName);
System.out.println("deviceCodeName ::: " + deviceCodeName);
System.out.println("deviceName ::: " + deviceName);
System.out.println("deviceType ::: " + deviceType);
System.out.println("platform ::: " + platform);
System.out.println("platformMaker ::: " + platformMaker);
System.out.println("platformVersion ::: " + platformVersion);
System.out.println("isMobile ::: " + isMobile);
System.out.println("isTablet ::: " + isTablet);

This prints :

Browser ::: Safari
deviceBrandName ::: Apple
deviceCodeName ::: iPhone
deviceName ::: iPhone
deviceType ::: Mobile Phone
platform ::: iOS
platformMaker ::: Apple Inc
platformVersion ::: 6.0
isMobile ::: true
isTablet ::: false

Some extra conveneance methods :

boolean isDesktop = browserCapabilities.isDesktop();
boolean isWindows = browserCapabilities.isWindows();
boolean isiOS = browserCapabilities.isiOS();
boolean isAndroid = browserCapabilities.isAndroid();
boolean isBot = browserCapabilities.isBot(); // Or crawler

Performance

For version '2.0-PRE', the parsing and searching of Ua strings is resolved within 1 ms. For the stable 1.4.4 version, expect result to be returned within 70ms average case.

Supported By

yourkit!

YourKit supports open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of YourKit Java Profiler and YourKit .NET Profiler,innovative and intelligent tools for profiling Java and .NET applications.