Added new method for stax reader to read from inputstream

This commit is contained in:
Mikhail Trofimov 2025-07-16 10:20:14 +03:00
parent 41582e676d
commit 1815596a7b
5 changed files with 25 additions and 5 deletions

View file

@ -97,7 +97,7 @@ public class SignalFacade {
zipInputStream = new ZipInputStream(file.getInputStream());
zipInputStream.getNextEntry();
monitoringRequest = staxXmlProcessor.read(zipInputStream.readAllBytes(), MonitoringRequest.class);
monitoringRequest = staxXmlProcessor.read(zipInputStream, MonitoringRequest.class);
} catch (Exception e) {
if (e.getCause() instanceof XMLStreamException) {
throw clientExcp(SignalError.ERROR_011, XmlErrorFormat.MonitoringError);

View file

@ -1,6 +1,9 @@
package ru.nbch.credit_tracker.service.xml.stax;
import java.io.InputStream;
public interface StaxXmlProcessor {
<Z> Z read(String xml, Class<Z> clazz);
<Z> Z read(byte[] xml, Class<Z> clazz);
<Z> Z read(InputStream inputStream, Class<Z> clazz);
}

View file

@ -8,6 +8,7 @@ import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashMap;
@ -66,4 +67,20 @@ public class XmlProcessorStaxImpl implements StaxXmlProcessor {
throw new RuntimeException(e);
}
}
@Override
public <Z> Z read(InputStream inputStream, Class<Z> clazz) {
try {
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream);
@SuppressWarnings("unchecked")
StaxReader<Z> staxProcessor = (StaxReader<Z>) staxReaders.get(clazz);
if (staxProcessor != null) {
return staxProcessor.read(xmlStreamReader);
}
throw new IllegalStateException("no stax reader registered for " + clazz);
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
}

View file

@ -58,7 +58,7 @@ public class StaxTest {
void testInvalidXmlRequest() {
Exception ex = null;
try {
staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/invalid_xml_request.xml").readAllBytes(), MonitoringRequest.class);
staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/invalid_xml_request.xml"), MonitoringRequest.class);
} catch (Exception e) {
ex = e;
}
@ -70,7 +70,7 @@ public class StaxTest {
void testEmptyXmlRequest() {
Exception ex = null;
try {
staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/empty_request.xml").readAllBytes(), MonitoringRequest.class);
staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/empty_request.xml"), MonitoringRequest.class);
} catch (Exception e) {
ex = e;
}
@ -80,7 +80,7 @@ public class StaxTest {
@Test
void testNonEpectedTag() throws IOException {
MonitoringRequest monitoringRequest = staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/non_expected_tag_request.xml").readAllBytes(), MonitoringRequest.class);
MonitoringRequest monitoringRequest = staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/non_expected_tag_request.xml"), MonitoringRequest.class);
Assertions.assertNull(monitoringRequest.getSubjects().getSubject().get(0).getId());
}

View file

@ -137,7 +137,7 @@ public class UtilTest {
try {
zipInputStream = new ZipInputStream(getClass().getResourceAsStream(path));
zipInputStream.getNextEntry();
monitoringRequest = staxXmlProcessor.read(zipInputStream.readAllBytes(), MonitoringRequest.class);
monitoringRequest = staxXmlProcessor.read(zipInputStream, MonitoringRequest.class);
} catch (Exception e) {
if (e.getCause() instanceof XMLStreamException) {
ex = clientExcp(SignalError.ERROR_011, XmlErrorFormat.MonitoringError);