/// <summary>
        /// Transform a given <see cref="ReceivedMessage"/> to a Canonical <see cref="MessagingContext"/> instance.
        /// </summary>
        /// <param name="message">Given message to transform.</param>
        /// <returns></returns>
        public async Task <MessagingContext> TransformAsync(ReceivedMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var transformer = new AS4MessageTransformer();
            MessagingContext sendContext = await transformer.TransformAsync(message);

            return(new MessagingContext(
                       sendContext.AS4Message,
                       sendContext.ReceivedMessage,
                       MessagingContextMode.Forward));
        }
        /// <summary>
        /// Transform a given <see cref="ReceivedMessage" /> to a Canonical <see cref="MessagingContext" /> instance.
        /// </summary>
        /// <param name="message">Given message to transform.</param>
        /// <returns></returns>
        public async Task <MessagingContext> TransformAsync(ReceivedMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (!(message is ReceivedEntityMessage entityMessage) ||
                !(entityMessage.Entity is MessageEntity me))
            {
                throw new InvalidDataException(
                          $"The message that must be transformed should be of type {nameof(ReceivedEntityMessage)} with a {nameof(MessageEntity)} as Entity");
            }

            MessagingContext context = await AS4MessageTransformer.TransformAsync(entityMessage);

            AS4Message as4Message = context.AS4Message;

            UserMessage toBeDeliveredUserMessage =
                as4Message.UserMessages.FirstOrDefault(u => u.MessageId == me.EbmsMessageId);

            if (toBeDeliveredUserMessage == null)
            {
                throw new InvalidOperationException(
                          $"No UserMessage {me.EbmsMessageId} can be found in stored record for delivering");
            }

            IEnumerable <Attachment> toBeUploadedAttachments =
                as4Message.Attachments
                .Where(a => a.MatchesAny(toBeDeliveredUserMessage.PayloadInfo))
                .ToArray();

            DeliverMessage deliverMessage =
                CreateDeliverMessage(
                    toBeDeliveredUserMessage,
                    toBeUploadedAttachments,
                    context.ReceivingPMode);

            Logger.Info($"(Deliver) Created DeliverMessage from (first) UserMessage {as4Message.FirstUserMessage.MessageId}");
            var envelope = new DeliverMessageEnvelope(
                message: deliverMessage,
                contentType: "application/xml",
                attachments: toBeUploadedAttachments);

            context.ModifyContext(envelope);
            return(context);
        }
Esempio n. 3
0
        /// <summary>
        /// Transform a given <see cref="ReceivedMessage"/> to a Canonical <see cref="MessagingContext"/> instance.
        /// </summary>
        /// <param name="message">Given message to transform.</param>
        /// <returns></returns>
        public async Task <MessagingContext> TransformAsync(ReceivedMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (!(message is ReceivedEntityMessage receivedMessage))
            {
                throw new NotSupportedException(
                          $"Incoming message stream from {message.Origin} that must be transformed should be of type {nameof(ReceivedEntityMessage)}");
            }

            if (receivedMessage.Entity is ExceptionEntity ex)
            {
                string ebmsMessageId = IdentifierFactory.Instance.Create();
                Error  error         = Error.FromErrorResult(
                    ebmsMessageId,
                    ex.EbmsRefToMessageId,
                    new ErrorResult(ex.Exception, ErrorAlias.Other));

                NotifyMessageEnvelope notifyEnvelope =
                    await CreateNotifyMessageEnvelopeAsync(AS4Message.Create(error), ebmsMessageId, ex.GetType());

                return(new MessagingContext(notifyEnvelope, receivedMessage));
            }

            if (receivedMessage.Entity is MessageEntity me)
            {
                var as4Transformer   = new AS4MessageTransformer();
                MessagingContext ctx = await as4Transformer.TransformAsync(receivedMessage);

                // Normally the message shouldn't have any attachments
                // but to be sure we should dispose them since we don't need attachments for notifying.
                ctx.AS4Message.CloseAttachments();

                NotifyMessageEnvelope notifyEnvelope =
                    await CreateNotifyMessageEnvelopeAsync(ctx.AS4Message, me.EbmsMessageId, me.GetType());

                ctx.ModifyContext(notifyEnvelope, receivedMessage.Entity.Id);

                return(ctx);
            }

            throw new InvalidOperationException();
        }
        /// <summary>
        /// Transform a given <see cref="ReceivedMessage"/> to a Canonical <see cref="MessagingContext"/> instance.
        /// </summary>
        /// <param name="message">Given message to transform.</param>
        /// <returns></returns>
        public async Task <MessagingContext> TransformAsync(ReceivedMessage message)
        {
            var receivedEntityMessage = message as ReceivedEntityMessage;

            if (receivedEntityMessage == null)
            {
                throw new NotSupportedException(
                          $"Minder Notify Transformer only supports transforming instances of type {typeof(ReceivedEntityMessage)}");
            }

            var as4Transformer       = new AS4MessageTransformer();
            MessagingContext context = await as4Transformer.TransformAsync(message);

            NotifyMessageEnvelope notifyMessage =
                await CreateNotifyMessageEnvelope(
                    context.AS4Message,
                    context.AS4Message.GetPrimaryMessageId(),
                    receivedEntityMessage.Entity.GetType());

            context.ModifyContext(notifyMessage);

            return(context);
        }