/******************************************************************************* * Copyright 2020 Graz University of Technology * MOA ZS has been developed in a cooperation between EGIZ * and Graz University of Technology. * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * https://joinup.ec.europa.eu/news/understanding-eupl-v12 * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. *******************************************************************************/ 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.Soap12; 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; /** * @author Christof Rabensteiner * */ @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_FULL_TAG_NAME = ""; private static final String MULTIPLE_MSG = "Found multiple {} elements. Will choose first element."; private static final String APP_DELIVERY_ID_MISSING_ERROR_MSG = APP_DELIVERY_ID_FULL_TAG_NAME + " is missing."; private static final String APP_DELIVERY_ID_EMPTY_ERROR_MSG = APP_DELIVERY_ID_FULL_TAG_NAME + " is empty."; 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(Soap12.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(); } }