use of com.helger.phase4.ebms3header.Ebms3PartInfo in project phase4 by phax.
the class MessageHelperMethods method createEbms3PartInfo.
@Nullable
public static Ebms3PartInfo createEbms3PartInfo(@Nullable final IAS4Attachment aAttachment) {
if (aAttachment == null)
return null;
final ICommonsSet<String> aUsedPropertyNames = new CommonsHashSet<>();
final Ebms3PartProperties aEbms3PartProperties = new Ebms3PartProperties();
aEbms3PartProperties.addProperty(createEbms3Property(PART_PROPERTY_MIME_TYPE, aAttachment.getUncompressedMimeType()));
aUsedPropertyNames.add(PART_PROPERTY_MIME_TYPE);
if (aAttachment.hasCharset()) {
aEbms3PartProperties.addProperty(createEbms3Property(PART_PROPERTY_CHARACTER_SET, aAttachment.getCharset().name()));
aUsedPropertyNames.add(PART_PROPERTY_CHARACTER_SET);
}
if (aAttachment.hasCompressionMode()) {
aEbms3PartProperties.addProperty(createEbms3Property(PART_PROPERTY_COMPRESSION_TYPE, aAttachment.getCompressionMode().getMimeTypeAsString()));
aUsedPropertyNames.add(PART_PROPERTY_COMPRESSION_TYPE);
}
// Add all custom part properties (since 0.12.0)
for (final Map.Entry<String, String> aEntry : aAttachment.customPartProperties().entrySet()) if (aUsedPropertyNames.add(aEntry.getKey()))
aEbms3PartProperties.addProperty(createEbms3Property(aEntry.getKey(), aEntry.getValue()));
final Ebms3PartInfo aEbms3PartInfo = new Ebms3PartInfo();
aEbms3PartInfo.setHref(PREFIX_CID + aAttachment.getId());
aEbms3PartInfo.setPartProperties(aEbms3PartProperties);
return aEbms3PartInfo;
}
use of com.helger.phase4.ebms3header.Ebms3PartInfo in project phase4 by phax.
the class AS4IncomingHandler method _decompressAttachments.
private static void _decompressAttachments(@Nonnull final ICommonsList<WSS4JAttachment> aIncomingDecryptedAttachments, @Nonnull final Ebms3UserMessage aUserMessage, @Nonnull final IAS4MessageState aState) {
// For all incoming attachments
for (final WSS4JAttachment aIncomingAttachment : aIncomingDecryptedAttachments.getClone()) {
final EAS4CompressionMode eCompressionMode = aState.getAttachmentCompressionMode(aIncomingAttachment.getId());
if (eCompressionMode != null) {
final IHasInputStream aOldISP = aIncomingAttachment.getInputStreamProvider();
aIncomingAttachment.setSourceStreamProvider(new HasInputStream(() -> {
try {
final InputStream aSrcIS = aOldISP.getInputStream();
if (aSrcIS == null)
throw new IllegalStateException("Failed to create InputStream from " + aOldISP);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Decompressing attachment with ID '" + aIncomingAttachment.getId() + "' using " + eCompressionMode);
return eCompressionMode.getDecompressStream(aSrcIS);
} catch (final IOException ex) {
// invalid payload
throw new AS4DecompressException(ex);
}
}, aOldISP.isReadMultiple()));
// Remember the compression mode
aIncomingAttachment.setCompressionMode(eCompressionMode);
final String sAttachmentContentID = StringHelper.trimStart(aIncomingAttachment.getId(), "attachment=");
// x.getHref() != null needed since, if a message contains a payload and
// an attachment, it would throw a NullPointerException since a payload
// does not have anything written in its partinfo therefore also now
// href
final Ebms3PartInfo aPartInfo = CollectionHelper.findFirst(aUserMessage.getPayloadInfo().getPartInfo(), x -> x.getHref() != null && x.getHref().contains(sAttachmentContentID));
if (aPartInfo != null && aPartInfo.getPartProperties() != null) {
// Find MimeType property
final Ebms3Property aProperty = CollectionHelper.findFirst(aPartInfo.getPartProperties().getProperty(), x -> x.getName().equalsIgnoreCase(MessageHelperMethods.PART_PROPERTY_MIME_TYPE));
if (aProperty != null) {
final String sMimeType = aProperty.getValue();
if (MimeTypeParser.safeParseMimeType(sMimeType) == null)
LOGGER.warn("Value '" + sMimeType + "' of property '" + MessageHelperMethods.PART_PROPERTY_MIME_TYPE + "' is not a valid MIME type");
aIncomingAttachment.overwriteMimeType(sMimeType);
}
}
}
}
}
use of com.helger.phase4.ebms3header.Ebms3PartInfo in project phase4 by phax.
the class SOAPHeaderElementProcessorExtractEbms3Messaging method processHeaderElement.
@Nonnull
public ESuccess processHeaderElement(@Nonnull final Document aSOAPDoc, @Nonnull final Element aElement, @Nonnull final ICommonsList<WSS4JAttachment> aAttachments, @Nonnull final AS4MessageState aState, @Nonnull final ErrorList aErrorList) {
final IMPCManager aMPCMgr = MetaAS4Manager.getMPCMgr();
IPMode aPMode = null;
final ICommonsMap<String, EAS4CompressionMode> aCompressionAttachmentIDs = new CommonsHashMap<>();
IMPC aEffectiveMPC = null;
String sInitiatorID = null;
String sResponderID = null;
final Locale aLocale = aState.getLocale();
// Parse EBMS3 Messaging object
final CollectingValidationEventHandler aCVEH = new CollectingValidationEventHandler();
final Ebms3Messaging aMessaging = Ebms3ReaderBuilder.ebms3Messaging().setValidationEventHandler(aCVEH).read(aElement);
// wellformed
if (aMessaging == null) {
// Invalid Header == not wellformed/invalid xml
for (final IError aError : aCVEH.getErrorList()) {
LOGGER.error("Header error: " + aError.getAsString(aLocale));
aErrorList.add(SingleError.builder(aError).errorID(EEbmsError.EBMS_INVALID_HEADER.getErrorCode()).build());
}
return ESuccess.FAILURE;
}
// Remember in state
aState.setMessaging(aMessaging);
// 0 or 1 are allowed
final int nUserMessages = aMessaging.getUserMessageCount();
if (nUserMessages > 1) {
LOGGER.error("Too many UserMessage objects (" + nUserMessages + ") contained.");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
// 0 or 1 are allowed
final int nSignalMessages = aMessaging.getSignalMessageCount();
if (nSignalMessages > 1) {
LOGGER.error("Too many SignalMessage objects (" + nSignalMessages + ") contained.");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
if (nUserMessages + nSignalMessages == 0) {
// No Message was found
LOGGER.error("Neither UserMessage nor SignalMessage object contained.");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
// Check if the usermessage has a PMode in the collaboration info
final Ebms3UserMessage aUserMessage = CollectionHelper.getAtIndex(aMessaging.getUserMessage(), 0);
if (aUserMessage != null) {
final Ebms3MessageInfo aMsgInfo = aUserMessage.getMessageInfo();
if (aMsgInfo != null) {
// Set this is as early as possible, so that eventually occurring error
// messages can use the "RefToMessageId" element properly
aState.setMessageID(aMsgInfo.getMessageId());
aState.setRefToMessageID(aMsgInfo.getRefToMessageId());
aState.setMessageTimestamp(aMsgInfo.getTimestamp());
}
// PartyInfo is mandatory in UserMessage
// From is mandatory in PartyInfo
// To is mandatory in PartyInfo
final List<Ebms3PartyId> aFromPartyIdList = aUserMessage.getPartyInfo().getFrom().getPartyId();
final List<Ebms3PartyId> aToPartyIdList = aUserMessage.getPartyInfo().getTo().getPartyId();
if (aFromPartyIdList.size() > 1 || aToPartyIdList.size() > 1) {
LOGGER.error("More than one PartyId is containted in From or To Recipient please check the message.");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
// Setting Initiator and Responder id, Required values or else xsd will
// throw an error
sInitiatorID = aFromPartyIdList.get(0).getValue();
sResponderID = aToPartyIdList.get(0).getValue();
final Ebms3CollaborationInfo aCollaborationInfo = aUserMessage.getCollaborationInfo();
if (aCollaborationInfo != null) {
// Find PMode
String sPModeID = null;
String sAgreementRef = null;
if (aCollaborationInfo.getAgreementRef() != null) {
sPModeID = aCollaborationInfo.getAgreementRef().getPmode();
sAgreementRef = aCollaborationInfo.getAgreementRef().getValue();
}
// Get responder address from properties file (may be null)
final String sAddress = AS4Configuration.getThisEndpointAddress();
aPMode = m_aPModeResolver.getPModeOfID(sPModeID, aCollaborationInfo.getService().getValue(), aCollaborationInfo.getAction(), sInitiatorID, sResponderID, sAgreementRef, sAddress);
// Should be screened by the XSD conversion already
if (aPMode == null) {
LOGGER.error("Failed to resolve PMode '" + sPModeID + "' using resolver " + m_aPModeResolver);
aErrorList.add(EEbmsError.EBMS_PROCESSING_MODE_MISMATCH.getAsError(aLocale));
return ESuccess.FAILURE;
}
}
// to use the configuration for leg2
PModeLeg aPModeLeg1 = null;
PModeLeg aPModeLeg2 = null;
// Needed for the compression check: it is not allowed to have a
// compressed attachment and a SOAPBodyPayload
boolean bHasSoapBodyPayload = false;
if (aPMode != null) {
aPModeLeg1 = aPMode.getLeg1();
aPModeLeg2 = aPMode.getLeg2();
// both are present
if (aPMode.getMEPBinding().getRequiredLegs() == 2 && aPModeLeg2 == null) {
LOGGER.error("Error processing the UserMessage, PMode does not contain leg 2.");
aErrorList.add(EEbmsError.EBMS_PROCESSING_MODE_MISMATCH.getAsError(aLocale));
return ESuccess.FAILURE;
}
final boolean bUseLeg1 = _isUseLeg1(aUserMessage);
final PModeLeg aEffectiveLeg = bUseLeg1 ? aPModeLeg1 : aPModeLeg2;
final int nLegNum = bUseLeg1 ? 1 : 2;
if (aEffectiveLeg == null) {
LOGGER.error("Error processing the UserMessage, PMode does not contain effective leg " + nLegNum + ".");
aErrorList.add(EEbmsError.EBMS_PROCESSING_MODE_MISMATCH.getAsError(aLocale));
return ESuccess.FAILURE;
}
aState.setEffectivePModeLeg(nLegNum, aEffectiveLeg);
if (_checkMPCOfPMode(aEffectiveLeg, aMPCMgr, aLocale, aErrorList).isFailure())
return ESuccess.FAILURE;
bHasSoapBodyPayload = _checkSOAPBodyHasPayload(aEffectiveLeg, aSOAPDoc);
final String sEffectiveMPCID = _getMPCIDOfUserMsg(aUserMessage, aEffectiveLeg);
// PMode is valid
// Now Check if MPC valid
aEffectiveMPC = aMPCMgr.getMPCOrDefaultOfID(sEffectiveMPCID);
if (aEffectiveMPC == null) {
LOGGER.error("Error processing the UserMessage, effective MPC ID '" + sEffectiveMPCID + "' is unknown!");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
}
// Remember in state
aState.setSoapBodyPayloadPresent(bHasSoapBodyPayload);
final Ebms3PayloadInfo aEbms3PayloadInfo = aUserMessage.getPayloadInfo();
if (aEbms3PayloadInfo == null || aEbms3PayloadInfo.getPartInfo().isEmpty()) {
if (bHasSoapBodyPayload) {
LOGGER.error("No PayloadInfo/PartInfo is specified, so no SOAP body payload is allowed.");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
// attachments in the message
if (aAttachments.isNotEmpty()) {
LOGGER.error("No PayloadInfo/PartInfo is specified, so no attachments are allowed.");
aErrorList.add(EEbmsError.EBMS_EXTERNAL_PAYLOAD_ERROR.getAsError(aLocale));
return ESuccess.FAILURE;
}
} else {
// Check if there are more Attachments then specified
if (aAttachments.size() > aEbms3PayloadInfo.getPartInfoCount()) {
LOGGER.error("Error processing the UserMessage, the amount of specified attachments does not correlate with the actual attachments in the UserMessage. Expected " + aEbms3PayloadInfo.getPartInfoCount() + " but having " + aAttachments.size() + " attachments.");
aErrorList.add(EEbmsError.EBMS_EXTERNAL_PAYLOAD_ERROR.getAsError(aLocale));
return ESuccess.FAILURE;
}
int nSpecifiedAttachments = 0;
for (final Ebms3PartInfo aPartInfo : aEbms3PayloadInfo.getPartInfo()) {
// If href is null or empty there has to be a SOAP Payload
if (StringHelper.hasNoText(aPartInfo.getHref())) {
// Check if there is a BodyPayload as specified in the UserMessage
if (!bHasSoapBodyPayload) {
LOGGER.error("Error processing the UserMessage. Expected a SOAPBody Payload but there is none present.");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
} else {
// Attachment
// To check attachments which are specified in the usermessage and
// the real amount in the mime message
nSpecifiedAttachments++;
final String sAttachmentID = StringHelper.trimStart(aPartInfo.getHref(), MessageHelperMethods.PREFIX_CID);
final WSS4JAttachment aIncomingAttachment = aAttachments.findFirst(x -> EqualsHelper.equals(x.getId(), sAttachmentID));
if (aIncomingAttachment == null) {
LOGGER.warn("Failed to resolve MIME attachment '" + sAttachmentID + "' in list of " + aAttachments.getAllMapped(WSS4JAttachment::getId));
}
boolean bMimeTypePresent = false;
boolean bCompressionTypePresent = false;
if (aPartInfo.getPartProperties() != null)
for (final Ebms3Property aEbms3Property : aPartInfo.getPartProperties().getProperty()) {
final String sPropertyName = aEbms3Property.getName();
final String sPropertyValue = aEbms3Property.getValue();
if (sPropertyName.equalsIgnoreCase(MessageHelperMethods.PART_PROPERTY_MIME_TYPE)) {
bMimeTypePresent = StringHelper.hasText(sPropertyValue);
} else if (sPropertyName.equalsIgnoreCase(MessageHelperMethods.PART_PROPERTY_COMPRESSION_TYPE)) {
// Only needed check here since AS4 does not support another
// CompressionType
// http://wiki.ds.unipi.gr/display/ESENS/PR+-+AS4
final EAS4CompressionMode eCompressionMode = EAS4CompressionMode.getFromMimeTypeStringOrNull(sPropertyValue);
if (eCompressionMode == null) {
LOGGER.error("Error processing the UserMessage, CompressionType '" + sPropertyValue + "' of attachment '" + sAttachmentID + "' is not supported.");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
aCompressionAttachmentIDs.put(sAttachmentID, eCompressionMode);
bCompressionTypePresent = true;
} else if (sPropertyName.equalsIgnoreCase(MessageHelperMethods.PART_PROPERTY_CHARACTER_SET)) {
if (StringHelper.hasText(sPropertyValue)) {
final Charset aCharset = CharsetHelper.getCharsetFromNameOrNull(sPropertyValue);
if (aCharset == null) {
LOGGER.error("Value '" + sPropertyValue + "' of property '" + MessageHelperMethods.PART_PROPERTY_CHARACTER_SET + "' of attachment '" + sAttachmentID + "' is not supported");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
} else if (aIncomingAttachment != null)
aIncomingAttachment.setCharset(aCharset);
}
}
// else we don't care about the property
}
// got compressed
if (bCompressionTypePresent && !bMimeTypePresent) {
LOGGER.error("Error processing the UserMessage, MimeType for a compressed attachment ('" + sAttachmentID + "') is not present.");
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
}
}
// This may also be an indicator for "external payloads"
if (nSpecifiedAttachments != aAttachments.size()) {
LOGGER.error("Error processing the UserMessage: the amount of specified attachments does not correlate with the actual attachments in the UserMessage. Expected " + aEbms3PayloadInfo.getPartInfoCount() + " but having " + aAttachments.size() + " attachments. This is an indicator, that an external attached was provided.");
aErrorList.add(EEbmsError.EBMS_EXTERNAL_PAYLOAD_ERROR.getAsError(aLocale));
return ESuccess.FAILURE;
}
}
} else {
// Must be a SignalMessage
// all vars stay null
final Ebms3SignalMessage aSignalMessage = aMessaging.getSignalMessageAtIndex(0);
final Ebms3MessageInfo aMsgInfo = aSignalMessage.getMessageInfo();
if (aMsgInfo != null) {
// Set this is as early as possible, so that eventually occurring error
// messages can use the "RefToMessageId" element properly
aState.setMessageID(aMsgInfo.getMessageId());
aState.setRefToMessageID(aMsgInfo.getRefToMessageId());
aState.setMessageTimestamp(aMsgInfo.getTimestamp());
}
final Ebms3PullRequest aEbms3PullRequest = aSignalMessage.getPullRequest();
final Ebms3Receipt aEbms3Receipt = aSignalMessage.getReceipt();
if (aEbms3PullRequest != null) {
final String sMPC = aEbms3PullRequest.getMpc();
final IMPC aMPC = aMPCMgr.getMPCOfID(sMPC);
if (aMPC == null) {
LOGGER.error("Failed to resolve the PullRequest MPC '" + sMPC + "'");
// Return value not recognized when MPC is not currently saved
aErrorList.add(EEbmsError.EBMS_VALUE_NOT_RECOGNIZED.getAsError(aLocale));
return ESuccess.FAILURE;
}
// Create SPI which returns a PMode
for (final IAS4ServletPullRequestProcessorSPI aProcessor : AS4ServletPullRequestProcessorManager.getAllProcessors()) {
aPMode = aProcessor.findPMode(aSignalMessage);
if (aPMode != null) {
LOGGER.info("Found PMode '" + aPMode.getID() + "' for MPC '" + sMPC + "' in SignalMessage " + aSignalMessage);
break;
}
}
if (aPMode == null) {
LOGGER.error("Failed to resolve PMode for PullRequest with MPC '" + sMPC + "'");
aErrorList.add(EEbmsError.EBMS_VALUE_NOT_RECOGNIZED.getAsError(aLocale));
return ESuccess.FAILURE;
}
} else if (aEbms3Receipt != null) {
final String sRefToMessageID = aSignalMessage.getMessageInfo().getRefToMessageId();
if (StringHelper.hasNoText(sRefToMessageID)) {
LOGGER.error("The Receipt does not contain a RefToMessageId");
aErrorList.add(EEbmsError.EBMS_INVALID_RECEIPT.getAsError(aLocale));
return ESuccess.FAILURE;
}
} else {
// Error Message
if (!aSignalMessage.getError().isEmpty()) {
for (final Ebms3Error aError : aSignalMessage.getError()) {
/*
* Ebms 3 spec 6.2.6: This OPTIONAL attribute indicates the
* MessageId of the message in error, for which this error is
* raised.
*/
if (false)
if (StringHelper.hasNoText(aError.getRefToMessageInError())) {
aErrorList.add(EEbmsError.EBMS_VALUE_INCONSISTENT.getAsError(aLocale));
return ESuccess.FAILURE;
}
}
}
}
}
// Remember in state
aState.setPMode(aPMode);
aState.setOriginalSoapDocument(aSOAPDoc);
aState.setOriginalAttachments(aAttachments);
aState.setCompressedAttachmentIDs(aCompressionAttachmentIDs);
aState.setMPC(aEffectiveMPC);
aState.setInitiatorID(sInitiatorID);
aState.setResponderID(sResponderID);
return ESuccess.SUCCESS;
}
Aggregations