From cb9a76eaafd37f921006822bcfe043655288bc63 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Mon, 22 Jul 2019 13:02:19 +0200 Subject: Test Flow of DeliveryRequest from "End-To-End" & Fix Bugs Schema Changes: - Remove mzs:DeliveryRequest/TnvzMetaData because all metadata fields can be collected from DeliveryRequest and redundancy is not needed. Fixes and Refactoring in preprocess: - MzsDeliveryRequestValidator: Instead of returning false, throw an exception when a condition is not met, and explain which condition is not met / why it is not met in the exception's message. - Integrate interface change in ConfigProfileGenerator and DeliveryRequestAugmenter. - Rewrite and simplify DeliveryRequestAugmenter's augmentation. - ConfigUtil Fixes: Ensure that we do not override the wrong parameters while merging. This error appeared in tnvz / msg client, connection / receive timeout, key / trust store, and lax hostname verification / trust all. Fix Bugs in Interceptor / SoapUtils: - Problem: DOM access and information extraction was implemented somewhat sloppy. - SolutioN: Change DOM access interface to access DOM more efficiently. Add boundary checks and handle edge cases while extracting information from SOAP Messages. - Test those changes properly. Testing: - Implement Delivery Request Flow in ITEndToEndTest. - Start application on random port instead of fixed port when running integration tests. - Add assertions to tests in ITMzsServiceTest suite. Others Bug Fixes: - ServicesConfig: Ensure that mzs service and msg service run on different endpoint addresses (/msg and /mzs). - DeliveryRequestBackend: Throw exception when binary message is missing. Don't wrap the exception. - SaveResponseToFileSink: Wrap Response in JAXB Element (otherwise, marshaller does not recognize it) --- .../java/at/gv/egiz/moazs/scheme/Marshaller.java | 4 ++ .../java/at/gv/egiz/moazs/scheme/NameSpace.java | 5 ++ .../java/at/gv/egiz/moazs/scheme/SOAPUtils.java | 64 ++++++++++++++++++---- 3 files changed, 62 insertions(+), 11 deletions(-) (limited to 'src/main/java/at/gv/egiz/moazs/scheme') diff --git a/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java b/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java index 83ace5c..1a86079 100644 --- a/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java +++ b/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java @@ -32,6 +32,10 @@ public class Marshaller { } public String marshallXml(final T obj) { + if (obj == null) { + return "null"; + } + StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); jaxbMarshaller.marshal(obj, result); diff --git a/src/main/java/at/gv/egiz/moazs/scheme/NameSpace.java b/src/main/java/at/gv/egiz/moazs/scheme/NameSpace.java index 6b6f34a..386a7a6 100644 --- a/src/main/java/at/gv/egiz/moazs/scheme/NameSpace.java +++ b/src/main/java/at/gv/egiz/moazs/scheme/NameSpace.java @@ -5,6 +5,7 @@ public class NameSpace { private NameSpace() {} private static final at.gv.zustellung.msg.xsd.ObjectFactory MSG_FACTORY = new at.gv.zustellung.msg.xsd.ObjectFactory(); + private static final at.gv.zustellung.app2mzs.xsd.ObjectFactory MZS_FACTORY = new at.gv.zustellung.app2mzs.xsd.ObjectFactory(); public static final String MSG_VERSION = "2.2.0"; public static final String MSG = MSG_FACTORY.createDeliveryRequest(null).getName().getNamespaceURI(); @@ -18,4 +19,8 @@ public class NameSpace { public static final String MSG_DELIVERY_NOTIFICATION = MSG_FACTORY.createDeliveryNotification(null).getName().getLocalPart(); public static final String MSG_APP_DELIVERY_ID = MSG_FACTORY.createAppDeliveryID("").getName().getLocalPart(); + public static final String MZS_DELIVERY_REQUEST = MZS_FACTORY.createDeliveryRequest(null).getName().getLocalPart(); + public static final String MZS_TNVZCLIENT = MZS_FACTORY.createTNVZClient(null).getName().getLocalPart(); + public static final String MZS_MSGCLIENT = MZS_FACTORY.createTNVZClient(null).getName().getLocalPart(); + } diff --git a/src/main/java/at/gv/egiz/moazs/scheme/SOAPUtils.java b/src/main/java/at/gv/egiz/moazs/scheme/SOAPUtils.java index 6e96a6b..461d8fe 100644 --- a/src/main/java/at/gv/egiz/moazs/scheme/SOAPUtils.java +++ b/src/main/java/at/gv/egiz/moazs/scheme/SOAPUtils.java @@ -3,8 +3,12 @@ package at.gv.egiz.moazs.scheme; import at.gv.egiz.eaaf.core.impl.utils.DOMUtils; import at.gv.egiz.moazs.MoaZSException; import org.apache.cxf.binding.soap.Soap11; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; @@ -12,34 +16,72 @@ import javax.xml.transform.TransformerException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.LinkedList; +import java.util.List; + +import static at.gv.egiz.moazs.MoaZSException.moaZSException; @Component public class SOAPUtils { + private static final Logger log = LoggerFactory.getLogger(SOAPUtils.class); + private static final String SOAP_BODY_MISSING_ERROR_MSG = " is missing."; + private static final String SOAP_BODY_CHILDREN_MISSING_ERROR_MSG = " has no child elements."; + private static final String APP_DELIVERY_ID_MISSING_ERROR_MSG = " is missing."; + private static final String APP_DELIVERY_ID_EMPTY_ERROR_MSG = " is empty."; + private static final String MULTIPLE_MSG = "Found multiple {} elements. Will choose first element."; + private static final String APP_DELIVERY_ID_FULL_TAG_NAME = ""; + public Element toDOM(byte[] bytes) throws IOException, SAXException, ParserConfigurationException { var stream = new ByteArrayInputStream(bytes); return DOMUtils.parseXmlNonValidating(stream); } - public byte[] unwrapSoapEnvelope(Element document) { - try { - var body = document.getElementsByTagNameNS(Soap11.SOAP_NAMESPACE, "Body"); - var item = body.item(0).getFirstChild(); + public Node getChildElementOfSoapBody(Element document) throws MoaZSException { + + var bodyList = document.getElementsByTagNameNS(Soap11.SOAP_NAMESPACE, "Body"); + if (bodyList.getLength() == 0) throw moaZSException(SOAP_BODY_MISSING_ERROR_MSG); + if (bodyList.getLength() > 1) log.warn(MULTIPLE_MSG, ""); + + var body = bodyList.item(0); + var children = body.getChildNodes(); + var candidates = filterNodeByType(children, Node.ELEMENT_NODE); + + if (candidates.isEmpty()) throw moaZSException(SOAP_BODY_CHILDREN_MISSING_ERROR_MSG); + if (candidates.size() > 1) log.warn(MULTIPLE_MSG, " child"); - return DOMUtils.serializeNode(item, true) - .getBytes(StandardCharsets.UTF_8); + return candidates.get(0); + } - } catch (IOException | TransformerException e) { - throw MoaZSException.moaZSException("Error while parsing message. ", e); + private List filterNodeByType(NodeList children, short nodeType) { + var candidates = new LinkedList(); + for (int i = 0; i < children.getLength(); i++) { + var child = children.item(i); + if (child.getNodeType() == nodeType) { + candidates.add(child); + } } + + return candidates; + } + + public byte[] toBytes(Node node) throws TransformerException, IOException { + return DOMUtils.serializeNode(node, true).getBytes(StandardCharsets.UTF_8); } public String getAppDeliveryIDFrom(Element document) { - var elements = document.getElementsByTagNameNS(NameSpace.MSG, NameSpace.MSG_APP_DELIVERY_ID); + var elementList = document.getElementsByTagNameNS(NameSpace.MSG, NameSpace.MSG_APP_DELIVERY_ID); + + if (elementList.getLength() == 0) throw moaZSException(APP_DELIVERY_ID_MISSING_ERROR_MSG); + if (elementList.getLength() > 1) log.warn(MULTIPLE_MSG, APP_DELIVERY_ID_FULL_TAG_NAME); - var appDeliveryIdElement = elements.item(0).getFirstChild(); + var children = elementList.item(0).getChildNodes(); + var candidates = filterNodeByType(children, Node.TEXT_NODE); + if (candidates.isEmpty() || candidates.get(0).getNodeValue().isBlank()) + throw moaZSException(APP_DELIVERY_ID_EMPTY_ERROR_MSG); + if (candidates.size() > 1) log.warn(MULTIPLE_MSG, APP_DELIVERY_ID_FULL_TAG_NAME); - return appDeliveryIdElement.getNodeValue(); + return candidates.get(0).getNodeValue(); } } -- cgit v1.2.3