public bool Process(Envelope envelope)
        {
            bool processMessage = true;

            // Rethrow any possible exception, message will be retried
            try
            {
                /*
                 * https://www.rabbitmq.com/reliability.html
                 * "...if the redelivered flag is not set then it is guaranteed that the message has not been seen before..."
                 */
                if (envelope.Headers.ContainsKey("Redelivered") && Convert.ToBoolean(envelope.Headers["Redelivered"].ToString()))
                {
                    // if exists in persistant storage
                    bool msgAlreadyProcessed =
                        _messageDeduplicationPersistor.GetMessageExists(
                            new Guid(Encoding.UTF8.GetString((byte[]) (envelope.Headers["MessageId"]))));
                    if (msgAlreadyProcessed)
                    {
                        processMessage = false;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Fatal("Error checking for duplicate messages.", ex);
                throw;
            }

            return processMessage;
        }
 public bool Process(Envelope envelope)
 {
     var json = Encoding.UTF8.GetString(envelope.Body);
     var message = JsonConvert.DeserializeObject<FakeMessage1>(json);
     message.Username = "******";
     envelope.Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
     _beforeFilter1Ran = true;
     return true;
 }
 public bool Process(Envelope envelope)
 {
     Console.WriteLine("Inside before filter 1");
     var json = Encoding.UTF8.GetString(envelope.Body);
     var message = JsonConvert.DeserializeObject<FilterMessage>(json);
     message.FilterModifiedValue = "modified by consumer";
     envelope.Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
     return true;
 }
 public bool Process(Envelope envelope)
 {
     if (envelope.Headers.ContainsKey("Authenticated") && bool.Parse(System.Text.Encoding.ASCII.GetString((byte[])envelope.Headers["Authenticated"])))
     {
         Console.WriteLine("authenticated");
         return true;
     }
     Console.WriteLine("not authenticated");
     return false;
 }
        public void ShouldProcessNewMessageWhenRedeliveredFlagIsNotSet()
        {
            // Arrange
            var incomingFilter = new IncomingFilter(_persistor.Object);
            var envelope = new Envelope();
            envelope.Headers = new Dictionary<string, object>();

            // Act
            var result = incomingFilter.Process(envelope);

            // Assert
            Assert.True(result);
        }
        public bool Process(Envelope envelope)
        {
            using (var compressedMessageMemoryStream = new MemoryStream(envelope.Body))
            using (var messageMemoryStream = new MemoryStream())
            {
                using (var gzipStream = new GZipStream(compressedMessageMemoryStream, CompressionMode.Decompress))
                {
                    MemoryStreamUtilities.CopyTo(gzipStream, messageMemoryStream);
                }

                envelope.Body = messageMemoryStream.ToArray();
            }
            return true;
        }
        public void ShouldProcessNewMessageWhenRedeliveredFlagIsSet()
        {
            // Arrange
            var incomingFilter = new IncomingFilter(_persistor.Object);
            Guid messageId = Guid.NewGuid();
            var envelope = new Envelope();
            envelope.Headers = new Dictionary<string, object> { { "Redelivered", true }, { "MessageId",  Encoding.ASCII.GetBytes(messageId.ToString()) } };

            // Act
            var result = incomingFilter.Process(envelope);

            // Assert
            Assert.True(result);
        }
        public bool Process(Envelope envelope)
        {
            var json = Encoding.UTF8.GetString(envelope.Body);
            var message = JsonConvert.DeserializeObject<FilterMessage>(json);

            Console.WriteLine("Inside outgoing filter 2");

            if (message.ProducerFilterFail)
            {
                return false;
            }

            return true;
        }
        public void ShouldNotProcessDuplicateMessageWhenRedeliveredFlagIsSet()
        {
            // Arrange
            Guid messageId = Guid.NewGuid();
            _persistor.Setup(i => i.GetMessageExists(messageId)).Returns(true);
            var incomingFilter = new IncomingFilter(_persistor.Object);
            var envelope = new Envelope();
            envelope.Headers = new Dictionary<string, object> { { "Redelivered", true }, { "MessageId", Encoding.ASCII.GetBytes(messageId.ToString()) } };

            // Act
            var result = incomingFilter.Process(envelope);

            // Assert
            Assert.False(result);
        }
        public bool Process(Envelope envelope)
        {
            try
            {
                _messageDeduplicationPersistor.Insert(
                    new Guid(Encoding.UTF8.GetString((byte[]) envelope.Headers["MessageId"])),
                    DateTime.UtcNow.AddHours(_settings.MsgExpiryHours));
            }
            catch (Exception ex)
            {
                Logger.Error("Error processing outgoing deduplication filter ", ex);
            }

            return true;
        }
        public void ShouldSwallowPersistanceException()
        {
            // Arrange
            Guid messageId = Guid.NewGuid();

            var deduplicationSettings = DeduplicationFilterSettings.Instance;
            deduplicationSettings.DisableMsgExpiry = true;

            _persistor.Setup(i => i.Insert(messageId, It.IsAny<DateTime>())).Throws(new Exception());

            var outgoingFilter = new OutgoingFilter(_persistor.Object);
            var envelope = new Envelope();
            envelope.Headers = new Dictionary<string, object>();
            envelope.Headers = new Dictionary<string, object> { { "MessageId", Encoding.ASCII.GetBytes(messageId.ToString()) } };

            // Act
            var result = outgoingFilter.Process(envelope);

            // Assert
            Assert.True(result);
        }
            public bool Process(Envelope envelope)
            {
                if (Bus == null)
                {
                    throw new Exception("Bus is null");
                }

                return false;
            }
 public bool Process(Envelope envelope)
 {
     _beforeFilter2Ran = true;
     return true;
 }
        public void SendBytes(string endPoint, byte[] packet, Dictionary<string, string> headers)
        {
            lock (_lock)
            {
                IBasicProperties basicProperties = _model.CreateBasicProperties();
                basicProperties.SetPersistent(true);

                var messageHeaders = GetHeaders(typeof(byte[]), headers, endPoint, "ByteStream");

                var envelope = new Envelope
                {
                    Body = packet,
                    Headers = messageHeaders
                };

                basicProperties.Headers = envelope.Headers;
                basicProperties.MessageId = basicProperties.Headers["MessageId"].ToString(); // keep track of retries

                Retry.Do(() => _model.BasicPublish(string.Empty, endPoint, basicProperties, envelope.Body),
                         ex => RetryConnection(),
                         new TimeSpan(0, 0, 0, 6), 10);
            }
        }
        private void DoPublish(Type type, byte[] message, Dictionary<string, string> headers)
        {
            lock (_lock)
            {
                IBasicProperties basicProperties = _model.CreateBasicProperties();

                var messageHeaders = GetHeaders(type, headers, _transportSettings.QueueName, "Publish");

                var envelope = new Envelope
                {
                    Body = message,
                    Headers = messageHeaders
                };

                basicProperties.Headers = envelope.Headers;
                basicProperties.MessageId = basicProperties.Headers["MessageId"].ToString(); // keep track of retries

                basicProperties.SetPersistent(true);
                var exchangeName = ConfigureExchange(type.FullName.Replace(".", string.Empty));

                Retry.Do(() => _model.BasicPublish(exchangeName, _transportSettings.QueueName, basicProperties, envelope.Body),
                    ex => RetryConnection(),
                    new TimeSpan(0, 0, 0, 6), 10);
            }
        }
        public bool Process(Envelope envelope)
        {
            var incomingFilter = new IncomingFilter(new MessageDeduplicationPersistorMongoDbSsl());

            return incomingFilter.Process(envelope);
        }
        public void ShouldRethrowAnyInternalException()
        {
            // Arrange
            Guid messageId = Guid.NewGuid();
            _persistor.Setup(i => i.GetMessageExists(messageId)).Throws(new Exception());
            var incomingFilter = new IncomingFilter(_persistor.Object);
            var envelope = new Envelope();
            envelope.Headers = new Dictionary<string, object> { { "Redelivered", true }, { "MessageId", Encoding.ASCII.GetBytes(messageId.ToString()) } };

            // Act / Assert
            Assert.Throws<Exception>(() => incomingFilter.Process(envelope));
        }
 public bool Process(Envelope envelope)
 {
     _afterFilter2Ran = true;
     return true;
 }
        public bool Process(Envelope envelope)
        {
            var outgoingFilter = new OutgoingFilter(new MessageDeduplicationPersistorMongoDb());

            return outgoingFilter.Process(envelope);
        }
            public bool Process(Envelope envelope)
            {
                if (Bus == null)
                {
                    throw new Exception("Bus is null");
                }

                _beforeFilter2Ran = true;
                return true;
            }
 public bool Process(Envelope envelope)
 {
     Console.WriteLine("Filter1");
     return true;
 }
 public bool Process(Envelope envelope)
 {
     Console.WriteLine("Inside after filter 2");
     return true;
 }
 public bool Process(Envelope envelope)
 {
     return false;
 }