| | | 1 | | using RabbitMQ.Client; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Nodes; |
| | | 5 | | |
| | | 6 | | namespace AsyncResponse.Transports.RabbitMQ; |
| | | 7 | | |
| | | 8 | | internal static class RabbitMqCorrelationIdExtractor |
| | | 9 | | { |
| | | 10 | | /// <summary>Extracts the correlation id from the supplied message.</summary> |
| | | 11 | | public static string? Extract( |
| | | 12 | | RabbitMqDelivery delivery, |
| | | 13 | | string messageJson, |
| | | 14 | | RabbitMqAsyncResponseOptions options) |
| | | 15 | | { |
| | 3 | 16 | | if (!string.IsNullOrWhiteSpace(delivery.BasicProperties.CorrelationId)) |
| | 3 | 17 | | return delivery.BasicProperties.CorrelationId; |
| | | 18 | | |
| | 3 | 19 | | if (!string.IsNullOrWhiteSpace(options.CorrelationIdHeader) |
| | 3 | 20 | | && delivery.BasicProperties.Headers is not null |
| | 3 | 21 | | && delivery.BasicProperties.Headers.TryGetValue(options.CorrelationIdHeader, out var header) |
| | 3 | 22 | | && TryConvertHeader(header) is { } headerValue |
| | 3 | 23 | | && !string.IsNullOrWhiteSpace(headerValue)) |
| | | 24 | | { |
| | 3 | 25 | | return headerValue; |
| | | 26 | | } |
| | | 27 | | |
| | 3 | 28 | | var jsonPaths = options.CorrelationIdJsonPaths; |
| | 3 | 29 | | if (jsonPaths is null || jsonPaths.Length == 0 || string.IsNullOrWhiteSpace(messageJson)) |
| | 3 | 30 | | return null; |
| | | 31 | | |
| | | 32 | | JsonNode? root; |
| | | 33 | | try |
| | | 34 | | { |
| | 3 | 35 | | root = JsonNode.Parse(messageJson); |
| | 3 | 36 | | } |
| | 3 | 37 | | catch (JsonException) |
| | | 38 | | { |
| | 3 | 39 | | return null; |
| | | 40 | | } |
| | | 41 | | |
| | 3 | 42 | | if (root is null) |
| | 3 | 43 | | return null; |
| | | 44 | | |
| | 3 | 45 | | foreach (var path in jsonPaths) |
| | | 46 | | { |
| | 3 | 47 | | var value = TryReadPath(root, path); |
| | 3 | 48 | | if (!string.IsNullOrWhiteSpace(value)) |
| | 3 | 49 | | return value; |
| | | 50 | | } |
| | | 51 | | |
| | 2 | 52 | | return null; |
| | 2 | 53 | | } |
| | | 54 | | |
| | | 55 | | private static string? TryConvertHeader(object? header) |
| | 2 | 56 | | => header switch |
| | 2 | 57 | | { |
| | 2 | 58 | | null => null, |
| | 2 | 59 | | string s => s, |
| | 2 | 60 | | byte[] bytes => Encoding.UTF8.GetString(bytes), |
| | 2 | 61 | | ReadOnlyMemory<byte> memory => Encoding.UTF8.GetString(memory.Span), |
| | 2 | 62 | | _ => header.ToString() |
| | 2 | 63 | | }; |
| | | 64 | | |
| | | 65 | | private static string? TryReadPath(JsonNode root, string path) |
| | | 66 | | { |
| | 3 | 67 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 68 | | return null; |
| | | 69 | | |
| | 3 | 70 | | var current = root; |
| | 3 | 71 | | foreach (var segment in path.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) |
| | | 72 | | { |
| | 3 | 73 | | current = UnwrapJsonString(current); |
| | 3 | 74 | | if (current is not JsonObject obj) |
| | 3 | 75 | | return null; |
| | | 76 | | |
| | 3 | 77 | | current = TryGetProperty(obj, segment); |
| | 3 | 78 | | if (current is null) |
| | 3 | 79 | | return null; |
| | | 80 | | } |
| | | 81 | | |
| | 3 | 82 | | current = UnwrapJsonString(current); |
| | 3 | 83 | | return current switch |
| | 3 | 84 | | { |
| | 3 | 85 | | JsonValue value when value.TryGetValue<string>(out var s) => s, |
| | 2 | 86 | | JsonValue value => value.ToString(), |
| | 3 | 87 | | _ => null |
| | 3 | 88 | | }; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | private static JsonNode? TryGetProperty(JsonObject obj, string name) |
| | | 92 | | { |
| | 3 | 93 | | if (obj.TryGetPropertyValue(name, out var exact)) |
| | 3 | 94 | | return exact; |
| | | 95 | | |
| | 2 | 96 | | foreach (var property in obj) |
| | | 97 | | { |
| | 2 | 98 | | if (string.Equals(property.Key, name, StringComparison.OrdinalIgnoreCase)) |
| | 2 | 99 | | return property.Value; |
| | | 100 | | } |
| | | 101 | | |
| | 2 | 102 | | return null; |
| | 3 | 103 | | } |
| | | 104 | | |
| | | 105 | | private static JsonNode? UnwrapJsonString(JsonNode? node) |
| | | 106 | | { |
| | 3 | 107 | | if (node is not JsonValue value || !value.TryGetValue<string>(out var text)) |
| | 3 | 108 | | return node; |
| | | 109 | | |
| | 3 | 110 | | var trimmed = text.AsSpan().TrimStart(); |
| | 3 | 111 | | if (trimmed.Length == 0 || (trimmed[0] != '{' && trimmed[0] != '[')) |
| | 3 | 112 | | return node; |
| | | 113 | | |
| | | 114 | | try |
| | | 115 | | { |
| | 2 | 116 | | return JsonNode.Parse(text); |
| | | 117 | | } |
| | 2 | 118 | | catch (JsonException) |
| | | 119 | | { |
| | 2 | 120 | | return node; |
| | | 121 | | } |
| | 3 | 122 | | } |
| | | 123 | | } |