Use utf-8 for answer; Got rid of temp failure directory

This commit is contained in:
Mikhail Trofimov 2025-06-16 11:52:51 +03:00
parent a2a6512d9c
commit 073932748f
8 changed files with 61 additions and 45 deletions

View file

@ -0,0 +1,19 @@
package ru.nbch.credit_tracker.entities.response.success;
import jakarta.xml.bind.annotation.*;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MonitoringResponse", propOrder = {
"status",
})
@XmlRootElement(name = "MonitoringResponse")
public class MonitoringResponse {
@XmlElement(name = "Status", required = true)
protected String status = "OK";
}

View file

@ -13,13 +13,13 @@ import ru.nbch.credit_tracker.entities.monitoring_request.Subject;
import ru.nbch.credit_tracker.entities.signal_package.Signals; import ru.nbch.credit_tracker.entities.signal_package.Signals;
import ru.nbch.credit_tracker.enums.SignalError; import ru.nbch.credit_tracker.enums.SignalError;
import ru.nbch.credit_tracker.enums.Status; import ru.nbch.credit_tracker.enums.Status;
import ru.nbch.credit_tracker.service.FailureAnswerGenerator;
import ru.nbch.credit_tracker.service.SignalService; import ru.nbch.credit_tracker.service.SignalService;
import ru.nbch.credit_tracker.service.indic.PhoneService; import ru.nbch.credit_tracker.service.indic.PhoneService;
import ru.nbch.credit_tracker.service.scoring.AuthorizationService; import ru.nbch.credit_tracker.service.scoring.AuthorizationService;
import ru.nbch.credit_tracker.service.signal.SignalRestService; import ru.nbch.credit_tracker.service.signal.SignalRestService;
import ru.nbch.credit_tracker.service.xml.IXmlProcessor; import ru.nbch.credit_tracker.service.xml.FailureAnswerGenerator;
import ru.nbch.credit_tracker.service.xml.XmlUtilService; import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
import ru.nbch.credit_tracker.service.xml.jaxb.XmlUtilService;
import ru.nbch.credit_tracker.utils.ValidationUtil; import ru.nbch.credit_tracker.utils.ValidationUtil;
import java.util.HashSet; import java.util.HashSet;
@ -105,8 +105,8 @@ public class SignalFacade {
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
String errorPath = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100); Resource answer = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100);
return ResponseEntity.internalServerError().body(new PathResource(errorPath)); return ResponseEntity.internalServerError().body(answer);
} }
// Теперь знаем, что в архиве лежит ровно один файл с раширением xml и он весит меньше 2гб. Можно валидировать сам xml // Теперь знаем, что в архиве лежит ровно один файл с раширением xml и он весит меньше 2гб. Можно валидировать сам xml
@ -121,8 +121,8 @@ public class SignalFacade {
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
String errorPath = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100); Resource answer = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100);
return ResponseEntity.internalServerError().body(new PathResource(errorPath)); return ResponseEntity.internalServerError().body(answer);
} }
// валидация xml // валидация xml
@ -148,8 +148,8 @@ public class SignalFacade {
packageId = signalService.registerPackage(monitoringRequest); packageId = signalService.registerPackage(monitoringRequest);
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
String errorPath = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100); Resource answer = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100);
return ResponseEntity.internalServerError().body(new PathResource(errorPath)); return ResponseEntity.internalServerError().body(answer);
} }
// в отдельном потоке запускаем поиск фидов и расчет флагов просрочек // в отдельном потоке запускаем поиск фидов и расчет флагов просрочек
@ -245,8 +245,8 @@ public class SignalFacade {
} }
private ResponseEntity<Resource> getBadResponse(SignalError error, String tag) { private ResponseEntity<Resource> getBadResponse(SignalError error, String tag) {
String errorPath = failureAnswerGenerator.generateFailureAnswer(error, tag); Resource answer = failureAnswerGenerator.generateFailureAnswer(error, tag);
return ResponseEntity.badRequest().body(new PathResource(errorPath)); return ResponseEntity.badRequest().body(answer);
} }
private ResponseEntity<Resource> getBadResponse(SignalError error) { private ResponseEntity<Resource> getBadResponse(SignalError error) {

View file

@ -15,8 +15,8 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings; import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings;
import ru.nbch.credit_tracker.entities.signal_package.Signals; import ru.nbch.credit_tracker.entities.signal_package.Signals;
import ru.nbch.credit_tracker.service.xml.IXmlProcessor; import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
import ru.nbch.credit_tracker.service.xml.XmlUtilService; import ru.nbch.credit_tracker.service.xml.jaxb.XmlUtilService;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;

View file

@ -1,47 +1,37 @@
package ru.nbch.credit_tracker.service; package ru.nbch.credit_tracker.service.xml;
import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.JAXBException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.nbch.credit_tracker.entities.response.errors.MonitoringError; import ru.nbch.credit_tracker.entities.response.errors.MonitoringError;
import ru.nbch.credit_tracker.enums.SignalError; import ru.nbch.credit_tracker.enums.SignalError;
import ru.nbch.credit_tracker.service.xml.IXmlProcessor; import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
import ru.nbch.credit_tracker.service.xml.XmlUtilService; import ru.nbch.credit_tracker.service.xml.jaxb.XmlUtilService;
import java.io.File; import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.UUID;
@Slf4j @Slf4j
@Service @Service
public class FailureAnswerGenerator { public class FailureAnswerGenerator {
private static final String ANSWER_PATH = "temp_failure_response";
private final XmlUtilService xmlUtilService; private final XmlUtilService xmlUtilService;
public FailureAnswerGenerator(XmlUtilService xmlUtilService) { public FailureAnswerGenerator(XmlUtilService xmlUtilService) {
this.xmlUtilService = xmlUtilService; this.xmlUtilService = xmlUtilService;
} }
public String generateFailureAnswer(SignalError error) { public Resource generateFailureAnswer(SignalError error) {
return generateFailureAnswer(error, null); return generateFailureAnswer(error, null);
} }
public String generateFailureAnswer(SignalError error, String tag) { public Resource generateFailureAnswer(SignalError error, String tag) {
try { try {
File tempFolder = new File(ANSWER_PATH);
if (!tempFolder.exists()) {
tempFolder.mkdir();
}
String answerPath = ANSWER_PATH + File.separator + UUID.randomUUID() + File.separator + "error_reply.xml";
IXmlProcessor xmlProcessor = xmlUtilService.createJaxbProcessor(); IXmlProcessor xmlProcessor = xmlUtilService.createJaxbProcessor();
String errorMessage = xmlProcessor.asString(createErrorMessage(error, tag), false); String errorMessage = xmlProcessor.asString(createErrorMessage(error, tag), false, StandardCharsets.UTF_8.name());
FileUtils.writeStringToFile(new File(answerPath), errorMessage, Charset.forName("windows-1251")); byte[] xmlBytes = errorMessage.getBytes(StandardCharsets.UTF_8);
return new ByteArrayResource(xmlBytes);
return answerPath; } catch (JAXBException e) {
} catch (IOException | JAXBException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
return null; return null;

View file

@ -1,4 +1,4 @@
package ru.nbch.credit_tracker.service.xml; package ru.nbch.credit_tracker.service.xml.jaxb;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Path; import java.nio.file.Path;
@ -6,6 +6,8 @@ import java.nio.file.Path;
public interface IXmlProcessor { public interface IXmlProcessor {
String asString(Object xmlObj, boolean useStandalone); String asString(Object xmlObj, boolean useStandalone);
String asString(Object xmlObj, boolean useStandalone, String encoding);
void write(String path, Object xmlObj, boolean useStandalone); void write(String path, Object xmlObj, boolean useStandalone);
<Z> Z read(InputStream inputStream, Class<Z> clazz); <Z> Z read(InputStream inputStream, Class<Z> clazz);

View file

@ -1,4 +1,4 @@
package ru.nbch.credit_tracker.service.xml; package ru.nbch.credit_tracker.service.xml.jaxb;
import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.JAXBException;

View file

@ -1,4 +1,4 @@
package ru.nbch.credit_tracker.service.xml; package ru.nbch.credit_tracker.service.xml.jaxb;
import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.JAXBException;
@ -33,13 +33,13 @@ public abstract class XmlProcessorImplBase implements IXmlProcessor {
} }
@Override @Override
public String asString(Object xmlObj, boolean useStandalone) { public String asString(Object xmlObj, boolean useStandalone, String encoding) {
try { try {
StringWriter strWriter = new StringWriter(); StringWriter strWriter = new StringWriter();
if (useStandalone) { if (useStandalone) {
strWriter.write("<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>\n".formatted(charsetName)); strWriter.write("<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>\n".formatted(encoding));
} }
genMarshaller(xmlObj, useStandalone).marshal(xmlObj, strWriter); genMarshaller(xmlObj, useStandalone, encoding).marshal(xmlObj, strWriter);
return strWriter.toString(); return strWriter.toString();
} catch (Throwable e) { } catch (Throwable e) {
log.error("Error serializing object to XML string: {}", e.getMessage()); log.error("Error serializing object to XML string: {}", e.getMessage());
@ -47,13 +47,18 @@ public abstract class XmlProcessorImplBase implements IXmlProcessor {
} }
} }
@Override
public String asString(Object xmlObj, boolean useStandalone) {
return asString(xmlObj, useStandalone, charsetName);
}
@Override @Override
public void write(String path, Object xmlObj, boolean useStandalone) { public void write(String path, Object xmlObj, boolean useStandalone) {
try (OutputStream os = new FileOutputStream(path)) { try (OutputStream os = new FileOutputStream(path)) {
if (useStandalone) { if (useStandalone) {
os.write("<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>\n".formatted(charsetName).getBytes(charsetName)); os.write("<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>\n".formatted(charsetName).getBytes(charsetName));
} }
genMarshaller(xmlObj, useStandalone).marshal(xmlObj, os); genMarshaller(xmlObj, useStandalone, charsetName).marshal(xmlObj, os);
} catch (Throwable e) { } catch (Throwable e) {
log.error("Error serializing object to XML string: {}", e.getMessage(), e); log.error("Error serializing object to XML string: {}", e.getMessage(), e);
throw new IllegalStateException("cannot serialize object to XML"); throw new IllegalStateException("cannot serialize object to XML");
@ -93,10 +98,10 @@ public abstract class XmlProcessorImplBase implements IXmlProcessor {
} }
} }
public Marshaller genMarshaller(Object obj, boolean useStandalone) throws JAXBException { public Marshaller genMarshaller(Object obj, boolean useStandalone, String encoding) throws JAXBException {
Marshaller marshaller = getContextByObject(obj).createMarshaller(); Marshaller marshaller = getContextByObject(obj).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, charsetName); marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
if (useStandalone) { if (useStandalone) {
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
} else { } else {

View file

@ -1,4 +1,4 @@
package ru.nbch.credit_tracker.service.xml; package ru.nbch.credit_tracker.service.xml.jaxb;
import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.JAXBException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;