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; 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 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 candidates.get(0); } 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 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 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 candidates.get(0).getNodeValue(); } }