Path: redb-route/redb.Route/Processors/EnrichProcessor.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Processors/EnrichProcessor.cs ---- using redb.Route.Abstractions; using redb.Route.Core; namespace redb.Route.Processors; /// /// Content Enricher: calls an external endpoint and merges the result into the current exchange. /// Implements the Content Enricher EIP pattern. The producer is cached for the lifetime /// of this processor and started on first use. /// public sealed class EnrichProcessor : IProcessor { private IEndpoint? _endpoint; private readonly IRouteContext _context; private readonly string? _resourceUri; private readonly DynamicEndpointResolver? _resolver; private readonly Func _mergeStrategy; private IProducer? _producer; /// Creates an enrich processor. /// Route context for resolving the resource endpoint. /// URI of the enrichment endpoint. /// Function merging original and enrichment exchanges: (original, enriched) → merged. public EnrichProcessor( IRouteContext context, string resourceUri, Func mergeStrategy) { _context = context ?? throw new ArgumentNullException(nameof(context)); _resourceUri = resourceUri ?? throw new ArgumentNullException(nameof(resourceUri)); (context as RouteContext)?.NoteStaticEndpoint(resourceUri); _mergeStrategy = mergeStrategy ?? throw new ArgumentNullException(nameof(mergeStrategy)); } /// Creates an enrich processor with a dynamic endpoint resolver. public EnrichProcessor( IRouteContext context, DynamicEndpointResolver resolver, Func mergeStrategy) { _context = context ?? throw new ArgumentNullException(nameof(context)); _resolver = resolver ?? throw new ArgumentNullException(nameof(resolver)); _mergeStrategy = mergeStrategy ?? throw new ArgumentNullException(nameof(mergeStrategy)); } /// public async Task Process(IExchange exchange, CancellationToken ct = default) { var resourceExchange = exchange.CorrelatedCopyLinked(); try { resourceExchange.Pattern = ExchangePattern.InOut; var (endpoint, producer) = _resolver is not null ? await _resolver.ResolvePairAsync(resourceExchange, ct).ConfigureAwait(false) : await GetOrCreatePairAsync(ct).ConfigureAwait(false); await CountedSend.Process(endpoint, producer, resourceExchange, ct).ConfigureAwait(false); // Merge the response back into the original exchange var merged = _mergeStrategy(exchange, resourceExchange); exchange.In.Body = merged.In.Body; foreach (var header in merged.In.Headers) exchange.In.Headers[header.Key] = header.Value; if (merged.Exception != null) exchange.Exception = merged.Exception; } finally { // As Apache Camel's Enrich hands the resource exchange's completions over: a streamed body merged into the // original keeps its connection until the original ends, not until this copy is disposed. ExchangeResources.HandOver(resourceExchange, exchange); if (resourceExchange is IAsyncDisposable d) await d.DisposeAsync().ConfigureAwait(false); } } private async Task<(IEndpoint, IProducer)> GetOrCreatePairAsync(CancellationToken ct) { if (_producer is not null) return (_endpoint!, _producer); _endpoint = _context.GetEndpoint(_resourceUri!); _producer = _endpoint.CreateProducerOnItsSide(); await _producer.Start(ct).ConfigureAwait(false); (_context as RouteContext)?.TrackProducer(_producer); return (_endpoint, _producer); } } /// /// Poll Enricher: sends a request to an endpoint with a timeout, merging the result /// into the current exchange. If the endpoint does not respond within the timeout, /// the merge strategy receives a null exchange, allowing graceful handling. /// The producer is cached for the lifetime of this processor. /// public sealed class PollEnrichProcessor : IProcessor { private readonly IRouteContext _context; private readonly string? _resourceUri; private readonly DynamicEndpointResolver? _resolver; private readonly Func _mergeStrategy; private readonly TimeSpan _timeout; private IProducer? _producer; private IEndpoint? _endpoint; /// Creates a poll enrich processor. /// Route context for resolving the resource endpoint. /// URI of the polling endpoint. /// Function merging original and polled exchanges: (original, polled?) → merged. /// Timeout waiting for data (default: 1 second). public PollEnrichProcessor( IRouteContext context, string resourceUri, Func mergeStrategy, TimeSpan? timeout = null) { _context = context ?? throw new ArgumentNullException(nameof(context)); _resourceUri = resourceUri ?? throw new ArgumentNullException(nameof(resourceUri)); (context as RouteContext)?.NoteStaticEndpoint(resourceUri); _mergeStrategy = mergeStrategy ?? throw new ArgumentNullException(nameof(mergeStrategy)); _timeout = timeout ?? TimeSpan.FromSeconds(1); } /// Creates a poll enrich processor with a dynamic endpoint resolver. public PollEnrichProcessor( IRouteContext context, DynamicEndpointResolver resolver, Func mergeStrategy, TimeSpan? timeout = null) { _context = context ?? throw new ArgumentNullException(nameof(context)); _resolver = resolver ?? throw new ArgumentNullException(nameof(resolver)); _mergeStrategy = mergeStrategy ?? throw new ArgumentNullException(nameof(mergeStrategy)); _timeout = timeout ?? TimeSpan.FromSeconds(1); } /// public async Task Process(IExchange exchange, CancellationToken ct = default) { IExchange? pollExchange = null; IExchange? resourceExchange = null; try { try { using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(ct); timeoutCts.CancelAfter(_timeout); // As Camel's pollEnrich, the polled exchange is not the caller's: an id of its own, no correlation id. pollExchange = exchange.CorrelatedCopyLinked(); pollExchange.Properties.Remove(ExchangePropertyKeys.CorrelationId); pollExchange.Pattern = ExchangePattern.InOut; var (endpoint, producer) = _resolver is not null ? await _resolver.ResolvePairAsync(pollExchange, timeoutCts.Token).ConfigureAwait(false) : GetOrCreatePair(); await CountedSend.Process(endpoint, producer, pollExchange, timeoutCts.Token).ConfigureAwait(false); resourceExchange = pollExchange; } catch (OperationCanceledException) when (!ct.IsCancellationRequested) { // Timeout — resourceExchange stays null } // Merge (polled may be null if timeout) var merged = _mergeStrategy(exchange, resourceExchange); exchange.In.Body = merged.In.Body; foreach (var header in merged.In.Headers) exchange.In.Headers[header.Key] = header.Value; if (merged.Exception != null) exchange.Exception = merged.Exception; } finally { // The polled exchange's resources go with the original, as in Process. if (pollExchange is not null) ExchangeResources.HandOver(pollExchange, exchange); if (pollExchange is IAsyncDisposable d) await d.DisposeAsync().ConfigureAwait(false); } } private (IEndpoint, IProducer) GetOrCreatePair() { if (_producer is not null) return (_endpoint!, _producer); _endpoint = _context.GetEndpoint(_resourceUri!); _producer = _endpoint.CreateProducerOnItsSide(); return (_endpoint, _producer); } }