This commit is contained in:
@@ -62,6 +62,7 @@ dependencies {
|
||||
implementation name: 'testlib-fat-0.3.1-all'
|
||||
|
||||
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.12.0'
|
||||
implementation 'com.maxmind.geoip2:geoip2:4.2.0'
|
||||
|
||||
implementation project(':referral')
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import ru.soune.nocopy.entity.violation.Violation;
|
||||
import ru.soune.nocopy.service.geo.GeoCountryService;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@@ -12,6 +13,8 @@ import java.util.List;
|
||||
@Builder
|
||||
public class ViolationResponse {
|
||||
|
||||
private final GeoCountryService geoCountryService;
|
||||
|
||||
@JsonProperty("violations")
|
||||
private List<ViolationDto> violations;
|
||||
|
||||
@@ -58,6 +61,9 @@ public class ViolationResponse {
|
||||
@JsonProperty("created_date")
|
||||
private LocalDateTime createdDate;
|
||||
|
||||
@JsonProperty("country")
|
||||
private String country;
|
||||
|
||||
@JsonProperty("file_id")
|
||||
private String fileId;
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.violation.Violation;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
import ru.soune.nocopy.service.file.FileEntityService;
|
||||
import ru.soune.nocopy.service.geo.GeoCountryService;
|
||||
import ru.soune.nocopy.service.register.AuthService;
|
||||
import ru.soune.nocopy.service.violation.ViolationService;
|
||||
import ru.soune.nocopy.service.violation.ViolationStatus;
|
||||
@@ -21,6 +22,7 @@ import java.io.FileNotFoundException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -38,6 +40,8 @@ public class ViolationHandler implements RequestHandler {
|
||||
|
||||
private final FileEntityService fileEntityService;
|
||||
|
||||
private final GeoCountryService geoCountryService;
|
||||
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
@Override
|
||||
@@ -112,10 +116,20 @@ public class ViolationHandler implements RequestHandler {
|
||||
);
|
||||
}
|
||||
|
||||
List<ViolationResponse.ViolationDto> violationDtos = violationPage.getContent()
|
||||
.stream()
|
||||
.map(ViolationResponse.ViolationDto::fromEntity)
|
||||
.toList();
|
||||
List<Violation> content = violationPage.getContent();
|
||||
List<ViolationResponse.ViolationDto> violationDtos = new ArrayList<>();
|
||||
|
||||
// List<ViolationResponse.ViolationDto> violationDtos = violationPage.getContent()
|
||||
// .stream()
|
||||
// .map(ViolationResponse.ViolationDto::fromEntity)
|
||||
// .toList();
|
||||
|
||||
for (Violation violation : content) {
|
||||
ViolationResponse.ViolationDto violationDto = ViolationResponse.ViolationDto.fromEntity(violation);
|
||||
violationDto.setCountry(geoCountryService.getCountry(violation.getHost()));
|
||||
|
||||
violationDtos.add(violationDto);
|
||||
}
|
||||
|
||||
ViolationResponse response = ViolationResponse.builder()
|
||||
.violations(violationDtos)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package ru.soune.nocopy.service.geo;
|
||||
|
||||
import com.maxmind.geoip2.DatabaseReader;
|
||||
import com.maxmind.geoip2.model.CountryResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class GeoCountryService {
|
||||
|
||||
private final DatabaseReader dbReader;
|
||||
|
||||
public GeoCountryService() throws IOException {
|
||||
Resource resource = new ClassPathResource("geo/GeoLite2-Country.mmdb");
|
||||
this.dbReader = new DatabaseReader.Builder(resource.getInputStream()).build();
|
||||
}
|
||||
|
||||
public String getCountry(String input) {
|
||||
try {
|
||||
String domain = extractDomain(input);
|
||||
|
||||
InetAddress ip = InetAddress.getByName(domain);
|
||||
|
||||
CountryResponse response = dbReader.country(ip);
|
||||
|
||||
return response.getCountry().getIsoCode();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Failed for input: {}", input, e);
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
private String extractDomain(String input) {
|
||||
if (input == null || input.isEmpty()) {
|
||||
throw new IllegalArgumentException("Input is empty");
|
||||
}
|
||||
|
||||
String cleaned = input.replaceAll("^https?://", "");
|
||||
cleaned = cleaned.split("/")[0];
|
||||
cleaned = cleaned.split(":")[0];
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user