This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package ru.soune.nocopy.service.geo;
|
||||
|
||||
import com.maxmind.geoip2.DatabaseReader;
|
||||
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
||||
import com.maxmind.geoip2.model.CountryResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -21,13 +22,9 @@ public class GeoCountryService {
|
||||
this.dbReader = new DatabaseReader.Builder(resource.getInputStream()).build();
|
||||
}
|
||||
|
||||
public String getCountry(String input) {
|
||||
public String getCountryName(String input) {
|
||||
try {
|
||||
String domain = extractDomain(input);
|
||||
|
||||
InetAddress ip = InetAddress.getByName(domain);
|
||||
|
||||
CountryResponse response = dbReader.country(ip);
|
||||
CountryResponse response = extractCountry(input);
|
||||
|
||||
return response.getCountry().getName();
|
||||
} catch (Exception e) {
|
||||
@@ -36,6 +33,25 @@ public class GeoCountryService {
|
||||
}
|
||||
}
|
||||
|
||||
public String getCountryCode(String input) {
|
||||
try {
|
||||
CountryResponse response = extractCountry(input);
|
||||
|
||||
return response.getCountry().getIsoCode();
|
||||
} catch (Exception e) {
|
||||
log.error("Failed for input: {}", input, e);
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
private CountryResponse extractCountry(String input) throws IOException, GeoIp2Exception {
|
||||
String domain = extractDomain(input);
|
||||
|
||||
InetAddress ip = InetAddress.getByName(domain);
|
||||
|
||||
return dbReader.country(ip);
|
||||
}
|
||||
|
||||
private String extractDomain(String input) {
|
||||
if (input == null || input.isEmpty()) {
|
||||
throw new IllegalArgumentException("Input is empty");
|
||||
|
||||
Reference in New Issue
Block a user