Skip to content

Commit 224a3a8

Browse files
joshiparthinKevinGilmore
authored andcommitted
GeoIp Article PR (eugenp#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.
1 parent a46208f commit 224a3a8

6 files changed

Lines changed: 234 additions & 1 deletion

File tree

‎spring-mvc-xml/pom.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@
112112
<artifactId>commons-io</artifactId>
113113
<version>2.2</version>
114114
</dependency>
115+
<dependency>
116+
<groupId>com.maxmind.geoip2</groupId>
117+
<artifactId>geoip2</artifactId>
118+
<version>2.8.0</version>
119+
</dependency>
115120
</dependencies>
116121

117122
<build>
@@ -146,7 +151,7 @@
146151
<version>${maven-surefire-plugin.version}</version>
147152
<configuration>
148153
<excludes>
149-
<!-- <exclude>**/*ProductionTest.java</exclude> -->
154+
<exclude>**/*IntegrationTest.java</exclude>
150155
</excludes>
151156
<systemPropertyVariables>
152157
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.spring.controller;
2+
3+
import java.io.IOException;
4+
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.ResponseBody;
10+
11+
import com.baeldung.spring.form.GeoIP;
12+
import com.baeldung.spring.service.RawDBDemoGeoIPLocationService;
13+
14+
@Controller
15+
public class GeoIPTestController {
16+
private RawDBDemoGeoIPLocationService locationService;
17+
public GeoIPTestController() throws IOException {
18+
locationService
19+
= new RawDBDemoGeoIPLocationService();
20+
}
21+
@RequestMapping(value="/GeoIPTest", method = RequestMethod.POST)
22+
@ResponseBody
23+
public GeoIP getLocation(
24+
@RequestParam(value="ipAddress", required=true) String ipAddress) throws Exception {
25+
26+
return locationService.getLocation(ipAddress);
27+
}
28+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.spring.form;
2+
3+
public class GeoIP {
4+
private String ipAddress;
5+
private String city;
6+
private String latitude;
7+
private String longitude;
8+
9+
public GeoIP() {
10+
11+
}
12+
13+
public GeoIP(String ipAddress) {
14+
this.ipAddress = ipAddress;
15+
}
16+
17+
public GeoIP(String ipAddress, String city, String latitude, String longitude) {
18+
this.ipAddress = ipAddress;
19+
this.city = city;
20+
this.latitude = latitude;
21+
this.longitude = longitude;
22+
}
23+
24+
public String getIpAddress() {
25+
return ipAddress;
26+
}
27+
28+
public void setIpAddress(String ipAddress) {
29+
this.ipAddress = ipAddress;
30+
}
31+
32+
public String getCity() {
33+
return city;
34+
}
35+
36+
public void setCity(String city) {
37+
this.city = city;
38+
}
39+
40+
public String getLatitude() {
41+
return latitude;
42+
}
43+
44+
public void setLatitude(String latitude) {
45+
this.latitude = latitude;
46+
}
47+
48+
public String getLongitude() {
49+
return longitude;
50+
}
51+
52+
public void setLongitude(String longitude) {
53+
this.longitude = longitude;
54+
}
55+
56+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.spring.service;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.InetAddress;
6+
7+
import com.baeldung.spring.form.GeoIP;
8+
import com.maxmind.geoip2.DatabaseReader;
9+
import com.maxmind.geoip2.exception.GeoIp2Exception;
10+
import com.maxmind.geoip2.model.CityResponse;
11+
12+
public class RawDBDemoGeoIPLocationService{
13+
private DatabaseReader dbReader;
14+
15+
public RawDBDemoGeoIPLocationService() throws IOException {
16+
File database = new File("C:\\Users\\Parth Joshi\\Desktop\\GeoLite2-City.mmdb\\GeoLite2-City.mmdb");
17+
dbReader = new DatabaseReader.Builder(database).build();
18+
}
19+
20+
public GeoIP getLocation(String ip) throws IOException, GeoIp2Exception {
21+
InetAddress ipAddress = InetAddress.getByName(ip);
22+
CityResponse response = dbReader.city(ipAddress);
23+
24+
String cityName = response.getCity().getName();
25+
String latitude = response.getLocation().getLatitude().toString();
26+
String longitude = response.getLocation().getLongitude().toString();
27+
return new GeoIP(ip, cityName, latitude, longitude);
28+
}
29+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="ISO-8859-1">
5+
<title>Geo IP Test</title>
6+
7+
<!--jquery dep -->
8+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
9+
<script type="text/javascript">
10+
$(document).ready (function () {
11+
// getting the public ip address from api and setting on text box
12+
// ip api : https://www.ipify.org/
13+
$.get( "https://api.ipify.org?format=json", function( data ) {
14+
console.log(data);
15+
$("#ip").val(data.ip) ;
16+
});
17+
18+
function showLocationOnMap (location) {
19+
var map;
20+
21+
map = new google.maps.Map(document.getElementById('map'), {
22+
center: {lat: Number(location.latitude), lng: Number(location.longitude)},
23+
zoom: 15
24+
});
25+
26+
var marker = new google.maps.Marker({
27+
position: {lat: Number(location.latitude), lng: Number(location.longitude)},
28+
map: map,
29+
title: "Public IP:"+location.ipAddress+" @ "+location.city
30+
});
31+
32+
}
33+
34+
$( "#ipForm" ).submit(function( event ) {
35+
event.preventDefault();
36+
$.ajax({
37+
url: "GeoIPTest",
38+
type: "POST",
39+
contentType: "application/x-www-form-urlencoded; charset=UTF-8", // send as JSON
40+
data: $.param( {ipAddress : $("#ip").val()} ),
41+
42+
complete: function(data) {
43+
console.log ("Request complete");
44+
45+
},
46+
47+
success: function(data) {
48+
$("#status").html(JSON.stringify(data));
49+
50+
if (data.ipAddress !=null) {
51+
console.log ("Success:"+data.ipAddress);
52+
showLocationOnMap(data);
53+
54+
}
55+
},
56+
57+
error: function(err) {
58+
console.log(err);
59+
$("#status").html("Error:"+JSON.stringify(data));
60+
},
61+
});
62+
63+
});
64+
65+
});
66+
</script>
67+
</head>
68+
<body>
69+
70+
71+
<form id="ipForm" action="GeoIPTest" method="POST">
72+
<input type="text" name = "ipAddress" id = "ip"/>
73+
<input type="submit" name="submit" value="submit" />
74+
75+
</form>
76+
77+
<div id="status"></div>
78+
79+
<div id="map" style="height: 500px; width:100%; position:absolute"></div>
80+
<!--AIzaSyDDr65sVtJtlbliOTAeXyZSDPvG9NROjJA -->
81+
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA4vsDQWHeOrDnzS98XMXl5hgwA9raaQZ8"
82+
async defer></script>
83+
</body>
84+
</html>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.geoip;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.InetAddress;
6+
7+
import org.junit.Test;
8+
9+
import com.maxmind.geoip2.DatabaseReader;
10+
import com.maxmind.geoip2.exception.GeoIp2Exception;
11+
import com.maxmind.geoip2.model.CityResponse;
12+
13+
14+
public class GeoIpIntegrationTest {
15+
16+
@Test
17+
public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception {
18+
File database = new File("C:\\Users\\Parth Joshi\\Desktop\\GeoLite2-City.mmdb\\GeoLite2-City.mmdb");
19+
DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
20+
21+
InetAddress ipAddress = InetAddress.getByName("202.47.112.9");
22+
CityResponse response = dbReader.city(ipAddress);
23+
24+
String countryName = response.getCountry().getName();
25+
String cityName = response.getCity().getName();
26+
String postal = response.getPostal().getCode();
27+
String state = response.getLeastSpecificSubdivision().getName();
28+
29+
}
30+
31+
}

0 commit comments

Comments
 (0)