From 224a3a8a42e2c005fa77adee45f362cb6ada5ae9 Mon Sep 17 00:00:00 2001 From: Parth Joshi Date: Sat, 19 Nov 2016 03:32:52 +0530 Subject: [PATCH] GeoIp Article PR (#789) * GEO Ip Article first commit. * Changes in controller and flow as per review by Kevin. * Removed this file from spring-mvc-java as I had worked on spring-mvc-xml and this was commited by mistake. * Some fix in service usage as i was initializing service, intializing the db in the each requests. * Changes for Integration Testing Config. Renaming the test file for the same purpose. * Removed integration profile. --- spring-mvc-xml/pom.xml | 7 +- .../controller/GeoIPTestController.java | 28 +++++++ .../java/com/baeldung/spring/form/GeoIP.java | 56 +++++++++++++ .../RawDBDemoGeoIPLocationService.java | 29 +++++++ spring-mvc-xml/src/main/webapp/GeoIpTest.jsp | 84 +++++++++++++++++++ .../baeldung/geoip/GeoIpIntegrationTest.java | 31 +++++++ 6 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 spring-mvc-xml/src/main/java/com/baeldung/spring/controller/GeoIPTestController.java create mode 100644 spring-mvc-xml/src/main/java/com/baeldung/spring/form/GeoIP.java create mode 100644 spring-mvc-xml/src/main/java/com/baeldung/spring/service/RawDBDemoGeoIPLocationService.java create mode 100644 spring-mvc-xml/src/main/webapp/GeoIpTest.jsp create mode 100644 spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index 849699cfae..ca51a56633 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -112,6 +112,11 @@ commons-io 2.2 + + com.maxmind.geoip2 + geoip2 + 2.8.0 + @@ -146,7 +151,7 @@ ${maven-surefire-plugin.version} - + **/*IntegrationTest.java diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/GeoIPTestController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/GeoIPTestController.java new file mode 100644 index 0000000000..16de4e56f5 --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/GeoIPTestController.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.controller; + +import java.io.IOException; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.baeldung.spring.form.GeoIP; +import com.baeldung.spring.service.RawDBDemoGeoIPLocationService; + +@Controller +public class GeoIPTestController { + private RawDBDemoGeoIPLocationService locationService; + public GeoIPTestController() throws IOException { + locationService + = new RawDBDemoGeoIPLocationService(); + } + @RequestMapping(value="/GeoIPTest", method = RequestMethod.POST) + @ResponseBody + public GeoIP getLocation( + @RequestParam(value="ipAddress", required=true) String ipAddress) throws Exception { + + return locationService.getLocation(ipAddress); + } +} diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/form/GeoIP.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/GeoIP.java new file mode 100644 index 0000000000..19f56867a1 --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/GeoIP.java @@ -0,0 +1,56 @@ +package com.baeldung.spring.form; + +public class GeoIP { + private String ipAddress; + private String city; + private String latitude; + private String longitude; + + public GeoIP() { + + } + + public GeoIP(String ipAddress) { + this.ipAddress = ipAddress; + } + + public GeoIP(String ipAddress, String city, String latitude, String longitude) { + this.ipAddress = ipAddress; + this.city = city; + this.latitude = latitude; + this.longitude = longitude; + } + + public String getIpAddress() { + return ipAddress; + } + + public void setIpAddress(String ipAddress) { + this.ipAddress = ipAddress; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getLatitude() { + return latitude; + } + + public void setLatitude(String latitude) { + this.latitude = latitude; + } + + public String getLongitude() { + return longitude; + } + + public void setLongitude(String longitude) { + this.longitude = longitude; + } + +} diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/service/RawDBDemoGeoIPLocationService.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/service/RawDBDemoGeoIPLocationService.java new file mode 100644 index 0000000000..0a292ab1e9 --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/service/RawDBDemoGeoIPLocationService.java @@ -0,0 +1,29 @@ +package com.baeldung.spring.service; + +import java.io.File; +import java.io.IOException; +import java.net.InetAddress; + +import com.baeldung.spring.form.GeoIP; +import com.maxmind.geoip2.DatabaseReader; +import com.maxmind.geoip2.exception.GeoIp2Exception; +import com.maxmind.geoip2.model.CityResponse; + +public class RawDBDemoGeoIPLocationService{ + private DatabaseReader dbReader; + + public RawDBDemoGeoIPLocationService() throws IOException { + File database = new File("C:\\Users\\Parth Joshi\\Desktop\\GeoLite2-City.mmdb\\GeoLite2-City.mmdb"); + dbReader = new DatabaseReader.Builder(database).build(); + } + + public GeoIP getLocation(String ip) throws IOException, GeoIp2Exception { + InetAddress ipAddress = InetAddress.getByName(ip); + CityResponse response = dbReader.city(ipAddress); + + String cityName = response.getCity().getName(); + String latitude = response.getLocation().getLatitude().toString(); + String longitude = response.getLocation().getLongitude().toString(); + return new GeoIP(ip, cityName, latitude, longitude); + } +} diff --git a/spring-mvc-xml/src/main/webapp/GeoIpTest.jsp b/spring-mvc-xml/src/main/webapp/GeoIpTest.jsp new file mode 100644 index 0000000000..431f6162bc --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/GeoIpTest.jsp @@ -0,0 +1,84 @@ + + + + +Geo IP Test + + + + + + + + +
+ + + +
+ +
+ +
+ + + + \ No newline at end of file diff --git a/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java b/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java new file mode 100644 index 0000000000..72d528095e --- /dev/null +++ b/spring-mvc-xml/src/test/java/com/baeldung/geoip/GeoIpIntegrationTest.java @@ -0,0 +1,31 @@ +package com.baeldung.geoip; + +import java.io.File; +import java.io.IOException; +import java.net.InetAddress; + +import org.junit.Test; + +import com.maxmind.geoip2.DatabaseReader; +import com.maxmind.geoip2.exception.GeoIp2Exception; +import com.maxmind.geoip2.model.CityResponse; + + +public class GeoIpIntegrationTest { + + @Test + public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception { + File database = new File("C:\\Users\\Parth Joshi\\Desktop\\GeoLite2-City.mmdb\\GeoLite2-City.mmdb"); + DatabaseReader dbReader = new DatabaseReader.Builder(database).build(); + + InetAddress ipAddress = InetAddress.getByName("202.47.112.9"); + CityResponse response = dbReader.city(ipAddress); + + String countryName = response.getCountry().getName(); + String cityName = response.getCity().getName(); + String postal = response.getPostal().getCode(); + String state = response.getLeastSpecificSubdivision().getName(); + + } + +}