51Degrees Device Detection Java  4.4

Device detection services for 51Degrees Pipeline


This example shows how to use the 51Degrees Cloud service to determine details about a device based on its User-Agent and User-Agent Client Hint HTTP header values.

You will learn:

  1. How to create a Pipeline that uses the 51Degrees cloud service
  2. How to pass input data (evidence) to the Pipeline
  3. How to retrieve the results

This example is available in full on GitHub.

To run this example, you will need to create a resource key. The resource key is used as shorthand to store the particular set of properties you are interested in as well as any associated license keys that entitle you to increased request limits and/or paid-for properties.

You can create a resource key using the 51Degrees Configurator.

package fiftyone.devicedetection.examples.console;
import fiftyone.devicedetection.DeviceDetectionPipelineBuilder;
import fiftyone.devicedetection.examples.shared.ResourceKeyHelper;
import fiftyone.devicedetection.examples.shared.ExampleTestHelper;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import static fiftyone.common.testhelpers.LogbackHelper.configureLogback;
import static fiftyone.devicedetection.examples.shared.PropertyHelper.asString;
import static fiftyone.pipeline.util.FileFinder.getFilePath;
public class GettingStartedCloud {
private static final Logger logger = LoggerFactory.getLogger(GettingStartedCloud.class);
public static void main(String[] args) throws Exception {
String resourceKey = args.length > 0 ? args[0]: null;
// prepare 'evidence' for use in pipeline (see below)
List<Map<String, String>> evidence = ExampleTestHelper.setUpEvidence();
run(resourceKey, evidence, System.out);
public static void run(String resourceKey,
List<Map<String, String>> evidenceList,
OutputStream outputStream) throws Exception {
logger.info("Running GettingStarted Cloud example");
resourceKey = ResourceKeyHelper.getOrSetTestResourceKey(resourceKey);
/* In this example, we use the DeviceDetectionPipelineBuilder and configure it in code.
For more information about pipelines in general see the documentation at
Note that we wrap the creation of a pipeline in a try/resources to control its lifecycle */
try (Pipeline pipeline = new DeviceDetectionPipelineBuilder()
.build()) {
// carry out some sample detections
for (Map<String, String> evidence : evidenceList) {
analyzeEvidence(evidence, pipeline, outputStream);
logger.info("All done");
private static void analyzeEvidence(Map<String, String> evidence,
Pipeline pipeline,
OutputStream out) throws Exception {
PrintWriter writer = new PrintWriter(out);
/* FlowData is a data structure that is used to convey information required for detection
and the results of the detection through the pipeline. Information required for
detection is called "evidence" and usually consists of a number of HTTP Header field
values, in this case represented by a Map<String, String> of header name/value entries.
FlowData is wrapped in a try/resources block in order to ensure that the unmanaged
resources allocated by the native device detection library are freed */
try (FlowData data = pipeline.createFlowData()) {
// list the evidence
writer.println("Input values:");
for (Map.Entry<String, String> entry : evidence.entrySet()) {
writer.format("\t%s: %s\n", entry.getKey(), entry.getValue());
// Add the evidence values to the flow data
// Process the flow data.
/* Now that it has been processed, the flow data will have been populated with the result.
In this case, we want information about the device, which we can get by asking for a
result matching the "DeviceData" interface. */
DeviceData device = data.get(DeviceData.class);
/* Display the results of the detection, which are called device properties. See the
property dictionary at https://51degrees.com/developers/property-dictionary for
details of all available properties. */
writer.println("\tMobile Device: " + asString(device.getIsMobile()));
writer.println("\tPlatform Name: " + asString(device.getPlatformName()));
writer.println("\tPlatform Version: " + asString(device.getPlatformVersion()));
writer.println("\tBrowser Name: " + asString(device.getBrowserName()));
writer.println("\tBrowser Version: " + asString(device.getBrowserVersion()));