-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoIPTest.java
More file actions
43 lines (34 loc) · 1.43 KB
/
Copy pathGeoIPTest.java
File metadata and controls
43 lines (34 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package codingTest;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.model.CountryResponse;
import org.junit.jupiter.api.Test;
public class GeoIPTest {
public static void main(String[] args) throws IOException {
String dbLocation = "";
// File database = new File(dbLocation)
// DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
}
@Test
public void givenIP_whenFetchingCity_thenReturnsCityData()
throws IOException, GeoIp2Exception {
String ip = "128.11.22.33";
String dbLocation = "D:\\sources\\geoip\\GeoLite2-City.mmdb";
File database = new File(dbLocation);
DatabaseReader dbReader = new DatabaseReader.Builder(database)
.build();
InetAddress ipAddress = InetAddress.getByName(ip);
CityResponse response = dbReader.city(ipAddress);
CountryResponse cResponse = dbReader.country(ipAddress);
String countryName = response.getCountry().getName();
String countryCode = response.getCountry().getIsoCode();
String cityName = response.getCity().getName();
String postal = response.getPostal().getCode();
String state = response.getLeastSpecificSubdivision().getName();
System.out.println("countryName:"+countryName);
}
}