/// <summary>Converts from BasicProperties to MessageProperties.</summary>
        /// <param name="source">The source.</param>
        /// <param name="envelope">The envelope.</param>
        /// <param name="charset">The charset.</param>
        /// <returns>The message properties.</returns>
        public MessageProperties ToMessageProperties(IBasicProperties source, BasicGetResult envelope, string charset)
        {
            var target = new MessageProperties();
            var headers = source.Headers;
            if (headers != null && headers.Count > 0)
            {
                foreach (DictionaryEntry entry in headers)
                {
                    var value = entry.Value;

                    /*
                    if (value is LongString) 
                    {
                        value = this.convertLongString((LongString) value, charset);
                    }
                    */
                    target.Headers[(string)entry.Key] = value;
                }
            }

            target.Timestamp = source.Timestamp.ToDateTime();
            target.MessageId = source.MessageId;
            target.UserId = source.UserId;
            target.AppId = source.AppId;
            target.ClusterId = source.ClusterId;
            target.Type = source.Type;
            target.DeliveryMode = (MessageDeliveryMode)source.DeliveryMode;
            target.Expiration = source.Expiration;
            target.Priority = source.Priority;
            target.ContentType = source.ContentType;
            target.ContentEncoding = source.ContentEncoding;
            var correlationId = source.CorrelationId;
            if (correlationId != null)
            {
                try
                {
                    target.CorrelationId = source.CorrelationId.ToByteArrayWithEncoding(charset);
                }
                catch (Exception ex)
                {
                    throw new AmqpUnsupportedEncodingException(ex);
                }
            }

            var replyTo = source.ReplyTo;
            if (replyTo != null)
            {
                target.ReplyTo = replyTo;
            }

            if (envelope != null)
            {
                target.ReceivedExchange = envelope.Exchange;
                target.ReceivedRoutingKey = envelope.RoutingKey;
                target.Redelivered = envelope.Redelivered;
                target.DeliveryTag = (long)envelope.DeliveryTag;
            }

            return target;
        }
 public void ToStringForSerializableMessageBody()
 {
     var messageProperties = new MessageProperties();
     messageProperties.ContentType = MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT;
     var message = new Message(SerializationUtils.SerializeObject(DateTime.Now), messageProperties);
     Assert.NotNull(message.ToString());
 }
 public void ToStringForNonSerializableMessageBody()
 {
     var messageProperties = new MessageProperties();
     messageProperties.ContentType = MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT;
     var message = new Message(Encoding.UTF8.GetBytes("foo"), messageProperties);
     // System.err.println(message);
     Assert.NotNull(message.ToString());
 }
        /// <summary>
        /// Create a message from the object with properties.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <param name="messageProperties">
        /// The message properties.
        /// </param>
        /// <returns>
        /// The Message.
        /// </returns>
        public virtual Message ToMessage(object obj, MessageProperties messageProperties)
        {
            if (messageProperties == null)
            {
                messageProperties = new MessageProperties();
            }

            Message message = this.CreateMessage(obj, messageProperties);
            messageProperties = message.MessageProperties;
            if (this.createMessageIds && messageProperties.MessageId == null)
            {
                messageProperties.MessageId = Guid.NewGuid().ToString();
            }

            return message;
        }
        /// <summary>
        /// Creates an AMQP Message from the provided Object.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <param name="messageProperties">
        /// The message properties.
        /// </param>
        /// <returns>
        /// The message.
        /// </returns>
        /// <exception cref="MessageConversionException">
        /// </exception>
        protected override Message CreateMessage(object obj, MessageProperties messageProperties)
        {
            byte[] bytes = null;

            if (obj is byte[])
            {
                bytes = (byte[])obj;
                messageProperties.ContentType = MessageProperties.CONTENT_TYPE_BYTES;
            }
            else if (obj is string)
            {
                try
                {
                    bytes = SerializationUtils.SerializeString((string)obj, this.defaultCharset);
                }
                catch (Exception e)
                {
                    throw new MessageConversionException("failed to convert to Message content", e);
                }
                messageProperties.ContentType = MessageProperties.CONTENT_TYPE_TEXT_PLAIN;
                messageProperties.ContentEncoding = this.defaultCharset;
            }
            else if ((obj is ISerializable) || (obj != null && obj.GetType().IsSerializable))
            {
                try
                {
                    bytes = SerializationUtils.SerializeObject((ISerializable)obj);
                }
                catch (Exception e)
                {
                    throw new MessageConversionException("failed to convert to serialized Message content", e);
                }
                messageProperties.ContentType = MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT;
            }

            if (bytes != null)
            {
                messageProperties.ContentLength = bytes.Length;
            }

            return new Message(bytes, messageProperties);
        }
        /// <summary>
        /// Convert from string to Type.
        /// </summary>
        /// <param name="properties">
        /// The message properties.
        /// </param>
        /// <returns>
        /// The type.
        /// </returns>
        public Type ToType(MessageProperties properties)
        {
            var headers = properties.Headers;
            var typeIdFieldNameValue = headers[this.TypeIdFieldName];

            string typeId = null;
            if (typeIdFieldNameValue != null)
            {
                if (typeIdFieldNameValue is byte[])
                {
                    typeId = Encoding.UTF8.GetString((byte[])typeIdFieldNameValue);
                }
                else
                {
                    typeId = typeIdFieldNameValue.ToString();
                }
            }

            if (typeId == null)
            {
                throw new MessageConversionException("failed to convert Message content. Could not resolve " + this.TypeIdFieldName + " in header");
            }

            return this.ToType(typeId);
        }
 /// <summary>
 /// Add type mapping to headers.
 /// </summary>
 /// <param name="typeOfObjectToConvert">
 /// The type of object to convert.
 /// </param>
 /// <param name="properties">
 /// The properties.
 /// </param>
 public void FromType(Type typeOfObjectToConvert, MessageProperties properties)
 {
     properties.Headers.Add(this.TypeIdFieldName, this.FromType(typeOfObjectToConvert));
 }
Exemple #8
0
 /// <summary>Initializes a new instance of the <see cref="Message"/> class. </summary>
 /// <param name="body">The body.</param>
 /// <param name="messageProperties">The message Properties.</param>
 public Message(byte[] body, MessageProperties messageProperties)
 {
     this.body = body;
     this.messageProperties = messageProperties;
 }
 public void TestReplyToNullByDefault()
 {
     var properties = new MessageProperties();
     Assert.AreEqual(null, properties.ReplyTo);
     Assert.AreEqual(null, properties.ReplyToAddress);
 }
        /// <summary>Converts from message properties to basic properties.</summary>
        /// <param name="channel">The channel.</param>
        /// <param name="source">The source.</param>
        /// <param name="charset">The charset.</param>
        /// <returns>The basic properties.</returns>
        public IBasicProperties FromMessageProperties(IModel channel, MessageProperties source, string charset)
        {
            var target = channel.CreateBasicProperties();

            // target.Headers = this.ConvertHeadersIfNecessary(source.Headers == null ? target.Headers : source.Headers);
            target.Headers = this.ConvertHeadersIfNecessary(source.Headers);
            target.Timestamp = source.Timestamp.ToAmqpTimestamp();

            if (source.MessageId != null)
            {
                target.MessageId = source.MessageId;
            }

            if (source.UserId != null)
            {
                target.UserId = source.UserId;
            }

            if (source.AppId != null)
            {
                target.AppId = source.AppId;
            }

            if (source.ClusterId != null)
            {
                target.ClusterId = source.ClusterId;
            }

            if (source.Type != null)
            {
                target.Type = source.Type;
            }

            target.DeliveryMode = (byte)((int)source.DeliveryMode);

            if (source.Expiration != null)
            {
                target.Expiration = source.Expiration;
            }

            target.Priority = (byte)source.Priority;

            if (source.ContentType != null)
            {
                target.ContentType = source.ContentType;
            }

            if (source.ContentEncoding != null)
            {
                target.ContentEncoding = source.ContentEncoding;
            }

            if (source.CorrelationId != null && source.CorrelationId.Length > 0)
            {
                try
                {
                    target.CorrelationId = source.CorrelationId.ToStringWithEncoding(charset);
                }
                catch (Exception ex)
                {
                    throw new AmqpUnsupportedEncodingException(ex);
                }
            }

            if (source.ReplyTo != null)
            {
                target.ReplyTo = source.ReplyTo;
            }

            return target;
        }
        /// <summary>
        /// Overridden implementation of CreateMessage, to cater for Json serialization.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <param name="messageProperties">
        /// The message properties.
        /// </param>
        /// <returns>
        /// The Message.
        /// </returns>
        /// <exception cref="MessageConversionException">
        /// </exception>
        protected override Message CreateMessage(object obj, MessageProperties messageProperties)
        {
            byte[] bytes = null;
            try
            {
                if (messageProperties == null)
                {
                    messageProperties = new MessageProperties();
                }

                bytes = SerializationUtils.SerializeJson(obj, this.defaultCharset);
            }
            catch (Exception e)
            {
                throw new MessageConversionException("Failed to convert Message content", e);
            }

            messageProperties.ContentType = ContentType.CONTENT_TYPE_JSON;
            messageProperties.ContentEncoding = this.defaultCharset;
            if (bytes != null)
            {
                messageProperties.ContentLength = bytes.Length;
            }

            this.typeMapper.FromType(obj.GetType(), messageProperties);
            return new Message(bytes, messageProperties);
        }
 /// <summary>
 /// Crate a message from the payload object and message properties provided. The message id will be added to the
 /// properties if necessary later.
 /// </summary>
 /// <param name="obj">
 /// The obj.
 /// </param>
 /// <param name="messageProperties">
 /// The message properties.
 /// </param>
 /// <returns>
 /// The Message.
 /// </returns>
 protected abstract Message CreateMessage(object obj, MessageProperties messageProperties);
        /// <summary>Convert from string to Type.</summary>
        /// <param name="properties">The message properties.</param>
        /// <returns>The type.</returns>
        public Type ToType(MessageProperties properties)
        {
            var headers = properties.Headers;

            if (!headers.ContainsKey(this.TypeIdFieldName))
            {
                throw new MessageConversionException("failed to convert Message content. Could not resolve " + this.TypeIdFieldName + " in header");
            }

            object typeIdFieldNameValue;
            try
            {
                typeIdFieldNameValue = headers[this.TypeIdFieldName];
            }
            catch (Exception)
            {
                typeIdFieldNameValue = null;
            }

            string typeId = null;
            if (typeIdFieldNameValue != null)
            {
                // Not in the Java version - validate that this is needed..
                if (typeIdFieldNameValue is byte[])
                {
                    typeId = Encoding.UTF8.GetString((byte[])typeIdFieldNameValue);
                }
                else
                {
                    typeId = typeIdFieldNameValue.ToString();
                }
            }

            if (typeId == null)
            {
                throw new MessageConversionException("failed to convert Message content. Could not resolve " + this.TypeIdFieldName + " in header");
            }

            return this.ToType(typeId);
        }
 /// <summary>The from type.</summary>
 /// <param name="type">The type.</param>
 /// <param name="properties">The properties.</param>
 public void FromType(Type type, MessageProperties properties) { properties.Headers.Add(this.TypeIdFieldName, this.FromType(type)); }
Exemple #15
0
 /// <summary>Initializes a new instance of the <see cref="Message"/> class. </summary>
 /// <param name="body">The body.</param>
 /// <param name="messageProperties">The message Properties.</param>
 public Message(byte[] body, MessageProperties messageProperties)
 {
     this.body = body;
     this.messageProperties = messageProperties;
 }
 public void MessageToSerializedObject()
 {
     var converter = new SimpleMessageConverter();
     var properties = new MessageProperties();
     properties.ContentType = MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT;
     var binaryFormatter = new BinaryFormatter();
     var byteStream = new MemoryStream();
     var testObject = new TestObject("foo");
     binaryFormatter.Serialize(byteStream, testObject);
     var bytes = byteStream.ToArray();
     var message = new Message(bytes, properties);
     var result = converter.FromMessage(message);
     Assert.AreEqual(typeof(TestObject), result.GetType());
     Assert.AreEqual(testObject, result);
 }
 public void SetUp()
 {
     this.typeMapper = new DefaultTypeMapper();
     this.props = new MessageProperties();
 }
 public void TestReplyTo()
 {
     var properties = new MessageProperties();
     properties.ReplyTo = "fanout://foo/bar";
     Assert.AreEqual("bar", properties.ReplyToAddress.RoutingKey);
 }
        /// <summary>
        /// Create MessageProperties.
        /// </summary>
        /// <param name="source">
        /// The source.
        /// </param>
        /// <param name="envelope">
        /// The envelope.
        /// </param>
        /// <param name="charset">
        /// The charset.
        /// </param>
        /// <returns>
        /// The MessageProperties.
        /// </returns>
        /// <exception cref="AmqpUnsupportedEncodingException">
        /// </exception>
        public static MessageProperties CreateMessageProperties(IBasicProperties source, BasicGetResult envelope, string charset)
        {
            var target = new MessageProperties();
            var headers = source.Headers;
            if (!CollectionUtils.IsEmpty(headers))
            {
                foreach (DictionaryEntry entry in headers)
                {
                    target.Headers[entry.Key] = entry.Value;
                }
            }

            target.Timestamp = source.Timestamp.ToDateTime();
            target.MessageId = source.MessageId;
            target.UserId = source.UserId;
            target.AppId = source.AppId;
            target.ClusterId = source.ClusterId;
            target.Type = source.Type;
            target.DeliveryMode = (MessageDeliveryMode)source.DeliveryMode;
            target.Expiration = source.Expiration;
            target.Priority = source.Priority;
            target.ContentType = source.ContentType;
            target.ContentEncoding = source.ContentEncoding;
            var correlationId = source.CorrelationId;
            if (correlationId != null)
            {
                try
                {
                    // TODO: Get the encoding from the ContentEncoding string.
                    target.CorrelationId = Encoding.UTF8.GetBytes(source.CorrelationId);
                }
                catch (Exception ex)
                {
                    throw new AmqpUnsupportedEncodingException(ex);
                }
            }

            var replyTo = source.ReplyTo;
            if (replyTo != null)
            {
                target.ReplyTo = new Address(replyTo);
            }

            if (envelope != null)
            {
                target.ReceivedExchange = envelope.Exchange;
                target.ReceivedRoutingKey = envelope.RoutingKey;
                target.Redelivered = envelope.Redelivered;
                target.DeliveryTag = (long)envelope.DeliveryTag;
            }

            return target;
        }
        /// <summary>Overridden implementation of CreateMessage, to cater for Json serialization.</summary>
        /// <param name="obj">The obj.</param>
        /// <param name="messageProperties">The message properties.</param>
        /// <returns>The Message.</returns>
        /// <exception cref="MessageConversionException"></exception>
        protected override Message CreateMessage(object obj, MessageProperties messageProperties)
        {
            byte[] bytes = null;
            try
            {
                var jsonString = string.Empty;
                var sb = new StringBuilder(128);
                var sw = new StringWriter(sb, CultureInfo.InvariantCulture);

                using (JsonWriter jsonWriter = new JsonTextWriter(sw))
                {
                    this.jsonSerializer.Serialize(jsonWriter, obj);
                    jsonString = sw.ToString();
                    var encoding = Encoding.GetEncoding(this.defaultCharset);
                    bytes = encoding.GetBytes(jsonString);
                }
            }
            catch (Exception e)
            {
                throw new MessageConversionException("Failed to convert Message content", e);
            }

            messageProperties.ContentType = MessageProperties.CONTENT_TYPE_JSON;
            messageProperties.ContentEncoding = this.defaultCharset;
            if (bytes != null)
            {
                messageProperties.ContentLength = bytes.Length;
            }

            this.typeMapper.FromType(obj.GetType(), messageProperties);
            return new Message(bytes, messageProperties);
        }