\r\n

51Degrees Device Detection Java  4.3Newer Version 4.4

Device detection services for 51Degrees Pipeline

console/GettingStartedCloud.java

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.console.helper.ExampleHelper;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.engines.data.AspectPropertyValue;
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 java.util.Objects;
import static fiftyone.common.testhelpers.LogbackHelper.configureLogback;
import static fiftyone.devicedetection.examples.console.helper.ExampleHelper.getResourceKey;
import static fiftyone.devicedetection.shared.testhelpers.FileUtils.getFilePath;
@SuppressWarnings("DuplicatedCode")
public class GettingStartedCloud {
private static final Logger logger = LoggerFactory.getLogger(GettingStartedCloud.class);
public static void main(String[] args) throws Exception {
configureLogback(getFilePath("logback.xml"));
String resourceKey = args.length > 0 ? args[0]: getResourceKey();
// prepare 'evidence' for use in pipeline (see below)
List<Map<String, String>> evidence = ExampleHelper.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");
ExampleHelper.checkResourceKey(resourceKey);
/* In this example, we use the DeviceDetectionPipelineBuilder and configure it in code.
For more information about pipelines in general see the documentation at
http://51degrees.com/documentation/4.3/_concepts__configuration__builders__index.html
Note that we wrap the creation of a pipeline in a try/resources to control its lifecycle */
try (Pipeline pipeline = new DeviceDetectionPipelineBuilder()
.useCloud(resourceKey)
.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
data.addEvidence(evidence);
// Process the flow data.
data.process();
writer.println("Results:");
/* 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(outputValue("Mobile Device", device.getIsMobile()));
writer.println(outputValue("Platform Name", device.getPlatformName()));
writer.println(outputValue("Platform Version", device.getPlatformVersion()));
writer.println(outputValue("Browser Name", device.getBrowserName()));
writer.println(outputValue("Browser Version", device.getBrowserVersion()));
}
writer.println();
writer.flush();
}
private static String outputValue(String name, AspectPropertyValue<?> value) {
/* Individual result values are wrapped with "AspectPropertyValue". This functions
similarly to a null-able type. If the value has not been set then trying to access the
"Value" property will throw an exception. AspectPropertyValue also includes the
"NoValueMessage", which describes why the value has not been set. */
return (value.hasValue() ?
String.format("\t%s: %s", name, value.getValue().toString()) :
String.format("\t%s: %s", name, value.getNoValueMessage()));
}
}