\r\n

51Degrees Geo-Location Java  4.4

Geo-location services for 51Degrees Pipeline

mvc/configuration/ExampleMvcConfig.java

Spring MVC device detection 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 <!--
3  ~ This Original Work is copyright of 51 Degrees Mobile Experts Limited.
4  ~ Copyright 2022 51 Degrees Mobile Experts Limited, Davidson House,
5  ~ Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
6  ~
7  ~ This Original Work is licensed under the European Union Public Licence
8  ~ (EUPL) v.1.2 and is subject to its terms as set out below.
9  ~
10  ~ If a copy of the EUPL was not distributed with this file, You can obtain
11  ~ one at https://opensource.org/licenses/EUPL-1.2.
12  ~
13  ~ The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
14  ~ amended by the European Commission) shall be deemed incompatible for
15  ~ the purposes of the Work and the provisions of the compatibility
16  ~ clause in Article 5 of the EUPL shall not apply.
17  ~
18  ~ If using the Work as, or as part of, a network application, by
19  ~ including the attribution notice(s) required under Article 5 of the EUPL
20  ~ in the end user terms of the application under an appropriate heading,
21  ~ such notice(s) shall fulfill the requirements of that article.
22  -->
23 
24 <PipelineOptions>
25  <Elements>
26  <Element>
27  <BuildParameters>
28  <EndPoint>https://cloud.51degrees.com/api/v4</EndPoint>
29  <!-- Obtain a resource key for free at
30  https://configure.51degrees.com
31  Make sure to include the 'Country','State',
32  'County','Town' properties as they are used
33  by this example. -->
34  <ResourceKey>!!YOUR_RESOURCE_KEY!!</ResourceKey>
35  </BuildParameters>
36  <BuilderName>CloudRequestEngine</BuilderName>
37  </Element>
38  <Element>
39  <BuildParameters>
40  <GeoLocationProvider>FiftyOneDegrees</GeoLocationProvider>
41  </BuildParameters>
42  <BuilderName>GeoLocationCloudEngine</BuilderName>
43  </Element>
44  </Elements>
45 </PipelineOptions>
  1. Set up MVC, enable configuration, and add the Pipeline component.
  2. Configure the interceptor.
  3. Enable client-side code to improve detection accuracy on devices like iPhones.
  4. Add the interceptor.
  5. Inject the FlowDataProvider into a controller.
    @Controller
    @RequestMapping("/")
    public class ExampleController {
    private FlowDataProvider flowDataProvider;
    @Autowired
    public ExampleController(FlowDataProvider flowDataProvider) {
    this.flowDataProvider = flowDataProvider;
    }
    ...
  6. Use the results contained in the flow data to display something on a page view.
    @Controller
    @RequestMapping("/")
    public class ExampleController {
    ...
    @RequestMapping(method = RequestMethod.GET)
    public String get(ModelMap model, HttpServletRequest request) {
    FlowData data = flowDataProvider.getFlowData(request);
    GeoData geo = data.get(GeoData.class);
    model.addAttribute("town", geo.getTown());
    model.addAttribute("county", geo.getCounty());
    model.addAttribute("state", geo.getState());
    model.addAttribute("country", geo.getCountry());
    return "example";
    }
    ...

    Controller

/* *********************************************************************
* 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.geolocation.web.examples.mvc.controller;
import fiftyone.geolocation.core.data.GeoData;
import fiftyone.pipeline.core.data.FlowData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import fiftyone.pipeline.web.mvc.components.FlowDataProvider;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/")
public class ExampleController {
private FlowDataProvider flowDataProvider;
@Autowired
public ExampleController(FlowDataProvider flowDataProvider) {
this.flowDataProvider = flowDataProvider;
}
@RequestMapping(method = RequestMethod.GET)
public String get(ModelMap model, HttpServletRequest request) {
FlowData data = flowDataProvider.getFlowData(request);
GeoData geo = data.get(GeoData.class);
model.addAttribute("town", geo.getTown());
model.addAttribute("county", geo.getCounty());
model.addAttribute("state", geo.getState());
model.addAttribute("country", geo.getCountry());
return "example";
}
}

Config

/* *********************************************************************
* 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.geolocation.web.examples.mvc.configuration;
import fiftyone.pipeline.web.mvc.components.FiftyOneInterceptor;
import fiftyone.pipeline.web.mvc.configuration.FiftyOneInterceptorConfig;
import fiftyone.pipeline.web.mvc.configuration.FiftyOneInterceptorConfigDefault;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import javax.servlet.ServletContext;
import static fiftyone.pipeline.web.mvc.components.FiftyOneInterceptor.enableClientsideProperties;
@EnableWebMvc
@Configuration
@ComponentScan({"fiftyone.geolocation.web.examples.mvc.controller","fiftyone.pipeline.web.mvc"})
public class ExampleMvcConfig implements WebMvcConfigurer {
public ExampleMvcConfig() {
super();
}
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
enableClientsideProperties(registry);
}
@Autowired
ServletContext servletContext;
@Bean
public FiftyOneInterceptorConfig fiftyOneInterceptorConfig() {
final FiftyOneInterceptorConfigDefault bean = new FiftyOneInterceptorConfigDefault();
bean.setDataFilePath(servletContext.getRealPath("/WEB-INF/51Degrees-Cloud.xml"));
bean.setClientsidePropertiesEnabled(true);
return bean;
}
@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
return bean;
}
@Autowired
FiftyOneInterceptor fiftyOneInterceptor;
@Override
public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(fiftyOneInterceptor);
}
}