\r\n

51Degrees Device Detection Java  4.4

Device detection services for 51Degrees Pipeline

GettingStartedWebCloud.java

This example is available in full on GitHub.

Overview

The PipelineFilter 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.

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 2023 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.data.FlowData;
import fiftyone.pipeline.web.Constants;
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.shared.KeyHelper.getOrSetTestResourceKey;
import static fiftyone.devicedetection.examples.web.HtmlContentHelper.*;
import static fiftyone.pipeline.util.FileFinder.getFilePath;
public class GettingStartedWebCloud extends HttpServlet {
private static final long serialVersionUID = 1734154705981153540L;
public static String resourceBase = "web/getting-started.cloud/src/main/webapp";
public static Logger logger = LoggerFactory.getLogger(GettingStartedWebCloud.class);
public static void main(String[] args) throws Exception {
configureLogback(getFilePath("logback.xml"));
String resourceKey = args.length > 0 ? args[0] : null;
logger.info("Running Example {}", GettingStartedWebCloud.class);
// check resource key and set as System Property
getOrSetTestResourceKey(resourceKey);
// 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 Cloud Example");
// request main 51Degrees Client Side Script - this is automatically
// served by inclusion of the PipelineFilter which intercepts the request
// and serves dynamically generated JavaScript
out.println("<script src=\"" + Constants.CORE_JS_NAME+ "\"></script>");
// 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");
doDeviceData(out, device, flowData, null);
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);
}
}
}