From c60aeec6f5b95800211457ebbc6571e572fe95a7 Mon Sep 17 00:00:00 2001 From: vladp Date: Wed, 15 Apr 2026 10:25:25 +0700 Subject: [PATCH] dev add geo country service --- .../dto/violation/ViolationResponse.java | 3 ++ .../nocopy/handler/ViolationHandler.java | 3 +- .../nocopy/service/geo/GeoCountryService.java | 28 +++++++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/soune/nocopy/dto/violation/ViolationResponse.java b/src/main/java/ru/soune/nocopy/dto/violation/ViolationResponse.java index 9c0bf7e..8a588c6 100644 --- a/src/main/java/ru/soune/nocopy/dto/violation/ViolationResponse.java +++ b/src/main/java/ru/soune/nocopy/dto/violation/ViolationResponse.java @@ -64,6 +64,9 @@ public class ViolationResponse { @JsonProperty("country") private String country; + @JsonProperty("country_code") + private String countryCode; + @JsonProperty("file_id") private String fileId; diff --git a/src/main/java/ru/soune/nocopy/handler/ViolationHandler.java b/src/main/java/ru/soune/nocopy/handler/ViolationHandler.java index f900c90..5344fd2 100644 --- a/src/main/java/ru/soune/nocopy/handler/ViolationHandler.java +++ b/src/main/java/ru/soune/nocopy/handler/ViolationHandler.java @@ -121,7 +121,8 @@ public class ViolationHandler implements RequestHandler { for (Violation violation : content) { ViolationResponse.ViolationDto violationDto = ViolationResponse.ViolationDto.fromEntity(violation); - violationDto.setCountry(geoCountryService.getCountry(violation.getPageUrl())); + violationDto.setCountry(geoCountryService.getCountryName(violation.getPageUrl())); + violationDto.setCountryCode(geoCountryService.getCountryCode(violation.getPageUrl())); violationDtos.add(violationDto); } diff --git a/src/main/java/ru/soune/nocopy/service/geo/GeoCountryService.java b/src/main/java/ru/soune/nocopy/service/geo/GeoCountryService.java index 1a2e662..68ca2e6 100644 --- a/src/main/java/ru/soune/nocopy/service/geo/GeoCountryService.java +++ b/src/main/java/ru/soune/nocopy/service/geo/GeoCountryService.java @@ -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");