예제 #1
0
        /// <summary>
        /// Transform to a <see cref="MessagingContext" />
        /// with a <see cref="AS4Message" /> included
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task <MessagingContext> TransformAsync(ReceivedMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message.UnderlyingStream == null)
            {
                throw new InvalidDataException(
                          $"The incoming stream from {message.Origin} is not an ebMS Message");
            }

            if (!ContentTypeSupporter.IsContentTypeSupported(message.ContentType))
            {
                throw new InvalidDataException(
                          $"ContentType {nameof(message.ContentType)} is not supported");
            }

            VirtualStream messageStream = await CopyIncomingStreamToVirtualStream(message);

            AS4Message as4Message = await DeserializeMessage(message.ContentType, messageStream, CancellationToken.None);

            return(new MessagingContext(as4Message, message, MessagingContextMode.Unknown));
        }
예제 #2
0
        private async Task <StepResult> HandleHttpResponseAsync(HttpWebRequest request, MessagingContext ctx)
        {
            Logger.Trace($"AS4Message received from: {request.Address}");
            (HttpWebResponse webResponse, WebException exception) =
                await _httpClient.Respond(request)
                .ConfigureAwait(false);

            if (webResponse != null &&
                ContentTypeSupporter.IsContentTypeSupported(webResponse.ContentType))
            {
                using (AS4Response res = await AS4Response.Create(ctx, webResponse).ConfigureAwait(false))
                {
                    SendResult result = SendResultUtils.DetermineSendResultFromHttpResonse(res.StatusCode);
                    await UpdateRetryStatusForMessageAsync(ctx, result);

                    var handler = new PullRequestResponseHandler(
                        _createDatastore,
                        new EmptyBodyResponseHandler(
                            new TailResponseHandler()));

                    return(await handler
                           .HandleResponse(res)
                           .ConfigureAwait(false));
                }
            }

            Logger.ErrorDeep(exception);
            throw new WebException($"Failed to Send AS4Message to Url: {request.RequestUri}.", exception);
        }
        /// <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.UnderlyingStream == null)
            {
                throw new InvalidMessageException(
                          "The incoming stream is not an ebMS Message. " +
                          "Only ebMS messages conform with the AS4 Profile are supported.");
            }

            if (!ContentTypeSupporter.IsContentTypeSupported(message.ContentType))
            {
                throw new InvalidMessageException(
                          $"ContentType is not supported {message.ContentType}{Environment.NewLine}" +
                          $"Supported ContentTypes are {Constants.ContentTypes.Soap} and {Constants.ContentTypes.Mime}");
            }

            ReceivedMessage rm = await EnsureIncomingStreamIsSeekable(message);

            AS4Message as4Message = await DeserializeToAS4Message(rm);

            //Debug.Assert(m.UnderlyingStream.Position == 0, "The Deserializer failed to reposition the stream to its start-position");

            if (as4Message.IsSignalMessage && ReceivingPMode != null)
            {
                Logger.Error(
                    "Static Receive configuration doesn't allow receiving signal messages. " +
                    $"Please remove the static configured Receiving PMode: {ReceivingPMode} to also receive signal messages");

                throw new InvalidMessageException(
                          "Static Receive configuration doesn't allow receiving signal messages. ");
            }

            if (as4Message.PrimaryMessageUnit != null)
            {
                Logger.Info($"(Receive) Receiving AS4Message -> {as4Message.PrimaryMessageUnit.GetType().Name} {as4Message.PrimaryMessageUnit.MessageId}");
            }

            var context = new MessagingContext(as4Message, rm, MessagingContextMode.Receive);

            if (ReceivingPMode != null)
            {
                ReceivingProcessingMode pmode =
                    _config.GetReceivingPModes()
                    ?.FirstOrDefault(p => p.Id == ReceivingPMode);

                if (pmode != null)
                {
                    context.ReceivingPMode = pmode;
                }
                else
                {
                    Logger.Error(
                        $"ReceivingPMode with Id: {ReceivingPMode} was configured as default PMode, but this PMode cannot be found in the configured receiving PModes."
                        + $"{Environment.NewLine} Configured Receiving PModes are placed on the folder: '.\\config\\receive-pmodes\\'.");

                    var errorResult = new ErrorResult(
                        "Static configured ReceivingPMode cannot be found",
                        ErrorAlias.ProcessingModeMismatch);

                    var as4Error = new Error(
                        IdentifierFactory.Instance.Create(),
                        as4Message.GetPrimaryMessageId() ?? IdentifierFactory.Instance.Create(),
                        ErrorLine.FromErrorResult(errorResult));

                    return(new MessagingContext(
                               AS4Message.Create(as4Error),
                               MessagingContextMode.Receive)
                    {
                        ErrorResult = errorResult
                    });
                }
            }

            return(context);
        }