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
The results of detection can be accessed by using a FlowDataProvider which is responsible for managing the lifecycle of the flowData - do NOT dispose
package fiftyone.ipintelligence.examples.web;
import fiftyone.ipintelligence.shared.IPIntelligenceData;
import fiftyone.pipeline.core.configuration.PipelineOptions;
import fiftyone.pipeline.core.configuration.PipelineOptionsFactory;
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 java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static fiftyone.common.testhelpers.LogbackHelper.configureLogback;
import static fiftyone.ipintelligence.examples.web.HtmlContentHelper.*;
import static fiftyone.pipeline.util.FileFinder.getFilePath;
public class GettingStartedWebOnPrem extends HttpServlet {
private static final long serialVersionUID = 1734154705981153540L;
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);
EmbedJetty.runWebApp(getResourceBase(), 8081);
}
public static String getResourceBase() {
final String basePath = "web/getting-started.onprem/src/main/webapp";
{
final Path path = Paths.get(basePath);
if (Files.exists(path) && Files.isDirectory(path)) {
return path.toString();
}
}
{
final Path path2 = Paths.get("ip-intelligence-java-examples", basePath);
if (Files.exists(path2) && Files.isDirectory(path2)) {
return path2.toString();
}
}
return basePath;
}
FlowDataProviderCore flowDataProvider = new FlowDataProviderCore.Default();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
FlowData flowData = flowDataProvider.getFlowData(request);
IPIntelligenceData device = flowData.get(IPIntelligenceData.class);
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
doHtmlPreamble(out, "Web Integration On-Premise Example");
out.println("<script src=\"" + Constants.CORE_JS_NAME+ "\"></script>");
String resourceBase = getResourceBase();
doStaticText(out, resourceBase + "/WEB-INF/html/example-description.html");
doStaticText(out, resourceBase + "/WEB-INF/html/client-side-js-include.html");
PipelineOptions pipelineOptions =
PipelineOptionsFactory.getOptionsFromFile(getFilePath(resourceBase) +
"/WEB-INF/51Degrees-OnPrem.xml");
doIPIntelligenceData(out, device, flowData,
pipelineOptions.findAndSubstitute("IPIntelligenceOnPremiseEngine", "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) {
try {
response.setStatus(200);
} catch (Exception e) {
e.printStackTrace();
response.setStatus(500);
}
}
}