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