How to parse User-Agent using Java

In this tutorial we show you how to parse a user-agent string to get the information about the browser and operating system. We compare two open source libraries with each other and list the pros and cons. Both libraries do not cover 100% of all user agents because the list of available user agents is just too extensive and new user-agents are created on a daily basis.

UA Detector

UA Detector is an open source library that parses the user-agent with the help of a data set. Currently, the latest version of this data set is from 2014. You’ll need to register/pay for the updated dataset managed by udger.

To use this library, add the following dependency to your project.

<dependency>
    <groupId>net.sf.uadetector</groupId>
    <artifactId>uadetector-resources</artifactId>
    <version>2014.10</version>
</dependency>

The API is easy to use. Just create a UserAgentStringParser with the factory method UADetectorServiceFactory.getResourceModuleParser() and execute the parse() method to parse a user-agent. This returns information about the browser and operating system like: the name, the major and minor version and producer.

package com.memorynotfound.useragent;

import net.sf.uadetector.*;
import net.sf.uadetector.service.UADetectorServiceFactory;

public class TestUaDetector {

    public static UserAgentStringParser parser = UADetectorServiceFactory.getResourceModuleParser();

    public static void main(String... args){
        String googleBot21 = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
        String macChrome = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36";
        String ie8 = "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.0; Trident/4.0; InfoPath.1; SV1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 3.0.04506.30)";

        printUa(parser.parse(googleBot21));
        printUa(parser.parse(macChrome));
        printUa(parser.parse(ie8));
    }

    public static void printUa(ReadableUserAgent agent){
        System.out.println("- - - - - - - - - - - - - - - - -");
        // type
        System.out.println("Browser type: " + agent.getType().getName());
        System.out.println("Browser name: " + agent.getName());
        VersionNumber browserVersion = agent.getVersionNumber();
        System.out.println("Browser version: " + browserVersion.toVersionString());
        System.out.println("Browser version major: " + browserVersion.getMajor());
        System.out.println("Browser version minor: " + browserVersion.getMinor());
        System.out.println("Browser version bug fix: " + browserVersion.getBugfix());
        System.out.println("Browser version extension: " + browserVersion.getExtension());
        System.out.println("Browser producer: " + agent.getProducer());

        // operating system
        OperatingSystem os = agent.getOperatingSystem();
        System.out.println("\nOS Name: " + os.getName());
        System.out.println("OS Producer: " + os.getProducer());
        VersionNumber osVersion = os.getVersionNumber();
        System.out.println("OS version: " + osVersion.toVersionString());
        System.out.println("OS version major: " + osVersion.getMajor());
        System.out.println("OS version minor: " + osVersion.getMinor());
        System.out.println("OS version bug fix: " + osVersion.getBugfix());
        System.out.println("OS version extension: " + osVersion.getExtension());

        // device category
        ReadableDeviceCategory device = agent.getDeviceCategory();
        System.out.println("\nDevice: " + device.getName());
    }
}

The previous code generates the following output.

- - - - - - - - - - - - - - - - -
Browser type: Robot
Browser name: Googlebot/2.1
Browser version: 2.1
Browser version major: 2
Browser version minor: 1
Browser version bug fix: 
Browser version extension: 
Browser producer: Google Inc.

OS Name: unknown
OS Producer: 
OS version: 
OS version major: 
OS version minor: 
OS version bug fix: 
OS version extension: 

Device: Other
- - - - - - - - - - - - - - - - -
Browser type: Browser
Browser name: Chrome
Browser version: 48.0.2564.116
Browser version major: 48
Browser version minor: 0
Browser version bug fix: 2564
Browser version extension: 
Browser producer: Google Inc.

OS Name: OS X 10.10 Yosemite
OS Producer: Apple Computer, Inc.
OS version: 10.10.5
OS version major: 10
OS version minor: 10
OS version bug fix: 5
OS version extension: 

Device: Personal computer
- - - - - - - - - - - - - - - - -
Browser type: Browser
Browser name: IE
Browser version: 8.0
Browser version major: 8
Browser version minor: 0
Browser version bug fix: 
Browser version extension: 
Browser producer: Microsoft Corporation.

OS Name: Windows 2000
OS Producer: Microsoft Corporation.
OS version: 5.0
OS version major: 5
OS version minor: 0
OS version bug fix: 
OS version extension: 

Device: Personal computer

User Agent Utils

The User Agent Utils library uses enums to represent browsers and operating systems. This means that you cannot add new user-agents dynamically thus depend on the project itself for new updates.

To use the user agent utils, add the following dependency to your project.

<dependency>
    <groupId>eu.bitwalker</groupId>
    <artifactId>UserAgentUtils</artifactId>
    <version>1.19</version>
</dependency>

You can parse the user-agent using the static UserAgent.parseUserAgentString() method. This returns information about the browser and operating system. The only downside here is that you don’t get the version number of the operating system.

package com.memorynotfound.useragent;

import eu.bitwalker.useragentutils.*;

public class TestUserAgentUtils {

    public static void main(String... args){
        String googleBot21 = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
        String macChrome = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36";
        String ie8 = "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.0; Trident/4.0; InfoPath.1; SV1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 3.0.04506.30)";

        printUa(UserAgent.parseUserAgentString(googleBot21));
        printUa(UserAgent.parseUserAgentString(macChrome));
        printUa(UserAgent.parseUserAgentString(ie8));
    }

    public static void printUa(UserAgent agent){
        System.out.println("- - - - - - - - - - - - - - - - -");

        // browser
        Browser browser = agent.getBrowser();
        System.out.println("Browser type: " + browser.getBrowserType().getName());
        System.out.println("Browser name: " + browser.getName());
        System.out.println("Browser render engine: " + browser.getRenderingEngine());

        Version version = agent.getBrowserVersion();
        if (version != null){
            System.out.println("Browser version: " + version.getVersion());
            System.out.println("Browser major version: " + version.getMajorVersion());
            System.out.println("Browser minor version: " + version.getMinorVersion());
        }
        System.out.println("Browser manufacturer: " + browser.getManufacturer().getName());

        // operating system
        OperatingSystem os = agent.getOperatingSystem();
        System.out.println("\nOS Name: " + os.getName());
        System.out.println("OS Manufacturer: " + os.getManufacturer());
        OperatingSystem group = os.getGroup();
        System.out.println("OS group: " + group.getName());

        // device type
        DeviceType deviceType = os.getDeviceType();
        System.out.println("\nDevice: " + deviceType.getName());

    }
}

The previous code generates the following output.

- - - - - - - - - - - - - - - - -
Browser type: Robot
Browser name: Robot/Spider
Browser render engine: OTHER
Browser manufacturer: Other

OS Name: Unknown
OS Manufacturer: OTHER
OS group: Unknown

Device: Unknown
- - - - - - - - - - - - - - - - -
Browser type: Browser
Browser name: Chrome 48
Browser render engine: WEBKIT
Browser version: 48.0.2564.116
Browser major version: 48
Browser minor version: 0
Browser manufacturer: Google Inc.

OS Name: Mac OS X
OS Manufacturer: APPLE
OS group: Mac OS X

Device: Computer
- - - - - - - - - - - - - - - - -
Browser type: Browser
Browser name: Internet Explorer 8
Browser render engine: TRIDENT
Browser version: 8.0
Browser major version: 8
Browser minor version: 0
Browser manufacturer: Microsoft Corporation

OS Name: Windows 2000
OS Manufacturer: MICROSOFT
OS group: Windows

Device: Computer

References

You may also like...