Use utf-8 for answer; Got rid of temp failure directory
This commit is contained in:
parent
a2a6512d9c
commit
073932748f
8 changed files with 61 additions and 45 deletions
|
|
@ -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";
|
||||
|
||||
}
|
||||
|
|
@ -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.enums.SignalError;
|
||||
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.indic.PhoneService;
|
||||
import ru.nbch.credit_tracker.service.scoring.AuthorizationService;
|
||||
import ru.nbch.credit_tracker.service.signal.SignalRestService;
|
||||
import ru.nbch.credit_tracker.service.xml.IXmlProcessor;
|
||||
import ru.nbch.credit_tracker.service.xml.XmlUtilService;
|
||||
import ru.nbch.credit_tracker.service.xml.FailureAnswerGenerator;
|
||||
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 java.util.HashSet;
|
||||
|
|
@ -105,8 +105,8 @@ public class SignalFacade {
|
|||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String errorPath = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100);
|
||||
return ResponseEntity.internalServerError().body(new PathResource(errorPath));
|
||||
Resource answer = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100);
|
||||
return ResponseEntity.internalServerError().body(answer);
|
||||
}
|
||||
|
||||
// Теперь знаем, что в архиве лежит ровно один файл с раширением xml и он весит меньше 2гб. Можно валидировать сам xml
|
||||
|
|
@ -121,8 +121,8 @@ public class SignalFacade {
|
|||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String errorPath = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100);
|
||||
return ResponseEntity.internalServerError().body(new PathResource(errorPath));
|
||||
Resource answer = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100);
|
||||
return ResponseEntity.internalServerError().body(answer);
|
||||
}
|
||||
|
||||
// валидация xml
|
||||
|
|
@ -148,8 +148,8 @@ public class SignalFacade {
|
|||
packageId = signalService.registerPackage(monitoringRequest);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String errorPath = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100);
|
||||
return ResponseEntity.internalServerError().body(new PathResource(errorPath));
|
||||
Resource answer = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_100);
|
||||
return ResponseEntity.internalServerError().body(answer);
|
||||
}
|
||||
|
||||
// в отдельном потоке запускаем поиск фидов и расчет флагов просрочек
|
||||
|
|
@ -245,8 +245,8 @@ public class SignalFacade {
|
|||
}
|
||||
|
||||
private ResponseEntity<Resource> getBadResponse(SignalError error, String tag) {
|
||||
String errorPath = failureAnswerGenerator.generateFailureAnswer(error, tag);
|
||||
return ResponseEntity.badRequest().body(new PathResource(errorPath));
|
||||
Resource answer = failureAnswerGenerator.generateFailureAnswer(error, tag);
|
||||
return ResponseEntity.badRequest().body(answer);
|
||||
}
|
||||
|
||||
private ResponseEntity<Resource> getBadResponse(SignalError error) {
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ import org.springframework.util.MultiValueMap;
|
|||
import org.springframework.web.client.RestTemplate;
|
||||
import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings;
|
||||
import ru.nbch.credit_tracker.entities.signal_package.Signals;
|
||||
import ru.nbch.credit_tracker.service.xml.IXmlProcessor;
|
||||
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 java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
|
|
|||
|
|
@ -1,47 +1,37 @@
|
|||
package ru.nbch.credit_tracker.service;
|
||||
package ru.nbch.credit_tracker.service.xml;
|
||||
|
||||
import jakarta.xml.bind.JAXBException;
|
||||
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 ru.nbch.credit_tracker.entities.response.errors.MonitoringError;
|
||||
import ru.nbch.credit_tracker.enums.SignalError;
|
||||
import ru.nbch.credit_tracker.service.xml.IXmlProcessor;
|
||||
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 java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.UUID;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class FailureAnswerGenerator {
|
||||
private static final String ANSWER_PATH = "temp_failure_response";
|
||||
private final XmlUtilService xmlUtilService;
|
||||
|
||||
public FailureAnswerGenerator(XmlUtilService xmlUtilService) {
|
||||
this.xmlUtilService = xmlUtilService;
|
||||
}
|
||||
|
||||
public String generateFailureAnswer(SignalError error) {
|
||||
public Resource generateFailureAnswer(SignalError error) {
|
||||
return generateFailureAnswer(error, null);
|
||||
}
|
||||
|
||||
public String generateFailureAnswer(SignalError error, String tag) {
|
||||
public Resource generateFailureAnswer(SignalError error, String tag) {
|
||||
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();
|
||||
String errorMessage = xmlProcessor.asString(createErrorMessage(error, tag), false);
|
||||
FileUtils.writeStringToFile(new File(answerPath), errorMessage, Charset.forName("windows-1251"));
|
||||
|
||||
return answerPath;
|
||||
} catch (IOException | JAXBException e) {
|
||||
String errorMessage = xmlProcessor.asString(createErrorMessage(error, tag), false, StandardCharsets.UTF_8.name());
|
||||
byte[] xmlBytes = errorMessage.getBytes(StandardCharsets.UTF_8);
|
||||
return new ByteArrayResource(xmlBytes);
|
||||
} catch (JAXBException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
|
|
@ -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.nio.file.Path;
|
||||
|
|
@ -6,6 +6,8 @@ import java.nio.file.Path;
|
|||
public interface IXmlProcessor {
|
||||
String asString(Object xmlObj, boolean useStandalone);
|
||||
|
||||
String asString(Object xmlObj, boolean useStandalone, String encoding);
|
||||
|
||||
void write(String path, Object xmlObj, boolean useStandalone);
|
||||
|
||||
<Z> Z read(InputStream inputStream, Class<Z> clazz);
|
||||
|
|
@ -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.JAXBException;
|
||||
|
|
@ -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.JAXBException;
|
||||
|
|
@ -33,13 +33,13 @@ public abstract class XmlProcessorImplBase implements IXmlProcessor {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String asString(Object xmlObj, boolean useStandalone) {
|
||||
public String asString(Object xmlObj, boolean useStandalone, String encoding) {
|
||||
try {
|
||||
StringWriter strWriter = new StringWriter();
|
||||
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();
|
||||
} catch (Throwable e) {
|
||||
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
|
||||
public void write(String path, Object xmlObj, boolean useStandalone) {
|
||||
try (OutputStream os = new FileOutputStream(path)) {
|
||||
if (useStandalone) {
|
||||
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) {
|
||||
log.error("Error serializing object to XML string: {}", e.getMessage(), e);
|
||||
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.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, charsetName);
|
||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
|
||||
if (useStandalone) {
|
||||
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
|
||||
} else {
|
||||
|
|
@ -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 org.springframework.stereotype.Service;
|
||||
Loading…
Add table
Reference in a new issue