Search in sources :

Example 1 with ChildElementIterator

use of com.helger.xml.ChildElementIterator in project peppol-commons by phax.

the class W3CEndpointReferenceHelper method getReferenceParameters.

/**
 * Get a list of all reference parameters contained in the passed endpoint
 * reference.
 *
 * @param aEndpointReference
 *        The endpoint reference to retrieve the reference parameters. May not
 *        be <code>null</code>.
 * @return A mutable element list
 */
@Nullable
public static ICommonsList<Element> getReferenceParameters(@Nonnull final W3CEndpointReference aEndpointReference) {
    ValueEnforcer.notNull(aEndpointReference, "EndpointReference");
    final Element eRefParams = XMLHelper.getFirstChildElementOfName(_convertReferenceToXML(aEndpointReference), "ReferenceParameters");
    if (eRefParams == null)
        return null;
    // All all child elements of ReferenceParameters :)
    return new CommonsArrayList<>(new ChildElementIterator(eRefParams));
}
Also used : ChildElementIterator(com.helger.xml.ChildElementIterator) Element(org.w3c.dom.Element) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nullable(javax.annotation.Nullable)

Example 2 with ChildElementIterator

use of com.helger.xml.ChildElementIterator in project phase4 by phax.

the class AS4IncomingHandler method _processSoapHeaderElements.

private static void _processSoapHeaderElements(@Nonnull final SOAPHeaderElementProcessorRegistry aRegistry, @Nonnull final Document aSoapDocument, @Nonnull final ICommonsList<WSS4JAttachment> aIncomingAttachments, @Nonnull final AS4MessageState aState, @Nonnull final ICommonsList<Ebms3Error> aErrorMessages) throws Phase4Exception {
    final ESoapVersion eSoapVersion = aState.getSoapVersion();
    final ICommonsList<AS4SingleSOAPHeader> aHeaders = new CommonsArrayList<>();
    {
        // Find SOAP header
        final Node aHeaderNode = XMLHelper.getFirstChildElementOfName(aSoapDocument.getDocumentElement(), eSoapVersion.getNamespaceURI(), eSoapVersion.getHeaderElementName());
        if (aHeaderNode == null)
            throw new Phase4Exception("SOAP document is missing a Header element {" + eSoapVersion.getNamespaceURI() + "}" + eSoapVersion.getHeaderElementName());
        // Extract all header elements including their "mustUnderstand" value
        for (final Element aHeaderChild : new ChildElementIterator(aHeaderNode)) {
            final QName aQName = XMLHelper.getQName(aHeaderChild);
            final String sMustUnderstand = aHeaderChild.getAttributeNS(eSoapVersion.getNamespaceURI(), "mustUnderstand");
            final boolean bIsMustUnderstand = eSoapVersion.getMustUnderstandValue(true).equals(sMustUnderstand);
            aHeaders.add(new AS4SingleSOAPHeader(aHeaderChild, aQName, bIsMustUnderstand));
        }
    }
    final ICommonsOrderedMap<QName, ISOAPHeaderElementProcessor> aAllProcessors = aRegistry.getAllElementProcessors();
    if (aAllProcessors.isEmpty())
        LOGGER.error("No SOAP Header element processor is registered");
    // handle all headers in the order of the registered handlers!
    for (final Map.Entry<QName, ISOAPHeaderElementProcessor> aEntry : aAllProcessors.entrySet()) {
        final QName aQName = aEntry.getKey();
        // Check if this message contains a header for the current handler
        final AS4SingleSOAPHeader aHeader = aHeaders.findFirst(x -> aQName.equals(x.getQName()));
        if (aHeader == null) {
            // no header element for current processor
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Message contains no SOAP header element with QName " + aQName.toString());
            continue;
        }
        final ISOAPHeaderElementProcessor aProcessor = aEntry.getValue();
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Processing SOAP header element " + aQName.toString() + " with processor " + aProcessor);
        // Error list for this processor
        final ErrorList aErrorList = new ErrorList();
        try {
            // Process element
            if (aProcessor.processHeaderElement(aSoapDocument, aHeader.getNode(), aIncomingAttachments, aState, aErrorList).isSuccess()) {
                // Mark header as processed (for mustUnderstand check)
                aHeader.setProcessed(true);
            } else {
                // upon failure, the element stays unprocessed and sends back a signal
                // message with the errors
                LOGGER.error("Failed to process SOAP header element " + aQName.toString() + " with processor " + aProcessor + "; error details: " + aErrorList);
                final String sRefToMessageID = aState.getMessageID();
                final Locale aLocale = aState.getLocale();
                for (final IError aError : aErrorList) {
                    final EEbmsError ePredefinedError = EEbmsError.getFromErrorCodeOrNull(aError.getErrorID());
                    if (ePredefinedError != null)
                        aErrorMessages.add(ePredefinedError.getAsEbms3Error(aLocale, sRefToMessageID));
                    else {
                        final Ebms3Error aEbms3Error = new Ebms3Error();
                        aEbms3Error.setErrorDetail(aError.getErrorText(aLocale));
                        aEbms3Error.setErrorCode(aError.getErrorID());
                        aEbms3Error.setSeverity(aError.getErrorLevel().getID());
                        aEbms3Error.setOrigin(aError.getErrorFieldName());
                        aEbms3Error.setRefToMessageInError(sRefToMessageID);
                        aErrorMessages.add(aEbms3Error);
                    }
                }
                // Stop processing of other headers
                break;
            }
        } catch (final Exception ex) {
            // upon failure, the element stays unprocessed and sends back a signal
            // message with the errors
            LOGGER.error("Error processing SOAP header element " + aQName.toString() + " with processor " + aProcessor, ex);
            aErrorMessages.add(EEbmsError.EBMS_OTHER.getAsEbms3Error(aState.getLocale(), aState.getMessageID(), "Error processing SOAP header element " + aQName.toString()));
            // Stop processing of other headers
            break;
        }
    }
    // If an error message is present, send it back gracefully
    if (aErrorMessages.isEmpty()) {
        // Are all must-understand headers processed?
        for (final AS4SingleSOAPHeader aHeader : aHeaders) if (aHeader.isMustUnderstand() && !aHeader.isProcessed())
            throw new Phase4Exception("Required SOAP header element " + aHeader.getQName().toString() + " could not be handled");
    }
}
Also used : Locale(java.util.Locale) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Ebms3Error(com.helger.phase4.ebms3header.Ebms3Error) IError(com.helger.commons.error.IError) AS4DecompressException(com.helger.phase4.attachment.AS4DecompressException) MessagingException(javax.mail.MessagingException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) Phase4Exception(com.helger.phase4.util.Phase4Exception) IOException(java.io.IOException) AS4SingleSOAPHeader(com.helger.phase4.servlet.soap.AS4SingleSOAPHeader) Phase4Exception(com.helger.phase4.util.Phase4Exception) ErrorList(com.helger.commons.error.list.ErrorList) ESoapVersion(com.helger.phase4.soap.ESoapVersion) ChildElementIterator(com.helger.xml.ChildElementIterator) EEbmsError(com.helger.phase4.error.EEbmsError) HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) Map(java.util.Map) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ISOAPHeaderElementProcessor(com.helger.phase4.servlet.soap.ISOAPHeaderElementProcessor)

Example 3 with ChildElementIterator

use of com.helger.xml.ChildElementIterator in project phase4 by phax.

the class AS4ReceiptMessage method _getAllReferences.

@Nonnull
@ReturnsMutableCopy
private static ICommonsList<Node> _getAllReferences(@Nullable final Node aUserMessage) {
    final ICommonsList<Node> aDSRefs = new CommonsArrayList<>();
    Node aNext = XMLHelper.getFirstChildElementOfName(aUserMessage, "Envelope");
    if (aNext != null) {
        aNext = XMLHelper.getFirstChildElementOfName(aNext, "Header");
        if (aNext != null) {
            aNext = XMLHelper.getFirstChildElementOfName(aNext, CAS4.WSSE_NS, "Security");
            if (aNext != null) {
                aNext = XMLHelper.getFirstChildElementOfName(aNext, CAS4.DS_NS, "Signature");
                if (aNext != null) {
                    aNext = XMLHelper.getFirstChildElementOfName(aNext, CAS4.DS_NS, "SignedInfo");
                    if (aNext != null) {
                        new ChildElementIterator(aNext).findAll(XMLHelper.filterElementWithNamespaceAndLocalName(CAS4.DS_NS, "Reference"), aDSRefs::add);
                    }
                }
            }
        }
    }
    return aDSRefs;
}
Also used : ChildElementIterator(com.helger.xml.ChildElementIterator) Node(org.w3c.dom.Node) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Aggregations

CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 ChildElementIterator (com.helger.xml.ChildElementIterator)3 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 ICommonsOrderedMap (com.helger.commons.collection.impl.ICommonsOrderedMap)1 IError (com.helger.commons.error.IError)1 ErrorList (com.helger.commons.error.list.ErrorList)1 HttpHeaderMap (com.helger.commons.http.HttpHeaderMap)1 AS4DecompressException (com.helger.phase4.attachment.AS4DecompressException)1 Ebms3Error (com.helger.phase4.ebms3header.Ebms3Error)1 EEbmsError (com.helger.phase4.error.EEbmsError)1 AS4SingleSOAPHeader (com.helger.phase4.servlet.soap.AS4SingleSOAPHeader)1 ISOAPHeaderElementProcessor (com.helger.phase4.servlet.soap.ISOAPHeaderElementProcessor)1 ESoapVersion (com.helger.phase4.soap.ESoapVersion)1 Phase4Exception (com.helger.phase4.util.Phase4Exception)1 IOException (java.io.IOException)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1