Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PW-1462: Parse refactor to support Any inner content in xsys messages #97

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@

import com.prowidesoftware.swift.model.MxBusinessProcess;
import com.prowidesoftware.swift.model.MxId;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang3.Validate;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
* Default implementation of the {@link MxRead} interface to parse XML strings into Mx message objects.
Expand Down Expand Up @@ -51,6 +69,13 @@
*/
public class MxReadImpl implements MxRead {
private static final Logger log = Logger.getLogger(MxReadImpl.class.getName());
private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
private static final XPathFactory X_PATH_FACTORY = XPathFactory.newInstance();

static {
DOCUMENT_BUILDER_FACTORY.setNamespaceAware(true);
}

/**
* Static parse implementation of {@link MxRead#read(Class, String, Class[])}
Expand Down Expand Up @@ -78,14 +103,8 @@ public static AbstractMX parse(
Objects.requireNonNull(params, "The unmarshalling params cannot be null");

try {

SAXSource documentSource = MxParseUtils.createFilteredSAXSource(xml, AbstractMX.DOCUMENT_LOCALNAME);
final AbstractMX parsedDocument =
(AbstractMX) MxParseUtils.parseSAXSource(documentSource, targetClass, classes, params);

Optional<AbstractMX> mx = Optional.ofNullable(parsedDocument);

Optional<AppHdr> appHdr = AppHdrParser.parse(xml, params);
Optional<AbstractMX> mx = getAbstractMX(targetClass, xml, classes, params);

if (mx.isPresent() && appHdr.isPresent()) {
mx.get().setAppHdr(appHdr.get());
Expand All @@ -99,6 +118,38 @@ public static AbstractMX parse(
}
}

private static Optional<AbstractMX> getAbstractMX(
Class<? extends AbstractMX> targetClass, String xml, Class<?>[] classes, MxReadParams params) {
// String documentXml = getDocumentXml(xml, params);
SAXSource documentSource = MxParseUtils.createFilteredSAXSource(xml, AbstractMX.DOCUMENT_LOCALNAME);
final AbstractMX parsedDocument =
(AbstractMX) MxParseUtils.parseSAXSource(documentSource, targetClass, classes, params);
return Optional.ofNullable(parsedDocument);
}

private static String getDocumentXml(String xml, MxReadParams params) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Este metodo no se usa por lo que veo. Es el intento por lo de Any? En general hay que evitar usar DOM, es muy lento, por eso usamos Sax

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

La llamada a este método está comentada. Habíamos quedado en que lo subía así y vos revisabas si no encontrabas una solución mejor. Este branch fue una 1era idea que no te convencía.

StringWriter writer = new StringWriter();
try {
Document doc = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
Node documentNode =
(Node) X_PATH_FACTORY.newXPath().evaluate("//*[local-name()='Document']", doc, XPathConstants.NODE);
Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // Omit the XML declaration
transformer.transform(new DOMSource(documentNode), new StreamResult(writer));
return writer.toString();
} catch (SAXException
| ParserConfigurationException
| XPathExpressionException
| TransformerException
| IOException e) {
Level level = params.verbose ? Level.SEVERE : Level.FINE;
log.log(
level,
"Cannot extract the Document info from the XML, make sure the XML contains proper namespaces and tags");
return xml;
}
}

/**
* Static parse implementation of {@link MxRead#read(String, MxId)}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang3.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -91,7 +94,7 @@ public void startElement(String namespace, String localName, String prefix, Attr
try {
super.startElement(namespaceToPropagate, localName, prefix, attributes);
} catch (Exception e) {
log.log(Level.WARNING, "Error parsing " + localName + " [" + namespace + "] element", e);
log.log(Level.WARNING, "Error parsing " + localName + " [" + namespace + "] start element", e);
}
} else {
// we have found an element within the structure to propagate with a not recognized namespace
Expand Down Expand Up @@ -150,7 +153,7 @@ public void endElement(String namespace, String localName, String prefix) throws
try {
super.endElement(namespaceToPropagate, localName, prefix);
} catch (Exception e) {
log.log(Level.WARNING, "Error parsing " + localName + " [" + namespace + "] element", e);
log.log(Level.WARNING, "Error parsing " + localName + " [" + namespace + "] end element", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.prowidesoftware.issues;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.prowidesoftware.swift.model.MxId;
import com.prowidesoftware.swift.model.mx.AbstractMX;
import com.prowidesoftware.swift.model.mx.AppHdr;
import com.prowidesoftware.swift.model.mx.AppHdrParser;
import com.prowidesoftware.swift.model.mx.sys.MxXsys00200101;
import com.prowidesoftware.swift.model.mx.sys.dic.SwAny;
import com.prowidesoftware.swift.utils.Lib;
import java.io.IOException;
import java.util.Optional;

import org.junit.jupiter.api.Test;

class Jira1462Test {

@Test
void test() throws IOException {
String xml = Lib.readResource("sample_hvcs_xsys.002_pacs.008.xml");
final AbstractMX read = AbstractMX.parse(xml, new MxId("xsys", "002", "001", "01"));
assertNotNull(read, "read object null ");
assertTrue(read instanceof MxXsys00200101);

final MxXsys00200101 mx = (MxXsys00200101) read;
assertNotNull(mx);
assertNotNull(mx.getMessageStandardType());
assertNotNull(mx.getAppHdr());
assertEquals(
"swi00003-2023-07-19T04:56:40.63614.012852Z",
mx.getXsys00200101().getAuthstnNtfctn().getSnFRef());

SwAny thirdPartyToSenderInformation =
mx.getXsys00200101().getAuthstnNtfctn().getThirdPartyToSenderInformation();
assertNotNull(thirdPartyToSenderInformation);
assertFalse(thirdPartyToSenderInformation.getAny().isEmpty());
System.out.println("MxXsys00200101 WITH TEXT: " + mx.message());
}

@Test
void testWithNamespaces() throws IOException {
String xml = Lib.readResource("sample_hvcs_xsys.002_pacs.008_namespaces.xml");
Optional<AppHdr> parse = AppHdrParser.parse(xml);
assertTrue(parse.isPresent());

final AbstractMX read = AbstractMX.parse(xml, new MxId("xsys", "002", "001", "01"));
assertNotNull(read, "read object null ");
assertTrue(read instanceof MxXsys00200101);

final MxXsys00200101 mx = (MxXsys00200101) read;
assertNotNull(mx);
assertNotNull(mx.getMessageStandardType());
assertEquals(
"swi00003-2023-07-19T04:56:40.63614.012852Z",
mx.getXsys00200101().getAuthstnNtfctn().getSnFRef());

SwAny thirdPartyToSenderInformation =
mx.getXsys00200101().getAuthstnNtfctn().getThirdPartyToSenderInformation();
assertNotNull(thirdPartyToSenderInformation);
assertFalse(thirdPartyToSenderInformation.getAny().isEmpty());
System.out.println("MxXsys00200101 WITH TEXT: " + mx.message());
}

@Test
void withoutNoSwAny() throws IOException {
final AbstractMX read = AbstractMX.parse(
Lib.readResource("sample_hvcs_xsys.002_pacs.008_noSw.xml"), new MxId("xsys", "002", "001", "01"));
assertNotNull(read, "read object null ");
assertTrue(read instanceof MxXsys00200101);

final MxXsys00200101 mx = (MxXsys00200101) read;
assertNotNull(mx);
assertNotNull(mx.getMessageStandardType());
assertEquals(
"swi00003-2023-07-19T04:56:40.63614.012852Z",
mx.getXsys00200101().getAuthstnNtfctn().getSnFRef());

SwAny thirdPartyToSenderInformation =
mx.getXsys00200101().getAuthstnNtfctn().getThirdPartyToSenderInformation();
assertNotNull(thirdPartyToSenderInformation);
// assertFalse(thirdPartyToSenderInformation.getAny().isEmpty());
// System.out.println("MxXsys00200101 WITH TEXT: " + mx.message());
}
}
31 changes: 31 additions & 0 deletions iso20022-core/src/test/resources/sample_hvcs_xsys.002_pacs.008.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<RequestPayload>
<Ah:AppHdr xmlns:Ah="urn:swift:xsd:$ahV10">
<Ah:MsgRef>2023-07-19T04:57:05Z</Ah:MsgRef>
<Ah:CrDate>2023-07-19T04:57:05Z</Ah:CrDate>
</Ah:AppHdr>
<Doc:Document xmlns:Doc="urn:swift:xsd:xsys.002.001.01" xmlns:Sw="urn:swift:snl:ns.Sw"
xmlns:SwInt="urn:swift:snl:ns.SwInt"
xmlns:SwSec="urn:swift:snl:ns.SwSec">
<Doc:xsys.002.001.01>
<Doc:AuthstnNtfctn>
<Sw:SnFRef>swi00003-2022-07-19T04:56:40.63614.012852Z</Sw:SnFRef>
<SwInt:RequestHeader>
<SwInt:Requestor>ou=xxx,o=rabcdu2s,o=swift</SwInt:Requestor>
<SwInt:Responder>ou=xxx,o=wvcxzsr,o=swift</SwInt:Responder>
<SwInt:Service>apn.hvcs!p</SwInt:Service>
<SwInt:RequestType>pacs.004.001.10</SwInt:RequestType>
<SwInt:Priority>Normal</SwInt:Priority>
<SwInt:RequestRef>201207190001100005007HV0</SwInt:RequestRef>
</SwInt:RequestHeader>
<Sw:ThirdPartyToSenderInformation>
<Sw:ESABal Ccy="AUD">999999.99</Sw:ESABal>
<Sw:RcvdDtTm>2023-07-19T14:56:42.000+10:00</Sw:RcvdDtTm>
<Sw:IntrBkSttlmDtTm>2023-07-19T14:56:46.000+10:00</Sw:IntrBkSttlmDtTm>
<Sw:IntrBkSttlmAmt Ccy="AUD">99.99</Sw:IntrBkSttlmAmt>
</Sw:ThirdPartyToSenderInformation>
<SwSec:SignatureValue>SignatureContent</SwSec:SignatureValue>
</Doc:AuthstnNtfctn>
</Doc:xsys.002.001.01>
</Doc:Document>
</RequestPayload>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<RequestPayload xmlns:Doc="urn:swift:xsd:xsys.002.001.01" xmlns:Sw="urn:swift:snl:ns.Sw"
xmlns:SwInt="urn:swift:snl:ns.SwInt"
xmlns:SwSec="urn:swift:snl:ns.SwSec"
xmlns:Ah="urn:swift:xsd:$ahV10">
<Ah:AppHdr>
<Ah:MsgRef>2023-07-19T04:57:05Z</Ah:MsgRef>
<Ah:CrDate>2023-07-19T04:57:05Z</Ah:CrDate>
</Ah:AppHdr>
<Doc:Document>
<Doc:xsys.002.001.01>
<Doc:AuthstnNtfctn>
<Sw:SnFRef>swi00003-2022-07-19T04:56:40.63614.012852Z</Sw:SnFRef>
<SwInt:RequestHeader>
<SwInt:Requestor>ou=xxx,o=rabcdu2s,o=swift</SwInt:Requestor>
<SwInt:Responder>ou=xxx,o=wvcxzsr,o=swift</SwInt:Responder>
<SwInt:Service>apn.hvcs!p</SwInt:Service>
<SwInt:RequestType>pacs.004.001.10</SwInt:RequestType>
<SwInt:Priority>Normal</SwInt:Priority>
<SwInt:RequestRef>201207190001100005007HV0</SwInt:RequestRef>
ptorres-prowide marked this conversation as resolved.
Show resolved Hide resolved
</SwInt:RequestHeader>
<Sw:ThirdPartyToSenderInformation>
<Sw:ESABal Ccy="AUD">999999.99</Sw:ESABal>
<Sw:RcvdDtTm>2023-07-19T14:56:42.000+10:00</Sw:RcvdDtTm>
<Sw:IntrBkSttlmDtTm>2023-07-19T14:56:46.000+10:00</Sw:IntrBkSttlmDtTm>
<Sw:IntrBkSttlmAmt Ccy="AUD">99.99</Sw:IntrBkSttlmAmt>
</Sw:ThirdPartyToSenderInformation>
<SwSec:SignatureValue>SignatureContent</SwSec:SignatureValue>
</Doc:AuthstnNtfctn>
</Doc:xsys.002.001.01>
</Doc:Document>
</RequestPayload>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<RequestPayload>
<Ah:AppHdr xmlns:Ah="urn:swift:xsd:$ahV10">
<Ah:MsgRef>2023-07-19T04:57:05Z</Ah:MsgRef>
<Ah:CrDate>2023-07-19T04:57:05Z</Ah:CrDate>
</Ah:AppHdr>
<Doc:Document xmlns:Doc="urn:swift:xsd:xsys.002.001.01" xmlns:Sw="urn:swift:snl:ns.Sw"
xmlns:SwInt="urn:swift:snl:ns.SwInt"
xmlns:SwSec="urn:swift:snl:ns.SwSec">
<Doc:xsys.002.001.01>
<Doc:AuthstnNtfctn>
<Sw:SnFRef>swi00003-2022-07-19T04:56:40.63614.012852Z</Sw:SnFRef>
<SwInt:RequestHeader>
<SwInt:Requestor>ou=xxx,o=rabcdu2s,o=swift</SwInt:Requestor>
<SwInt:Responder>ou=xxx,o=wvcxzsr,o=swift</SwInt:Responder>
<SwInt:Service>apn.hvcs!p</SwInt:Service>
<SwInt:RequestType>pacs.004.001.10</SwInt:RequestType>
<SwInt:Priority>Normal</SwInt:Priority>
<SwInt:RequestRef>201207190001100005007HV0</SwInt:RequestRef>
</SwInt:RequestHeader>
<Sw:ThirdPartyToSenderInformation>
<ESABal Ccy="AUD">999999.99</ESABal>
<RcvdDtTm>2023-07-19T14:56:42.000+10:00</RcvdDtTm>
<IntrBkSttlmDtTm>2023-07-19T14:56:46.000+10:00</IntrBkSttlmDtTm>
<IntrBkSttlmAmt Ccy="AUD">99.99</IntrBkSttlmAmt>
</Sw:ThirdPartyToSenderInformation>
<SwSec:SignatureValue>SignatureContent</SwSec:SignatureValue>
</Doc:AuthstnNtfctn>
</Doc:xsys.002.001.01>
</Doc:Document>
</RequestPayload>