51Degrees Device Detection Java  4.1

Device detection services for 51Degrees Pipeline

cloud/ConfigureFromFile.java

This example shows how to configure a pipeline from a configuration file.

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.

The configuration file used here is:

1 <?xml version="1.0" encoding="utf-8" ?>
2 <PipelineOptions>
3  <Elements>
4  <Element>
5  <BuildParameters>
6  <EndPoint>https://cloud.51degrees.com/api/v4</EndPoint>
7  <!-- Obtain a resource key for free at
8  https://configure.51degrees.com
9  Make sure to include the 'IsMobile' property as it is used
10  by this example. -->
11  <ResourceKey>!!YOUR_RESOURCE_KEY!!</ResourceKey>
12 
13  </BuildParameters>
14  <BuilderName>CloudRequestEngine</BuilderName>
15  </Element>
16  <Element>
17  <BuilderName>DeviceDetectionCloudEngine</BuilderName>
18  </Element>
19  </Elements>
20 </PipelineOptions>
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
*
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
package fiftyone.devicedetection.examples.cloud;
import fiftyone.devicedetection.cloud.flowelements.DeviceDetectionCloudEngine;
import fiftyone.devicedetection.examples.ExampleBase;
import fiftyone.devicedetection.examples.ProgramBase;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.pipeline.core.configuration.PipelineOptions;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.engines.data.AspectPropertyMetaData;
import fiftyone.pipeline.engines.fiftyone.flowelements.FiftyOnePipelineBuilder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
public class ConfigureFromFile extends ProgramBase {
public static void main(String[] args) throws Exception {
new Example(true).run();
System.out.println("Complete. Press enter to exit.");
System.in.read();
}
public static class Example extends ExampleBase {
private final String mobileUserAgent =
"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) " +
"AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile" +
"/11D167 Safari/9537.53";
public Example(boolean printOutput) {
super(printOutput);
}
public void run() throws Exception {
// Create the configuration object
File file = new File(getClass().getClassLoader().getResource("cloud.xml").getFile());
JAXBContext jaxbContext = JAXBContext.newInstance(PipelineOptions.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
// Bind the configuration to a pipeline options instance
PipelineOptions options = (PipelineOptions) unmarshaller.unmarshal(file);
if (options.elements.get(0).buildParameters.get("ResourceKey").toString().startsWith("!!")) {
println("You need to create a resource key at " +
"https://configure.51degrees.com and paste it into this example.");
println("Make sure to include the 'IsMobile' " +
"property as it is used by this example.");
}
else {
// Create a simple pipeline to access the engine with.
Pipeline pipeline = new FiftyOnePipelineBuilder()
.buildFromConfiguration(options);
DeviceDetectionCloudEngine engine = pipeline.getElement(DeviceDetectionCloudEngine.class);
for (AspectPropertyMetaData property : engine.getProperties()) {
println(property.getName());
}
// Create a new FlowData instance ready to be populated with evidence for the
// Pipeline.
FlowData data = pipeline.createFlowData();
// Process a single HTTP User-Agent string to retrieve the values associated
// with the User-Agent for the selected properties.
data.addEvidence(
"header.user-agent",
mobileUserAgent)
.process();
// Extract the value of a property from the results.
println("IsMobile: " + data.get(DeviceData.class).getIsMobile());
// Extract the value of a property as a string from the results.
println(data.get(DeviceData.class).getBackCameraMegaPixels().getValue().toString());
}
}
}
}