\r\n

51Degrees Device Detection Java  4.4

Device detection services for 51Degrees Pipeline

GettingStartedWebOnPrem.java

This example is available in full on GitHub.

This example requires a local data file. The free 'Lite' data file can be acquired by pulling the git submodules under this repository (run `git submodule update --recursive`) or from the device-detection-data GitHub repository.

The Lite data file is only used for illustration, and has limited accuracy and capabilities. Find out about the more capable data files that are available on our pricing page

Overview

The PipelineFilter is used to intercept requests and perform device detection. The results will be stored in the HttpServletRequest object. The filter will also handle setting response headers (e.g. Accept-CH for User-Agent Client Hints) and serving requests for client-side JavaScript and JSON resources.

The results of detection can be accessed by using a FlowDataProvider which is responsible for managing the lifecycle of the flowData - do NOT dispose

FlowData flowData = flowDataProvider.getFlowData(request);
DeviceData device = flowData.get(DeviceData.class);
...

Results can also be accessed in client-side code by using the fod object. Note that the global object name can be changed by using the setObjectName option on the JavaScriptBuilderElementBuilder

window.onload = function () {
fod.complete(function(data) {
var hardwareName = data.device.hardwarename;
alert(hardwareName.join(", "));
}
}
/*
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2022 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* 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.web;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.pipeline.core.configuration.PipelineOptions;
import fiftyone.pipeline.core.configuration.PipelineOptionsFactory;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.web.services.FlowDataProviderCore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import static fiftyone.common.testhelpers.LogbackHelper.configureLogback;
import static fiftyone.devicedetection.examples.web.HtmlContentHelper.*;
import static fiftyone.pipeline.util.FileFinder.getFilePath;
public class GettingStartedWebOnPrem extends HttpServlet {
private static final long serialVersionUID = 1734154705981153540L;
public static String resourceBase = "device-detection.examples/web/getting-started" +
".onprem/src/main/webapp";
public static Logger logger = LoggerFactory.getLogger(GettingStartedWebOnPrem.class);
public static void main(String[] args) throws Exception {
configureLogback(getFilePath("logback.xml"));
logger.info("Running Example {}", GettingStartedWebOnPrem.class);
// start Jetty with this WebApp
EmbedJetty.runWebApp(resourceBase, 8081);
}
FlowDataProviderCore flowDataProvider = new FlowDataProviderCore.Default();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
// the detection has already been carried out by the Filter
// which is responsible to the lifecycle of the flowData - do NOT dispose
FlowData flowData = flowDataProvider.getFlowData(request);
// retrieve the device data from the flowdata
DeviceData device = flowData.get(DeviceData.class);
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
doHtmlPreamble(out, "Web Integration On-Premise Example");
// include description of example
doStaticText(out, resourceBase + "/WEB-INF/html/example-description.html");
// include a script to display the results of the client side detection
doStaticText(out, resourceBase + "/WEB-INF/html/client-side-js-include.html");
// find out where our data file is from the configuration
PipelineOptions pipelineOptions =
PipelineOptionsFactory.getOptionsFromFile(getFilePath(resourceBase) +
"/WEB-INF/51Degrees-OnPrem.xml");
doDeviceData(out, device, flowData,
pipelineOptions.findAndSubstitute("DeviceDetectionHashEngine", "DataFile"));
doStaticText(out, resourceBase + "/WEB-INF/html/apple-detection.html");
doEvidence(out, request, flowData);
doResponseHeaders(out, response);
doHtmlPostamble(out);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
// any failure causes 500 error
try {
processRequest(request, response);
response.setStatus(200);
} catch (Exception e) {
e.printStackTrace();
response.setStatus(500);
}
}
}