PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MassTransit.HangfireIntegration/HangfireScheduledMessageData.cs

http://github.com/phatboyg/MassTransit
C# | 130 lines | 100 code | 30 blank | 0 comment | 10 complexity | cabb8e7a0d61b5f2c91bc0ce791bf8f6 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. namespace MassTransit.HangfireIntegration
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Xml.Linq;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using Scheduling;
  12. using Serialization;
  13. class HangfireScheduledMessageData : SerializedMessage
  14. {
  15. public string DestinationAddress { get; set; }
  16. public Uri Destination => new Uri(DestinationAddress);
  17. public string ContentType { get; set; }
  18. public string ExpirationTime { get; set; }
  19. public string ResponseAddress { get; set; }
  20. public string FaultAddress { get; set; }
  21. public string Body { get; set; }
  22. public string MessageId { get; set; }
  23. public string RequestId { get; set; }
  24. public string CorrelationId { get; set; }
  25. public string ConversationId { get; set; }
  26. public string InitiatorId { get; set; }
  27. public string HeadersAsJson { get; set; }
  28. public string PayloadMessageHeadersAsJson { get; set; }
  29. public static HangfireScheduledMessageData Create(ConsumeContext<ScheduleMessage> context)
  30. {
  31. var message = new HangfireScheduledMessageData();
  32. SetBaseProperties(message, context, context.Message.Destination);
  33. return message;
  34. }
  35. protected static void SetBaseProperties(HangfireScheduledMessageData message, ConsumeContext context, Uri destination)
  36. {
  37. message.DestinationAddress = destination?.ToString() ?? "";
  38. message.Body = ExtractBody(context.ReceiveContext.ContentType?.MediaType, context.ReceiveContext.GetBody(), destination);
  39. message.ContentType = context.ReceiveContext.ContentType?.MediaType;
  40. message.FaultAddress = context.FaultAddress?.ToString() ?? "";
  41. message.ResponseAddress = context.ResponseAddress?.ToString() ?? "";
  42. if (context.MessageId.HasValue)
  43. message.MessageId = context.MessageId.Value.ToString();
  44. if (context.CorrelationId.HasValue)
  45. message.CorrelationId = context.CorrelationId.Value.ToString();
  46. if (context.ConversationId.HasValue)
  47. message.ConversationId = context.ConversationId.Value.ToString();
  48. if (context.InitiatorId.HasValue)
  49. message.InitiatorId = context.InitiatorId.Value.ToString();
  50. if (context.RequestId.HasValue)
  51. message.RequestId = context.RequestId.Value.ToString();
  52. if (context.ExpirationTime.HasValue)
  53. message.ExpirationTime = context.ExpirationTime.Value.ToString("O");
  54. IEnumerable<KeyValuePair<string, object>> headers = context.Headers.GetAll();
  55. if (headers.Any())
  56. message.HeadersAsJson = JsonConvert.SerializeObject(headers);
  57. }
  58. protected static string ExtractBody(string mediaType, byte[] bytes, Uri destination)
  59. {
  60. var body = Encoding.UTF8.GetString(bytes);
  61. if (JsonMessageSerializer.JsonContentType.MediaType.Equals(mediaType, StringComparison.OrdinalIgnoreCase))
  62. body = TranslateJsonBody(body, destination.ToString());
  63. else if (XmlMessageSerializer.XmlContentType.MediaType.Equals(mediaType, StringComparison.OrdinalIgnoreCase))
  64. body = TranslateXmlBody(body, destination.ToString());
  65. else
  66. throw new InvalidOperationException("Only JSON and XML messages can be scheduled");
  67. return body;
  68. }
  69. static string TranslateJsonBody(string body, string destination)
  70. {
  71. var envelope = JObject.Parse(body);
  72. envelope["destinationAddress"] = destination;
  73. var message = envelope["message"];
  74. var payload = message["payload"];
  75. var payloadType = message["payloadType"];
  76. envelope["message"] = payload;
  77. envelope["messageType"] = payloadType;
  78. return JsonConvert.SerializeObject(envelope, Formatting.Indented);
  79. }
  80. static string TranslateXmlBody(string body, string destination)
  81. {
  82. using var reader = new StringReader(body);
  83. var document = XDocument.Load(reader);
  84. var envelope = (from e in document.Descendants("envelope") select e).Single();
  85. var destinationAddress = (from a in envelope.Descendants("destinationAddress") select a).Single();
  86. var message = (from m in envelope.Descendants("message") select m).Single();
  87. IEnumerable<XElement> messageType = (from mt in envelope.Descendants("messageType") select mt);
  88. var payload = (from p in message.Descendants("payload") select p).Single();
  89. IEnumerable<XElement> payloadType = (from pt in message.Descendants("payloadType") select pt);
  90. message.Remove();
  91. messageType.Remove();
  92. destinationAddress.Value = destination;
  93. message = new XElement("message");
  94. message.Add(payload.Descendants());
  95. envelope.Add(message);
  96. envelope.Add(payloadType.Select(x => new XElement("messageType", x.Value)));
  97. return document.ToString(SaveOptions.DisableFormatting);
  98. }
  99. }
  100. }