\r\n

51Degrees Device Detection Java  4.3Newer Version 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 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.

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(", "));
}
}
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.web.HtmlContentHelper.*;
import static fiftyone.devicedetection.shared.testhelpers.FileUtils.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
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");
// 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");
// find out where our data file is from the configuration
OptionsHelper oh = new OptionsHelper(resourceBase + "/WEB-INF/51Degrees-OnPrem.xml");
doDeviceData(out, device, flowData, oh.find("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);
}
}
}