This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
/
MaxMindServiceImpl.java
98 lines (87 loc) · 3.03 KB
/
MaxMindServiceImpl.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.madadipouya.neatgeoip.service;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.madadipouya.neatgeoip.pojo.Country;
import com.madadipouya.neatgeoip.util.loader.DatabaseFileLoader;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import static org.apache.commons.lang3.StringUtils.isEmpty;
/*
* This file is part of NeatGeoIP.
*
* NeatGeoIP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3
* as published by the Free Software Foundation.
*
* NeatGeoIP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. <http://www.gnu.org/licenses/>
*
* Author(s):
*
* © 2016 Kasra Madadipouya <kasra@madadipouya.com>
*/
@Service
@Data
public class MaxMindServiceImpl implements MaxMindService {
@Autowired
@Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE)
private DatabaseFileLoader databaseFileLoader;
@Autowired
@Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE)
private Country country;
@Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE)
private DatabaseReader reader;
@Autowired
private void loadDatabaseFile() {
setReader(databaseFileLoader.loadDatabaseFileFromDisk());
}
@Override
public Country findCountryFor(String ip) {
return resultMapper(lookup(ip));
}
private CityResponse lookup(String ip) {
try {
return getReader().city(InetAddress.getByName(ip));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (GeoIp2Exception e) {
e.printStackTrace();
}
return null;
}
private Country resultMapper(CityResponse response) {
try {
if(isEmpty(response.getCountry().getName())) {
return noMatchingCountry();
}
country.setName(response.getCountry().getName())
.setIsoCode(response.getCountry().getIsoCode())
.setNames(response.getCountry().getNames());
} catch(Exception ex) {
noMatchingCountry();
}
return country;
}
private Country noMatchingCountry() {
return country.setName("").setIsoCode("")
.setNames(ImmutableMap.<String, String>builder().build())
.setErrors(ImmutableList.<String>builder().add("Country not found!").build());
}
}