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