| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Constraints; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.AspNetCore.Correlation; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents framework-neutral request correlation data resolved from the current ASP.NET Core HTTP request. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Initializes a new instance of the <see cref="AsiBackboneHttpRequestCorrelation" /> class. |
| | | 11 | | /// </remarks> |
| | | 12 | | /// <param name="correlationId">The resolved correlation identifier, when available.</param> |
| | | 13 | | /// <param name="traceId">The resolved trace identifier, when available.</param> |
| | | 14 | | /// <param name="metadata">Safe request metadata resolved from the host.</param> |
| | 45 | 15 | | public sealed class AsiBackboneHttpRequestCorrelation( |
| | 45 | 16 | | string? correlationId = null, |
| | 45 | 17 | | string? traceId = null, |
| | 45 | 18 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 19 | | { |
| | 3 | 20 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 3 | 21 | | new ReadOnlyDictionary<string, string>( |
| | 3 | 22 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the request correlation identifier, when supplied by the host or propagated request headers. |
| | | 26 | | /// </summary> |
| | 74 | 27 | | public string? CorrelationId { get; } = NormalizeOptional(correlationId); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the request trace identifier, when supplied by the host or current activity. |
| | | 31 | | /// </summary> |
| | 66 | 32 | | public string? TraceId { get; } = NormalizeOptional(traceId); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets safe request metadata supplied by the ASP.NET Core adapter. |
| | | 36 | | /// </summary> |
| | 102 | 37 | | public IReadOnlyDictionary<string, string> Metadata { get; } = NormalizeMetadata(metadata); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets a value indicating whether safe request metadata is available. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates a framework-neutral constraint evaluation context from the resolved request correlation data. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 48 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 49 | | /// <param name="metadata">Optional host-provided metadata to merge with safe request metadata.</param> |
| | | 50 | | /// <returns>A framework-neutral constraint evaluation context.</returns> |
| | | 51 | | public AsiBackboneConstraintEvaluationContext ToEvaluationContext( |
| | | 52 | | string? policyVersion = null, |
| | | 53 | | string? policyHash = null, |
| | | 54 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 55 | | { |
| | 11 | 56 | | return new AsiBackboneConstraintEvaluationContext( |
| | 11 | 57 | | CorrelationId, |
| | 11 | 58 | | policyVersion, |
| | 11 | 59 | | policyHash, |
| | 11 | 60 | | MergeMetadata(metadata)); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Merges safe request metadata with host-provided metadata. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="metadata">Optional host-provided metadata.</param> |
| | | 67 | | /// <returns>A normalized metadata dictionary.</returns> |
| | | 68 | | public IReadOnlyDictionary<string, string> MergeMetadata(IReadOnlyDictionary<string, string>? metadata) |
| | | 69 | | { |
| | 17 | 70 | | if ((metadata is null || metadata.Count == 0) && Metadata.Count == 0) |
| | | 71 | | { |
| | 4 | 72 | | return EmptyMetadata; |
| | | 73 | | } |
| | | 74 | | |
| | 13 | 75 | | Dictionary<string, string> merged = new(StringComparer.Ordinal); |
| | | 76 | | |
| | 38 | 77 | | foreach (KeyValuePair<string, string> item in Metadata) |
| | | 78 | | { |
| | 6 | 79 | | AddIfValid(merged, item.Key, item.Value); |
| | | 80 | | } |
| | | 81 | | |
| | 13 | 82 | | if (metadata is not null) |
| | | 83 | | { |
| | 110 | 84 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 85 | | { |
| | 42 | 86 | | AddIfValid(merged, item.Key, item.Value); |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | 13 | 90 | | return merged.Count == 0 |
| | 13 | 91 | | ? EmptyMetadata |
| | 13 | 92 | | : new ReadOnlyDictionary<string, string>(merged); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | private static string? NormalizeOptional(string? value) |
| | | 96 | | { |
| | 90 | 97 | | return string.IsNullOrWhiteSpace(value) |
| | 90 | 98 | | ? null |
| | 90 | 99 | | : value.Trim(); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 103 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 104 | | { |
| | 45 | 105 | | if (metadata is null || metadata.Count == 0) |
| | | 106 | | { |
| | 29 | 107 | | return EmptyMetadata; |
| | | 108 | | } |
| | | 109 | | |
| | 16 | 110 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 111 | | |
| | 80 | 112 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 113 | | { |
| | 24 | 114 | | AddIfValid(normalizedMetadata, item.Key, item.Value); |
| | | 115 | | } |
| | | 116 | | |
| | 16 | 117 | | return normalizedMetadata.Count == 0 |
| | 16 | 118 | | ? EmptyMetadata |
| | 16 | 119 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | private static void AddIfValid(Dictionary<string, string> metadata, string key, string? value) |
| | | 123 | | { |
| | 72 | 124 | | if (string.IsNullOrWhiteSpace(key)) |
| | | 125 | | { |
| | 0 | 126 | | return; |
| | | 127 | | } |
| | | 128 | | |
| | 72 | 129 | | metadata[key.Trim()] = value?.Trim() ?? string.Empty; |
| | 72 | 130 | | } |
| | | 131 | | } |