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