PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  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.
  13. namespace 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. /// <summary>
  24. /// A body serializer takes a byte array message body and just streams it out to the message
  25. /// unmodified.
  26. /// </summary>
  27. public class StringMessageSerializer :
  28. IMessageSerializer
  29. {
  30. readonly Encoding _encoding;
  31. string _body;
  32. public StringMessageSerializer(ContentType contentType, string body, Encoding encoding = null)
  33. {
  34. ContentType = contentType;
  35. _body = body;
  36. _encoding = encoding ?? Encoding.UTF8;
  37. }
  38. public ContentType ContentType { get; }
  39. public void Serialize<T>(Stream stream, SendContext<T> context)
  40. where T : class
  41. {
  42. byte[] body = _encoding.GetBytes(_body);
  43. stream.Write(body, 0, body.Length);
  44. }
  45. public void UpdateJsonHeaders(IDictionary<string, object> values)
  46. {
  47. var envelope = JObject.Parse(_body);
  48. var headersToken = envelope["headers"] ?? new JObject();
  49. var headers = headersToken.ToObject<Dictionary<string, object>>();
  50. foreach (KeyValuePair<string, object> payloadHeader in values)
  51. {
  52. headers[payloadHeader.Key] = payloadHeader.Value;
  53. }
  54. envelope["headers"] = JToken.FromObject(headers);
  55. _body = JsonConvert.SerializeObject(envelope, Formatting.Indented);
  56. }
  57. public void UpdateXmlHeaders(IDictionary<string, object> values)
  58. {
  59. using (var reader = new StringReader(_body))
  60. {
  61. var document = XDocument.Load(reader);
  62. var envelope = (from e in document.Descendants("envelope") select e).Single();
  63. var headers = (from h in envelope.Descendants("headers") select h).SingleOrDefault();
  64. if (headers == null)
  65. {
  66. headers = new XElement("headers");
  67. envelope.Add(headers);
  68. }
  69. foreach (KeyValuePair<string, object> payloadHeader in values)
  70. {
  71. headers.Add(new XElement(payloadHeader.Key, payloadHeader.Value));
  72. }
  73. _body = document.ToString(SaveOptions.DisableFormatting);
  74. }
  75. }
  76. }
  77. }