PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/src/MassTransit/Serialization/StringMessageSerializer.cs

http://github.com/MassTransit/MassTransit
C# | 93 lines | 61 code | 16 blank | 16 comment | 2 complexity | 0c83a75a7cb815d5608ea6d72e8030d2 MD5 | raw file
 1// Copyright 2007-2016 Chris Patterson, Dru Sellers, Travis Smith, et. al.
 2//  
 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 4// this file except in compliance with the License. You may obtain a copy of the 
 5// License at 
 6// 
 7//     http://www.apache.org/licenses/LICENSE-2.0 
 8// 
 9// Unless required by applicable law or agreed to in writing, software distributed
10// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
11// CONDITIONS OF ANY KIND, either express or implied. See the License for the 
12// specific language governing permissions and limitations under the License.
13namespace MassTransit.Serialization
14{
15    using System.Collections.Generic;
16    using System.IO;
17    using System.Linq;
18    using System.Net.Mime;
19    using System.Text;
20    using System.Xml.Linq;
21    using Newtonsoft.Json;
22    using Newtonsoft.Json.Linq;
23
24
25    /// <summary>
26    /// A body serializer takes a byte array message body and just streams it out to the message
27    /// unmodified.
28    /// </summary>
29    public class StringMessageSerializer :
30        IMessageSerializer
31    {
32        readonly Encoding _encoding;
33        string _body;
34
35        public StringMessageSerializer(ContentType contentType, string body, Encoding encoding = null)
36        {
37            ContentType = contentType;
38            _body = body;
39            _encoding = encoding ?? Encoding.UTF8;
40        }
41
42        public ContentType ContentType { get; }
43
44        public void Serialize<T>(Stream stream, SendContext<T> context)
45            where T : class
46        {
47            byte[] body = _encoding.GetBytes(_body);
48
49            stream.Write(body, 0, body.Length);
50        }
51
52        public void UpdateJsonHeaders(IDictionary<string, object> values)
53        {
54            var envelope = JObject.Parse(_body);
55
56            var headersToken = envelope["headers"] ?? new JObject();
57            var headers = headersToken.ToObject<Dictionary<string, object>>();
58
59            foreach (KeyValuePair<string, object> payloadHeader in values)
60            {
61                headers[payloadHeader.Key] = payloadHeader.Value;
62            }
63
64            envelope["headers"] = JToken.FromObject(headers);
65
66            _body = JsonConvert.SerializeObject(envelope, Formatting.Indented);
67        }
68
69        public void UpdateXmlHeaders(IDictionary<string, object> values)
70        {
71            using (var reader = new StringReader(_body))
72            {
73                var document = XDocument.Load(reader);
74
75                var envelope = (from e in document.Descendants("envelope") select e).Single();
76
77                var headers = (from h in envelope.Descendants("headers") select h).SingleOrDefault();
78                if (headers == null)
79                {
80                    headers = new XElement("headers");
81                    envelope.Add(headers);
82                }
83
84                foreach (KeyValuePair<string, object> payloadHeader in values)
85                {
86                    headers.Add(new XElement(payloadHeader.Key, payloadHeader.Value));
87                }
88
89                _body = document.ToString(SaveOptions.DisableFormatting);
90            }
91        }
92    }
93}