Search in sources :

Example 1 with AS4SingleSOAPHeader

use of com.helger.phase4.servlet.soap.AS4SingleSOAPHeader 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)

Aggregations

CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)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 ChildElementIterator (com.helger.xml.ChildElementIterator)1 IOException (java.io.IOException)1 Locale (java.util.Locale)1 Map (java.util.Map)1 MessagingException (javax.mail.MessagingException)1 QName (javax.xml.namespace.QName)1 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)1 Element (org.w3c.dom.Element)1