user-agent detection
test database library API blog GitHub

User-Agent detection in Java

Places to be

UserAgentDetector

This library is meant to give meaning to so-called user-agent strings. Out of it, it can give the OS, the Browser, the device, the country and the language of the user. However, chances are that only a few of these variables will be extracted.

Moreover, users visiting any website can spoof this user-agent string trivially. So don't use it for anything too serious.

What use can this library have then?

You might want to give it the use you see fit. I use it to build statistics out of my access_log files, so I can see what percentage of my visitors use IE or Firefox, which use Linux, what percentage Chrome OS has so far, has Windows Phone beaten iOS yet (lol), etc. Just out of curiosity.

Project Status

The project is in an early release phase. It works, but:

  • Some major API changes are upcoming in the following weeks.
  • I need to update the detection of the most recent mobile devices (notably Android and Windows Mobile). For now, they are reported as generic Android/WinMo mobile devices, so it's not that bad.

License

This software is licensed under the very popular WTFPL. Do what you want with it.

Performance

The current test database (above 24k strings as of 01/2015) process each user-agent a little under 0.09ms on average on an i7-3537U CPU @ 2.00GHz. This time obviously varies from one string to another. If you want to see for yourself, check out the project, edit test.sh and replace the test command with perf. In other words, launch:

java -cp build/user-agent-detector.jar net.pieroxy.ua.tooling.UserAgentTester perf test-data/database.gz

from the root of your clone.

Usage

Just call the main class static method and dig through the results:

import net.pieroxy.ua.detection.*; public class Test { public static void main(String[]args) { UserAgentDetectionResult res = new UserAgentDetector().parseUserAgent(args[0]); System.out.println("Is the browser running the Gecko rendering engine: " + res.getBrowser().getFamily().isGecko()); System.out.println("Is the operating system running Linux: " + res.getOperatingSystem().getFamily().isLinuxKernel()); System.out.println("The operating system is " + res.getOperatingSystem().getDescription()); System.out.println("The device manufacturer is " + res.getDevice().getManufacturer()); } }

Changelog

The changelog can be found on GitHub.

How to contribute

I will not merge pull requests, as most of this code and tests are generated. While I can still use them, it doesn't seem to be the best way to contribute in most cases. What you can do is open issues if you find problems or would like to suggest improvements. You can also leave a message on the blog if you just want to ask something.