Do not use files for creating response to nbki service
This commit is contained in:
parent
c0ae69df4f
commit
69a86bbe57
1 changed files with 19 additions and 29 deletions
|
|
@ -2,9 +2,9 @@ package ru.nbch.credit_tracker.service.signal;
|
|||
|
||||
import jakarta.xml.bind.DatatypeConverter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
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.HttpHeaders;
|
||||
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.XmlUtilService;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
@Slf4j
|
||||
|
|
@ -48,16 +46,10 @@ public class SignalRestService {
|
|||
}
|
||||
|
||||
public String update(Signals signals) throws Exception {
|
||||
String folder = tmpFilePath + File.separator + UUID.randomUUID();
|
||||
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);
|
||||
Resource request = compressFile(signals);
|
||||
|
||||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||
body.add("file", fileResource);
|
||||
body.add("file", request);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
|
|
@ -67,9 +59,6 @@ public class SignalRestService {
|
|||
|
||||
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) {
|
||||
// good response
|
||||
return null;
|
||||
|
|
@ -80,20 +69,21 @@ public class SignalRestService {
|
|||
return "Непредвиденная ошибка";
|
||||
}
|
||||
|
||||
private String compressFile(String filePath, String folderPath) throws Exception {
|
||||
byte[] buffer = new byte[1024];
|
||||
String archivePath = folderPath + "arhive.gz";
|
||||
private Resource compressFile(Signals signals) throws Exception {
|
||||
IXmlProcessor processor = xmlUtilService.createJaxbProcessor();
|
||||
String requestData = processor.asString(signals, false);
|
||||
|
||||
FileInputStream fis = new FileInputStream(filePath);
|
||||
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(archivePath));
|
||||
|
||||
int bytesRead;
|
||||
while ((bytesRead = fis.read(buffer)) != -1) {
|
||||
gzos.write(buffer, 0, bytesRead);
|
||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream)) {
|
||||
gzipStream.write(requestData.getBytes(Charset.forName("windows-1251")));
|
||||
}
|
||||
gzos.close();
|
||||
fis.close();
|
||||
return archivePath;
|
||||
|
||||
return new ByteArrayResource(byteStream.toByteArray()) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return "arhive.gz";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String buildToken(String signalsToken, String signalsuser) throws Exception {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue