51Degrees Geo-Location Java  4.2

Geo-location services for 51Degrees Pipeline

servlet/Example.java

Servlet geo location exampleThis example shows how to:

  1. Set up configuration options to add elements to the 51Degrees Pipeline.
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 'BrowserName','BrowserVendor',
10  'BrowserVersion','HardwareName','HardeareVendor',
11  'PlatformName','PlatformVendor','PlatformVersion'
12  properties as they are used by this example. -->
13  <ResourceKey>!!YOUR_RESOURCE_KEY!!</ResourceKey>
14  </BuildParameters>
15  <BuilderName>CloudRequestEngine</BuilderName>
16  </Element>
17  <Element>
18  <BuildParameters>
19  <GeoLocationProvider>FiftyOneDegrees</GeoLocationProvider>
20  </BuildParameters>
21  <BuilderName>GeoLocationCloudEngine</BuilderName>
22  </Element>
23  </Elements>
24 </PipelineOptions>
  1. Configure the filter and map it to be run for all URLs.
    1 <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    2  ...
    3  <filter>
    4  <filter-name>Pipeline</filter-name>
    5  <filter-class>fiftyone.pipeline.web.PipelineFilter</filter-class>
    6  <init-param>
    7  <param-name>clientside-properties-enabled</param-name>
    8  <param-value>true</param-value>
    9  </init-param>
    10  </filter>
    11  <filter-mapping>
    12  <filter-name>Pipeline</filter-name>
    13  <url-pattern>/*</url-pattern>
    14  </filter-mapping>
    15  ...
  2. Add the FlowDataProvider to the servlet.
  3. User the results contained in the flow data to display something on a page, and optionally add the client-side code to improve detection accuracy on devices like iPhones.

Servlet

/* *********************************************************************
* 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.geolocation.web.examples.servlet;
import fiftyone.geolocation.core.data.GeoData;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.engines.data.AspectPropertyValue;
import fiftyone.pipeline.engines.exceptions.NoValueException;
import fiftyone.pipeline.jsonbuilder.data.JsonBuilderData;
import fiftyone.pipeline.web.services.FlowDataProviderCore;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import static fiftyone.pipeline.util.StringManipulation.stringJoin;
public class Example extends HttpServlet {
FlowDataProviderCore flowDataProvider = new FlowDataProviderCore.Default();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, NoValueException {
FlowData data = flowDataProvider.getFlowData(request);
GeoData geo = data.get(GeoData.class);
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Example</title>");
out.println("</head>");
out.println("<body>");
out.println("<script src=\"/geo-location.web.examples.servlet/51Degrees.core.js\"></script>");
AspectPropertyValue<String> town = geo.getTown();
AspectPropertyValue<String> county = geo.getCounty();
AspectPropertyValue<String> state = geo.getState();
AspectPropertyValue<String> country = geo.getCountry();
out.println("<h2>Example</h2>\n" +
"\n" +
"<div id=\"content\">\n" +
" <p>\n" +
" The following values are determined sever-side on the first request.\n" +
" As the server has no location information to work from, these \n" +
" values will all be unknown:\n" +
" </p>\n" +
" <dl>\n" +
" <dt><strong>Country</strong></dt>\n" +
" <dv>" + (county.hasValue() ? county.getValue() : "Unknown: " + county.getNoValueMessage()) + "</dv>\n" +
" <dt><strong>State</strong></dt>\n" +
" <dv>" + (state.hasValue() ? state.getValue() : "Unknown: " + state.getNoValueMessage()) + "</dv>\n" +
" <dt><strong>County</strong></dt>\n" +
" <dv>" + (country.hasValue() ? country.getValue() : "Unknown: " + country.getNoValueMessage()) + "</dv>\n" +
" <dt><strong>Town/City</strong></dt>\n" +
" <dv>" + (town.hasValue() ? town.getValue() : "Unknown: " + town.getNoValueMessage()) + "</dv>\n" +
" </dl> \n" +
" <p>\n" +
" When the button below is clicked, JavaScript running on the client-side will be used to obtain additional evidence (i.e. the location information from the device). If no additional information appears then it may indicate an external problem such as JavaScript being disabled in your browser.\n" +
" </p>\n" +
" <p>\n" +
" Note that the accuracy of the information is dependent on the accuracy of the location data returned by your device. Any device that lacks GPS is likely to return a highly inaccurate result. Among devices with GPS, some have a significantly lower margin of error than others.\n" +
" </p>\n" +
" <button type=\"button\" onclick=\"buttonClicked()\">Use my location</button>\n" +
"</div>\n" +
"\n" +
"<script>\n" +
" buttonClicked = function () {\n" +
" // This function will fire when the JSON data object is updated\n" +
" // with information from the server.\n" +
" // The sequence is:\n" +
" // 1. Response contains JavaScript property 'JavaScript'. This is not executed immediately on the client as it will prompt the user to allow access to location.\n" +
" // 2. When the button is clicked, the fod.complete function is called, passing 'location' as the second parameter. This lets the code know that we want to execute\n" +
" // any JavaScript needed to obtain the location data that is needed to determine the user's postal address details.\n" +
" // 3. The execution of the location JavaScript triggers a background callback to the webserver that includes the new evidence (i.e. lat/lon).\n" +
" // 4. The web server responds with new JSON data that contains the updated property values based on the new evidence.\n" +
" // 5. The JavaScript integrates the new JSON data and fires the 'complete' callback function below, which then displays the results.\n" +
" fod.complete(function (data) {\n" +
" let fieldValues = [];\n" +
" if (data.location) {\n" +
" fieldValues.push([\"Country\", data.location.country]);\n" +
" fieldValues.push([\"State\", data.location.state]);\n" +
" fieldValues.push([\"County\", data.location.country]);\n" +
" fieldValues.push([\"Town/City\", data.location.town]);\n" +
" }\n" +
" else {\n" +
" fieldValue.push([\"Location data is empty. This probably means that something has gone wrong with the JavaScript evaluation.\", \"\"])\n" +
" }\n" +
" displayValues(fieldValues);\n" +
" }, 'location');\n" +
" }\n" +
"\n" +
" function displayValues(fieldValues) {\n" +
" var list = document.createElement('dl');\n" +
" fieldValues.forEach(function (entry) {\n" +
" var name = document.createElement('dt');\n" +
" var value = document.createElement('dv');\n" +
" var bold = document.createElement('strong');\n" +
" var fieldname = document.createTextNode(entry[0]);\n" +
" var fieldvalue = document.createTextNode(entry[1]);\n" +
" bold.appendChild(fieldname);\n" +
" name.appendChild(bold);\n" +
" value.appendChild(fieldvalue);\n" +
" list.appendChild(name);\n" +
" list.appendChild(value);\n" +
" });\n" +
"\n" +
" var element = document.getElementById('content');\n" +
" element.appendChild(list);\n" +
" }\n" +
"</script>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (NoValueException e) {
e.printStackTrace();
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}