Load and unzip archive to disk

This commit is contained in:
Mikhail Trofimov 2025-07-21 09:40:38 +03:00
parent cb5f0d9a9b
commit ebb9cd0dc8
2 changed files with 37 additions and 38 deletions

View file

@ -31,7 +31,6 @@ import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -264,35 +263,8 @@ public class MonitoringService {
log.info("Signal hits updated for package {}", userPackage.getPackageName());
}
/**
* Loads .gz report from nbki service, unzips it to tmp_folder
*
* @return path to unzipped file. Example: <loadDir>/tmp_folder/<package_id>/0001XX_MY_BATCH_204.1.online.2025-07-08T20-00-46.xml.gz.xml
*/
public String loadReport(Integer packageId, String reportName) {
byte[] archive = signalRestService.downloadReport(reportName);
if (archive == null) {
return null;
}
String folderPath = config.getLoadDir() + File.separator + "tmp_folder" + File.separator + packageId + File.separator;
String filePath = folderPath + reportName + ".xml";
CTUtil.createFolder(folderPath);
try (InputStream in = new GZIPInputStream(new ByteArrayInputStream(archive));
OutputStream out = Files.newOutputStream(Path.of(filePath))) {
byte[] buffer = new byte[65536];
int bytesRead;
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
return filePath;
} catch (IOException e) {
log.error(e.getMessage(), e);
return null;
}
return signalRestService.downloadReport(packageId, reportName);
}
}

View file

@ -17,16 +17,16 @@ import ru.nbch.credit_tracker.entities.response.online_report.SgnlOnlineReport;
import ru.nbch.credit_tracker.service.SignalService;
import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
import ru.nbch.credit_tracker.service.xml.stax.StaxXmlProcessor;
import ru.nbch.credit_tracker.utils.CTUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.function.Predicate;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@Slf4j
@ -44,6 +44,8 @@ public class SignalRestService {
private final SignalService signalService;
private final String authToken;
private final CreditTrackerSettings config;
public SignalRestService(@Qualifier(value = "restTemplate") RestTemplate restTemplate,
IXmlProcessor xmlProcessor,
StaxXmlProcessor staxProcessor,
@ -53,6 +55,7 @@ public class SignalRestService {
this.xmlProcessor = xmlProcessor;
this.staxProcessor = staxProcessor;
this.signalService = signalService;
this.config = config;
this.authToken = buildToken(config.getSignalsToken(), config.getSignalsuser());
}
@ -145,7 +148,12 @@ public class SignalRestService {
}
}
public byte[] downloadReport(String reportName) {
/**
* Loads .gz report from nbki service, unzips it to tmp_folder
*
* @return path to unzipped file. Example: <loadDir>/tmp_folder/<package_id>/0001XX_MY_BATCH_204.1.online.2025-07-08T20-00-46.xml.gz.xml
*/
public String downloadReport(Integer packageId, String reportName) {
String url = DOWNLOAD_REPORT_URI + "?fNameReport=" + reportName;
HttpHeaders headers = new HttpHeaders();
@ -160,18 +168,37 @@ public class SignalRestService {
byte[].class
);
byte[] answer = response.getBody();
if (isXmlError(response.getBody())) {
SgnlReport error = staxProcessor.read(answer, SgnlReport.class);
boolean isGzipped = response.getHeaders().getContentType() != null
&& response.getHeaders().getContentType().toString().equals("application/x-gzip");
if (!isGzipped && isXmlError(response.getBody())) {
SgnlReport error = staxProcessor.read(response.getBody(), SgnlReport.class);
if (error.getError().getDescription() != null) {
log.error("Ошибка при скачивание отчета. fNameReport={}. Описание: {}", reportName, error.getError().getDescription());
return null;
}
log.error("Неизвестная ошибка при скачивание отчета. fNameReport={}", reportName);
return null;
}
} else {
String folderPath = config.getLoadDir() + File.separator + "tmp_folder" + File.separator + packageId + File.separator;
String filePath = folderPath + reportName + ".xml";
CTUtil.createFolder(folderPath);
return answer;
try (InputStream in = new GZIPInputStream(new ByteArrayInputStream(response.getBody()));
OutputStream out = Files.newOutputStream(Path.of(filePath))) {
byte[] buffer = new byte[65536];
int bytesRead;
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
return filePath;
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
}
private String buildToken(String signalsToken, String signalsuser) throws Exception {