use of com.helger.phase4.error.EEbmsError 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");
}
}
Aggregations