/// <summary>
        /// Inserts all the message units of the specified <paramref name="as4Message"/> as <see cref="OutMessage"/> records
        /// each containing the appropriate Status and Operation.
        /// User messages will be set to <see cref="Operation.ToBeProcessed"/>
        /// Signal messages that must be async returned will be set to <see cref="Operation.ToBeSent"/>.
        /// </summary>
        /// <param name="as4Message">The message for which the containing message units will be inserted.</param>
        /// <param name="sendingPMode">The processing mode that will be stored with each message unit if present.</param>
        /// <param name="receivingPMode">The processing mode that will be used to determine if the signal messages must be async returned, this pmode will be stored together with the message units.</param>
        public IEnumerable <OutMessage> InsertAS4Message(
            AS4Message as4Message,
            SendingProcessingMode sendingPMode,
            ReceivingProcessingMode receivingPMode)
        {
            if (as4Message == null)
            {
                throw new ArgumentNullException(nameof(as4Message));
            }

            if (!as4Message.MessageUnits.Any())
            {
                Logger.Trace("Incoming AS4Message hasn't got any message units to insert");
                return(Enumerable.Empty <OutMessage>());
            }

            string messageBodyLocation =
                _messageBodyStore.SaveAS4Message(
                    _configuration.OutMessageStoreLocation,
                    as4Message);

            IDictionary <string, MessageExchangePattern> relatedInMessageMeps =
                GetEbsmsMessageIdsOfRelatedSignals(as4Message);

            var results = new Collection <OutMessage>();

            foreach (MessageUnit messageUnit in as4Message.MessageUnits)
            {
                IPMode pmode =
                    SendingOrReceivingPMode(messageUnit, sendingPMode, receivingPMode);

                (OutStatus st, Operation op) =
                    DetermineReplyPattern(messageUnit, relatedInMessageMeps, receivingPMode);

                OutMessage outMessage =
                    OutMessageBuilder
                    .ForMessageUnit(messageUnit, as4Message.ContentType, pmode)
                    .BuildForSending(messageBodyLocation, st, op);

                Logger.Debug($"Insert OutMessage {outMessage.EbmsMessageType} with {{Operation={outMessage.Operation}, Status={outMessage.Status}}}");
                _repository.InsertOutMessage(outMessage);
                results.Add(outMessage);
            }

            return(results.AsEnumerable());
        }
Beispiel #2
0
        /// <summary>
        /// Updates an <see cref="AS4Message"/> for delivery and notification.
        /// </summary>
        /// <param name="as4Message">The message.</param>
        /// <param name="receivingPMode"></param>
        /// <param name="messageBodyStore">The as4 message body persister.</param>
        /// <param name="sendingPMode"></param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns></returns>
        public void UpdateAS4MessageForMessageHandling(
            AS4Message as4Message,
            SendingProcessingMode sendingPMode,
            ReceivingProcessingMode receivingPMode,
            IAS4MessageBodyStore messageBodyStore)
        {
            if (as4Message == null)
            {
                throw new ArgumentNullException(nameof(as4Message));
            }

            if (messageBodyStore == null)
            {
                throw new ArgumentNullException(nameof(messageBodyStore));
            }

            if (as4Message.HasUserMessage)
            {
                string savedLocation =
                    messageBodyStore.SaveAS4Message(_configuration.InMessageStoreLocation, as4Message);

                IEnumerable <string> userMessageIds = as4Message.UserMessages.Select(u => u.MessageId);

                _repository.UpdateInMessages(
                    m => userMessageIds.Any(id => id == m.EbmsMessageId),
                    m => m.MessageLocation = savedLocation);
            }

            if (receivingPMode?.MessageHandling?.MessageHandlingType == MessageHandlingChoiceType.Forward)
            {
                Logger.Debug($"Received AS4Message must be forwarded since the ReceivingPMode {receivingPMode?.Id} MessageHandling has a <Forward/> element");

                string pmodeString = AS4XmlSerializer.ToString(receivingPMode);
                string pmodeId     = receivingPMode.Id;

                // Only set the Operation of the InMessage that represents the
                // Primary Message-Unit to 'ToBeForwarded' since we want to prevent
                // that the same message is forwarded more than once (x number of messaging units
                // present in the AS4 Message).

                _repository.UpdateInMessages(
                    m => as4Message.MessageIds.Contains(m.EbmsMessageId),
                    m =>
                {
                    m.Intermediary = true;
                    m.SetPModeInformation(pmodeId, pmodeString);
                    Logger.Debug($"Update InMessage {m.EbmsMessageType} with {{Intermediary={m.Intermediary}, PMode={pmodeId}}}");
                });

                _repository.UpdateInMessage(
                    as4Message.GetPrimaryMessageId(),
                    m =>
                {
                    m.Operation = Operation.ToBeForwarded;
                    Logger.Debug($"Update InMessage {m.EbmsMessageType} with Operation={m.Operation}");
                });
            }
            else if (receivingPMode?.MessageHandling?.MessageHandlingType == MessageHandlingChoiceType.Deliver)
            {
                UpdateUserMessagesForDelivery(as4Message.UserMessages, receivingPMode);
                UpdateSignalMessagesForNotification(as4Message.SignalMessages, sendingPMode);
            }
            else
            {
                UpdateSignalMessagesForNotification(as4Message.SignalMessages, sendingPMode);
            }
        }