private static EventData ToEventData(TransportMessage transportMessage, EventStoreMessageMetadata metadata)
        {
            metadata.CorrelationId = transportMessage.CorrelationId;
            metadata.MessageId = transportMessage.Id;
            metadata.ReplyTo = transportMessage.ReplyToAddress.ToString();
            metadata.Headers = transportMessage.Headers;
            var type = transportMessage.IsControlMessage() 
                              ? "ControlMessage" 
                              : transportMessage.Headers[Headers.EnclosedMessageTypes];

            byte[] data;
            string contentType;
            if (transportMessage.Headers.TryGetValue(Headers.ContentType, out contentType))
            {
                if (contentType != ContentTypes.Json)
                {
                    throw new InvalidOperationException("Invalid content type: "+contentType);
                }
                data = transportMessage.Body;
            }
            else
            {
                data = new byte[0];
            }
            return new EventData(Guid.NewGuid(), type, true, data, metadata.ToJsonBytes());
        }
Exemplo n.º 2
0
        public IMessage Encode(TransportMessage message, ISession session)
        {
            if (message.IsControlMessage())
            {
                IMessage encoded = session.CreateBytesMessage();

                if (message.Body != null)
                {
                    encoded = session.CreateBytesMessage(message.Body);
                }

                return encoded;
            }

            return null;
        }
        /// <summary>
        /// Allow for a V3.X subscriber to subscribe/unsubscribe to a V2.6 publisher
        /// Mutate outgoing subscribe/unsubscribe messages: Create and serialize a completion message into the 
        /// body of the transport message.
        /// </summary>
        /// <param name="messages"></param>
        /// <param name="transportMessage"></param>
        public void MutateOutgoing(object[] messages, TransportMessage transportMessage)
        {
            if ((transportMessage.IsControlMessage() &&
                ((transportMessage.MessageIntent == MessageIntentEnum.Subscribe) ||
                (transportMessage.MessageIntent == MessageIntentEnum.Unsubscribe) ||
                (transportMessage.MessageIntent == MessageIntentEnum.Send))))
            {
                var stream = new MemoryStream();
                var completionMessage = new CompletionMessage();
                if (transportMessage.Headers.ContainsKey(Headers.ReturnMessageErrorCodeHeader))
                    completionMessage.ErrorCode = int.Parse(transportMessage.Headers[Headers.ReturnMessageErrorCodeHeader]);

                MessageSerializer.Serialize(new object[]  { completionMessage }, stream);
                transportMessage.Body = stream.ToArray();
                Log.Debug("Added Completion message and sending message intent: " + transportMessage.MessageIntent);
            }
        }