Do not use files for creating response to nbki service

This commit is contained in:
Mikhail Trofimov 2025-06-16 16:28:39 +03:00
parent c0ae69df4f
commit 69a86bbe57

View file

@ -2,9 +2,9 @@ package ru.nbch.credit_tracker.service.signal;
import jakarta.xml.bind.DatatypeConverter; import jakarta.xml.bind.DatatypeConverter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.PathResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -18,13 +18,11 @@ import ru.nbch.credit_tracker.entities.signal_package.Signals;
import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor; import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
import ru.nbch.credit_tracker.service.xml.jaxb.XmlUtilService; import ru.nbch.credit_tracker.service.xml.jaxb.XmlUtilService;
import java.io.File; import java.io.ByteArrayOutputStream;
import java.io.FileInputStream; import java.nio.charset.Charset;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Base64; import java.util.Base64;
import java.util.UUID;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@Slf4j @Slf4j
@ -48,16 +46,10 @@ public class SignalRestService {
} }
public String update(Signals signals) throws Exception { public String update(Signals signals) throws Exception {
String folder = tmpFilePath + File.separator + UUID.randomUUID(); Resource request = compressFile(signals);
String path = folder + File.separator + "reply.xml";
IXmlProcessor processor = xmlUtilService.createJaxbProcessor();
processor.write(path, signals, false);
String archivePath = compressFile(path, folder);
PathResource fileResource = new PathResource(archivePath);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", fileResource); body.add("file", request);
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.setContentType(MediaType.MULTIPART_FORM_DATA);
@ -67,9 +59,6 @@ public class SignalRestService {
ResponseEntity<String> response = restTemplate.postForEntity(UPDATE_URI, requestEntity, String.class); ResponseEntity<String> response = restTemplate.postForEntity(UPDATE_URI, requestEntity, String.class);
FileUtils.delete(FileUtils.getFile(path));
FileUtils.delete(FileUtils.getFile(archivePath));
if (response.getStatusCode().value() == 302) { if (response.getStatusCode().value() == 302) {
// good response // good response
return null; return null;
@ -80,20 +69,21 @@ public class SignalRestService {
return "Непредвиденная ошибка"; return "Непредвиденная ошибка";
} }
private String compressFile(String filePath, String folderPath) throws Exception { private Resource compressFile(Signals signals) throws Exception {
byte[] buffer = new byte[1024]; IXmlProcessor processor = xmlUtilService.createJaxbProcessor();
String archivePath = folderPath + "arhive.gz"; String requestData = processor.asString(signals, false);
FileInputStream fis = new FileInputStream(filePath); ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(archivePath)); try (GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream)) {
gzipStream.write(requestData.getBytes(Charset.forName("windows-1251")));
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
gzos.write(buffer, 0, bytesRead);
} }
gzos.close();
fis.close(); return new ByteArrayResource(byteStream.toByteArray()) {
return archivePath; @Override
public String getFilename() {
return "arhive.gz";
}
};
} }
private String buildToken(String signalsToken, String signalsuser) throws Exception { private String buildToken(String signalsToken, String signalsuser) throws Exception {