public void ThenStreamGetsSetToZero_IfStreamIsNonClosableStream() { // Arrange using (var stubStream = new NonCloseableStream(GetNotZeroPositionStream())) { // Act StreamUtilities.MovePositionToStreamStart(stubStream); // Assert AssertEqualsZero(stubStream.InnerStream); } }
public void ThenStreamGetsSetToZero_IfStreamIsFilteredStream() { // Arrange using (var stubStream = new FilteredStream(GetNotZeroPositionStream())) { // Act StreamUtilities.MovePositionToStreamStart(stubStream); // Assert AssertEqualsZero(stubStream.Source); } }
public void ThenStreamGetsSetToZero_IfStreamIsSeekable() { // Arrange using (MemoryStream stubStream = GetNotZeroPositionStream()) { // Act StreamUtilities.MovePositionToStreamStart(stubStream); // Assert AssertEqualsZero(stubStream); } }
/// <summary> /// Resets the reference stream position to 0. /// </summary> /// <param name="reference"></param> protected static void ResetReferenceStreamPosition(Reference reference) { if (RefTargetField == null) { return; } if (RefTargetField.GetValue(reference) is Stream referenceStream) { StreamUtilities.MovePositionToStreamStart(referenceStream); } }
/// <summary> /// Inserts a received Message in the DataStore. /// For each message-unit that exists in the AS4Message,an InMessage record is created. /// The AS4 Message Body is persisted as it has been received. /// </summary> /// <remarks>The received Message is parsed to an AS4 Message instance.</remarks> /// <param name="sendingPMode"></param> /// <param name="mep"></param> /// <param name="messageBodyStore"></param> /// <param name="as4Message"></param> /// <param name="originalMessage"></param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="InvalidOperationException"></exception> /// <returns>A MessagingContext instance that contains the parsed AS4 Message.</returns> public async Task <AS4Message> InsertAS4MessageAsync( AS4Message as4Message, ReceivedMessage originalMessage, SendingProcessingMode sendingPMode, MessageExchangePattern mep, IAS4MessageBodyStore messageBodyStore) { if (as4Message == null) { throw new ArgumentNullException(nameof(as4Message)); } if (originalMessage == null) { throw new InvalidOperationException("The MessagingContext must contain a ReceivedMessage"); } if (messageBodyStore == null) { throw new ArgumentNullException(nameof(messageBodyStore)); } // TODO: should we start the transaction here. string location = await messageBodyStore.SaveAS4MessageStreamAsync( location : _configuration.InMessageStoreLocation, as4MessageStream : originalMessage.UnderlyingStream).ConfigureAwait(false); StreamUtilities.MovePositionToStreamStart(originalMessage.UnderlyingStream); try { InsertUserMessages(as4Message, mep, location, sendingPMode); InsertSignalMessages(as4Message, mep, location, sendingPMode); return(as4Message); } catch (Exception ex) { Logger.Error(ex.Message); var service = new ExceptionService(_configuration, _repository, messageBodyStore); await service.InsertIncomingExceptionAsync(ex, new MemoryStream(Encoding.UTF8.GetBytes(location))); throw; } }
/// <summary> /// Asynchronously deserializes the given <paramref name="input"/> stream to an <see cref="AS4Message"/> model. /// </summary> /// <param name="input">The source stream from where the message should be read.</param> /// <param name="contentType">The content type required to correctly deserialize the message into different MIME parts.</param> /// <param name="cancellation">The token to control the cancellation of the deserialization.</param> public async Task <AS4Message> DeserializeAsync( Stream input, string contentType, CancellationToken cancellation = default(CancellationToken)) { if (input == null) { throw new ArgumentNullException(nameof(input)); } if (contentType == null) { throw new ArgumentNullException(nameof(contentType)); } var envelopeDocument = new XmlDocument { PreserveWhitespace = true }; envelopeDocument.Load(input); // Sometimes throws 'The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not declared.' // ValidateEnvelopeDocument(envelopeDocument); XmlNamespaceManager nsMgr = GetNamespaceManagerForDocument(envelopeDocument); SecurityHeader securityHeader = DeserializeSecurityHeader(envelopeDocument, nsMgr); Messaging messagingHeader = DeserializeMessagingHeader(envelopeDocument, nsMgr); Body1 body = DeserializeBody(envelopeDocument, nsMgr); if (messagingHeader == null) { throw new InvalidMessageException("The envelopeStream does not contain a Messaging element"); } AS4Message as4Message = await AS4Message.CreateAsync( envelopeDocument, contentType, securityHeader, messagingHeader, body); StreamUtilities.MovePositionToStreamStart(input); return(as4Message); }
/// <summary> /// Creates a new result from a stream. /// </summary> /// <param name="status"></param> /// <param name="content"></param> /// <param name="contentType"></param> /// <returns></returns> public static HttpResult FromStream(HttpStatusCode status, Stream content, string contentType) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (contentType == null) { throw new ArgumentNullException(nameof(contentType)); } return(new HttpResult( status, contentType, async response => { StreamUtilities.MovePositionToStreamStart(content); await content.CopyToFastAsync(response.OutputStream); })); }
public void FailsToMovePositionOnNullStream() { // Act / Assert Assert.Throws <ArgumentNullException>( () => StreamUtilities.MovePositionToStreamStart(stream: null)); }