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.
This commit is contained in:
parent
a46208fc4f
commit
224a3a8a42
|
@ -112,6 +112,11 @@
|
|||
<artifactId>commons-io</artifactId>
|
||||
<version>2.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.maxmind.geoip2</groupId>
|
||||
<artifactId>geoip2</artifactId>
|
||||
<version>2.8.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -146,7 +151,7 @@
|
|||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<!-- <exclude>**/*ProductionTest.java</exclude> -->
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
</excludes>
|
||||
<systemPropertyVariables>
|
||||
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="ISO-8859-1">
|
||||
<title>Geo IP Test</title>
|
||||
|
||||
<!--jquery dep -->
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready (function () {
|
||||
// getting the public ip address from api and setting on text box
|
||||
// ip api : https://www.ipify.org/
|
||||
$.get( "https://api.ipify.org?format=json", function( data ) {
|
||||
console.log(data);
|
||||
$("#ip").val(data.ip) ;
|
||||
});
|
||||
|
||||
function showLocationOnMap (location) {
|
||||
var map;
|
||||
|
||||
map = new google.maps.Map(document.getElementById('map'), {
|
||||
center: {lat: Number(location.latitude), lng: Number(location.longitude)},
|
||||
zoom: 15
|
||||
});
|
||||
|
||||
var marker = new google.maps.Marker({
|
||||
position: {lat: Number(location.latitude), lng: Number(location.longitude)},
|
||||
map: map,
|
||||
title: "Public IP:"+location.ipAddress+" @ "+location.city
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
$( "#ipForm" ).submit(function( event ) {
|
||||
event.preventDefault();
|
||||
$.ajax({
|
||||
url: "GeoIPTest",
|
||||
type: "POST",
|
||||
contentType: "application/x-www-form-urlencoded; charset=UTF-8", // send as JSON
|
||||
data: $.param( {ipAddress : $("#ip").val()} ),
|
||||
|
||||
complete: function(data) {
|
||||
console.log ("Request complete");
|
||||
|
||||
},
|
||||
|
||||
success: function(data) {
|
||||
$("#status").html(JSON.stringify(data));
|
||||
|
||||
if (data.ipAddress !=null) {
|
||||
console.log ("Success:"+data.ipAddress);
|
||||
showLocationOnMap(data);
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
error: function(err) {
|
||||
console.log(err);
|
||||
$("#status").html("Error:"+JSON.stringify(data));
|
||||
},
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
<form id="ipForm" action="GeoIPTest" method="POST">
|
||||
<input type="text" name = "ipAddress" id = "ip"/>
|
||||
<input type="submit" name="submit" value="submit" />
|
||||
|
||||
</form>
|
||||
|
||||
<div id="status"></div>
|
||||
|
||||
<div id="map" style="height: 500px; width:100%; position:absolute"></div>
|
||||
<!--AIzaSyDDr65sVtJtlbliOTAeXyZSDPvG9NROjJA -->
|
||||
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA4vsDQWHeOrDnzS98XMXl5hgwA9raaQZ8"
|
||||
async defer></script>
|
||||
</body>
|
||||
</html>
|
|
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue