diff options
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/scheme')
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java | 4 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/scheme/NameSpace.java | 5 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/scheme/SOAPUtils.java | 64 |
3 files changed, 62 insertions, 11 deletions
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 <T> 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 = "<soap11:Body> is missing."; + private static final String SOAP_BODY_CHILDREN_MISSING_ERROR_MSG = "<soap11:Body> has no child elements."; + private static final String APP_DELIVERY_ID_MISSING_ERROR_MSG = "<msg:AppDeliveryID> is missing."; + private static final String APP_DELIVERY_ID_EMPTY_ERROR_MSG = "<msg:AppDeliveryID> 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 = "<msg:" + NameSpace.MSG_APP_DELIVERY_ID + ">"; + 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, "<soap11:Body>"); + + 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, "<soap11:Body> 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<Node> filterNodeByType(NodeList children, short nodeType) { + var candidates = new LinkedList<Node>(); + 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(); } } |