Ejemplo n.º 1
0
        public static Message CreateMessage(RestMSMessage restMSMessage)
        {
            var header = ReadMessageHeaders(restMSMessage);
            var body = ReadMessageBody(restMSMessage);

            return new Message(header, body);
        }
Ejemplo n.º 2
0
        private static MessageBody ReadMessageBody(RestMSMessage restMSMessage)
        {
            var srcEncoding = Encoding.GetEncoding(0);
            if (restMSMessage.Content.Encoding == "QuotedPrintable" || restMSMessage.Content.Encoding == "Plain")
            {
                srcEncoding = Encoding.ASCII;
            }
            //TODO: Handle base64 messages which are allowed by specification

            var bytes = srcEncoding.GetBytes(restMSMessage.Content.Value);
            var body = Encoding.Convert(srcEncoding, Encoding.Unicode, bytes);
            return new MessageBody(Encoding.Unicode.GetString(body));
        }
Ejemplo n.º 3
0
        private static MessageHeader ReadMessageHeaders(RestMSMessage restMSMessage)
        {
            var messageId = Guid.Empty;
            if (!Guid.TryParse(restMSMessage.MessageId, out messageId))
                return FailureMessageHeader(messageId, restMSMessage.Address);

            var messageType = MessageType.MT_NONE;
            if (restMSMessage.Headers == null || !restMSMessage.Headers.Any())
                return FailureMessageHeader(messageId, restMSMessage.Address);

            var messageTypeString = restMSMessage.Headers.First(header => header.Name == "MessageType").Value;
            if (messageTypeString == null)
                return FailureMessageHeader(messageId, restMSMessage.Address);

            if (!Enum.TryParse<MessageType>(messageTypeString, out messageType))
                return FailureMessageHeader(messageId, restMSMessage.Address);

            var messageHeader = new MessageHeader(messageId, restMSMessage.Address, messageType);
            restMSMessage.Headers.Each(header => messageHeader.Bag.Add(header.Name, header.Value));
            return messageHeader;
        }
Ejemplo n.º 4
0
        private StringContent CreateMessageEntityBody(Message message)
        {
            var messageToSend = new RestMSMessage
            {
                Address = message.Header.Topic,
                MessageId = message.Header.Id.ToString(),
                Content = new RestMSMessageContent
                {
                    Value = message.Body.Value,
                    Type = "text/plain",
                    Encoding = Encoding.ASCII.WebName
                }
            };

            var messageHeaders = new List<RestMSMessageHeader> { new RestMSMessageHeader { Name = "MessageType", Value = message.Header.MessageType.ToString() } };
            messageHeaders.AddRange(message.Header.Bag.Select(customHeader => new RestMSMessageHeader { Name = customHeader.Key, Value = Encoding.ASCII.GetString((byte[])customHeader.Value) }));

            messageToSend.Headers = messageHeaders.ToArray();

            string messageContent;
            if (!XmlRequestBuilder.TryBuild(messageToSend, out messageContent)) return null;
            return new StringContent(messageContent);
        }