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