From 86f39dfcaf2950e9ee9c6225e4f960a6437b98e5 Mon Sep 17 00:00:00 2001 From: Sebastian Zubrinic Date: Thu, 18 Aug 2022 18:36:49 -0300 Subject: [PATCH] PW-922: reduce log in MxSwiftMessage parse when the XML is not recognized (#59) --- CHANGELOG.txt | 3 + build.gradle | 4 +- .../swift/model/MxSwiftMessage.java | 8 +- .../swift/model/mx/MxReadImpl.java | 19 +++-- .../swift/model/mx/MxReadParams.java | 8 ++ .../swift/model/MxSwiftMessageTest.java | 74 +++++++++++++++++++ .../swift/model/mx/AbstractMXTest.java | 74 ++++++++++++++++++- 7 files changed, 180 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 97fab5c5b..9f83d9190 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -2,6 +2,9 @@ Prowide ISO 20022 - CHANGELOG ----------------------------------------------------------------------------------------------------------------------- +RELEASE 9.2.8 - August 2022 + * (PW-922) Added a parameter in the MxReadParams used by the AbstractMX#parse to control the log verbosity when parsing unrecognized messages + RELEASE 9.2.7 - August 2022 * Added model for "trck" types * (GH-45) Fixed Json serialization in Java 17 diff --git a/build.gradle b/build.gradle index bb6ce8c22..dfc3eb121 100644 --- a/build.gradle +++ b/build.gradle @@ -113,7 +113,7 @@ configure(subprojects.findAll {it.name.endsWith('-types')}) { project(':iso20022-core') { dependencies { // included build - api 'com.prowidesoftware:pw-swift-core:SRU2021-9.2.16' + api 'com.prowidesoftware:pw-swift-core:SRU2021-9.2.17' implementation 'org.apache.commons:commons-lang3:3.12.0' implementation 'com.google.code.gson:gson:2.8.9' @@ -227,7 +227,7 @@ artifacts { // declared dependencies for pom generation dependencies { // included build (keep in sync with the latest Prowide Core version) - api 'com.prowidesoftware:pw-swift-core:SRU2021-9.2.16' + api 'com.prowidesoftware:pw-swift-core:SRU2021-9.2.17' implementation 'org.apache.commons:commons-lang3:3.12.0' implementation 'com.google.code.gson:gson:2.8.9' } diff --git a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/MxSwiftMessage.java b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/MxSwiftMessage.java index 0722b07f6..5c32fb767 100644 --- a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/MxSwiftMessage.java +++ b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/MxSwiftMessage.java @@ -505,7 +505,13 @@ public void updateMetadata(MessageMetadataStrategy strategy) { private void applyStrategy(String xml, MessageMetadataStrategy strategy) { boolean isKnownType = this.businessProcess != null && this.functionality != null && this.variant != null && this.version != null; - AbstractMX mx = isKnownType ? AbstractMX.parse(xml, getMxId()) : AbstractMX.parse(xml); + MxId mxId = isKnownType ? getMxId() : null; + + // when parsing the message just for the metadata extraction, we want to avoid underlying error logs + // since this MxSwiftMessage is lenient on the constraints of the parsed XML payload + MxReadParams params = new MxReadParams(); + params.verbose = false; + AbstractMX mx = MxReadImpl.parse(xml, mxId, params); if (mx == null) { // could not parse the XML into a message model diff --git a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxReadImpl.java b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxReadImpl.java index 58ac7a14e..740c8c69d 100644 --- a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxReadImpl.java +++ b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxReadImpl.java @@ -51,7 +51,7 @@ * @since 9.0 */ public class MxReadImpl implements MxRead { - private static final transient Logger log = Logger.getLogger(MxReadImpl.class.getName()); + private static final Logger log = Logger.getLogger(MxReadImpl.class.getName()); /** * Static parse implementation of {@link MxRead#read(Class, String, Class[])} @@ -109,7 +109,7 @@ public static AbstractMX parse(final String xml, MxId id) { /** * @since 9.2.6 */ - static AbstractMX parse(final String xml, final MxId id, final MxReadParams params) { + public static AbstractMX parse(final String xml, final MxId id, final MxReadParams params) { Objects.requireNonNull(xml, "XML to parse must not be null"); Validate.notBlank(xml, "XML to parse must not be a blank string"); Objects.requireNonNull(params, "unmarshalling params cannot be null"); @@ -121,7 +121,8 @@ static AbstractMX parse(final String xml, final MxId id, final MxReadParams para if (namespace.isPresent()) { resolvedId = new MxId(namespace.get()); } else { - log.severe("Cannot detect the Mx type from the XML, ensure the XML contains proper namespaces or provide an MxId object as parameter to the parse call"); + Level level = params.verbose? Level.SEVERE: Level.FINE; + log.log(level, "Cannot detect the Mx type from the XML, make sure the XML contains proper namespaces or provide an MxId object as parameter to the parse call"); return null; } } @@ -135,9 +136,17 @@ static AbstractMX parse(final String xml, final MxId id, final MxReadParams para java.lang.reflect.Field _classes = clazz.getDeclaredField("_classes"); mx = parse(clazz, xml, (Class[]) _classes.get(null), params); } catch (ClassNotFoundException e) { - log.log(Level.SEVERE, "MX model implementation not found for " + fqn, e); + if (params.verbose) { + log.log(Level.SEVERE, "Cannot find class " + fqn + " to parse the XML", e); + } else { + log.fine("MX model implementation not found for " + fqn); + } } catch (Exception e) { - log.log(Level.SEVERE, "Error calling parse in specific MX model implementation", e); + if (params.verbose) { + log.log(Level.SEVERE, "Error calling parse in specific MX model implementation", e); + } else { + log.fine("Error calling parse in specific MX model implementation"); + } } return mx; } diff --git a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxReadParams.java b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxReadParams.java index 363dbfa4a..4a65e3f0c 100644 --- a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxReadParams.java +++ b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxReadParams.java @@ -29,6 +29,14 @@ public class MxReadParams { */ public TypeAdaptersConfiguration adapters; + /** + * When true; errors during parsing, such as a ClassNotFoundException, will generate a log error, while when set + * to false those errors will generate finer log entries. + * + * @since 9.2.8 + */ + public boolean verbose = true; + public MxReadParams() { this.adapters = new TypeAdaptersConfiguration(); } diff --git a/iso20022-core/src/test/java/com/prowidesoftware/swift/model/MxSwiftMessageTest.java b/iso20022-core/src/test/java/com/prowidesoftware/swift/model/MxSwiftMessageTest.java index 9a917a5d2..8adebbfe2 100644 --- a/iso20022-core/src/test/java/com/prowidesoftware/swift/model/MxSwiftMessageTest.java +++ b/iso20022-core/src/test/java/com/prowidesoftware/swift/model/MxSwiftMessageTest.java @@ -227,4 +227,78 @@ public void category() { assertEquals("camt", mx.getCategory()); } + /** + * Tests the MxSwiftMessage can parse an XML, even if the corresponding AbstractMX subclass is missing + */ + @Test + public void testUnrecognizedMessage() { + String xmlInput = "\n" + + "\n" + + " \n" + + " 12345\n" + + " 2022-05-24T10:29:05+08:00\n" + + " \n" + + " \n" + + " Tax Invoice for RTGS Billing\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " The Monetary Authority of Foobar\n" + + " \n" + + " 99 Foo Way, FOO Building 012345\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " M12343076J\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " FOO BANK OF FOOBAR\n" + + " \n" + + " 44 Street Unit 27-01/08 Foobar Hub 012345\n" + + " \n" + + " \n" + + " See further information on the RTGS Billing Detail screen\n" + + " \n" + + " 2022-05-24\n" + + " I22/123/0123\n" + + " \n" + + " 2022-05-25\n" + + " 2022-05-25\n" + + " \n" + + " \n" + + " \n" + + " 7\n" + + " 0.30\n" + + " 0.02\n" + + " \n" + + " 0.32\n" + + " 2022-05-24\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " 12340100\n" + + " \n" + + " \n" + + " \n" + + " 0.32\n" + + " CHAR\n" + + " \n" + + " \n" + + " \n" + + "\n" + + "\n"; + MxSwiftMessage mx = new MxSwiftMessage(xmlInput); + assertNotNull(mx); + assertEquals("camt.077.001.01", mx.getIdentifier()); + } } diff --git a/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/AbstractMXTest.java b/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/AbstractMXTest.java index ea352f9af..47b9266df 100644 --- a/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/AbstractMXTest.java +++ b/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/AbstractMXTest.java @@ -22,8 +22,6 @@ /** * Test for the Mx parser API in the base {@link AbstractMX} class - * - * @since 9.0 */ public class AbstractMXTest { @@ -113,6 +111,78 @@ public void testParseXsys() { " \n" + " "; AbstractMX mx = AbstractMX.parse(xml); + assertNotNull(mx); + } + + @Test + public void testUnrecognizedMessage() { + String xml = "\n" + + "\n" + + " \n" + + " 12345\n" + + " 2022-05-24T10:29:05+08:00\n" + + " \n" + + " \n" + + " Tax Invoice for RTGS Billing\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " The Monetary Authority of Foobar\n" + + " \n" + + " 99 Foo Way, FOO Building 012345\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " M12343076J\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " FOO BANK OF FOOBAR\n" + + " \n" + + " 44 Street Unit 27-01/08 Foobar Hub 012345\n" + + " \n" + + " \n" + + " See further information on the RTGS Billing Detail screen\n" + + " \n" + + " 2022-05-24\n" + + " I22/123/0123\n" + + " \n" + + " 2022-05-25\n" + + " 2022-05-25\n" + + " \n" + + " \n" + + " \n" + + " 7\n" + + " 0.30\n" + + " 0.02\n" + + " \n" + + " 0.32\n" + + " 2022-05-24\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " 12340100\n" + + " \n" + + " \n" + + " \n" + + " 0.32\n" + + " CHAR\n" + + " \n" + + " \n" + + " \n" + + "\n" + + "\n"; + AbstractMX mx = AbstractMX.parse(xml); + assertNull(mx); } }