Path: redb-route/redb.Route/Definitions/InterceptDefinition.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/Definitions/InterceptDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Predicates;
namespace redb.Route.Definitions;
/// Where an fires.
public enum InterceptKind
{
/// Before every step of the route (Camel intercept()).
EveryStep,
/// Once, when a message enters the route (Camel interceptFrom()).
From,
/// Before a To / ToD whose target URI matches the mask (Camel interceptSendToEndpoint()).
SendToEndpoint,
}
///
/// An interception scope: its steps run before the intercepted point, optionally only when a
/// condition holds. Declared on a route (From(...).Intercept()...EndIntercept()) or on the
/// RouteBuilder for every route it defines. Intercepts are not steps of the route — they are
/// decorators applied when the route is compiled, so they reach steps nested in Choice,
/// Split, Multicast, TryCatch and every other scope.
///
/// For the target URI is available to the intercept steps
/// in the exchange property CamelToEndpoint (), as in Apache Camel;
///
/// makes the interception replace the send (dry runs, test doubles). A Stop() inside the
/// intercept stops the exchange as anywhere else.
///
///
public sealed class InterceptDefinition : RouteDefinitionBase, IRouteScope
{
internal InterceptDefinition(InterceptKind kind, string? uriPattern)
{
if (kind == InterceptKind.SendToEndpoint)
ArgumentException.ThrowIfNullOrWhiteSpace(uriPattern);
// A blank From mask is a mistake, not "every consumer": refused here, where the author sees it,
// instead of failing the mask match at compile and silently dropping the whole route.
if (kind == InterceptKind.From && uriPattern is not null && string.IsNullOrWhiteSpace(uriPattern))
throw new ArgumentException("InterceptFrom(mask): the mask is blank. Omit it to intercept every consumer, or give a URI mask.", nameof(uriPattern));
// An invalid regex: mask fails here, where the author sees it, not inside route compilation.
if (uriPattern is not null)
redb.Route.Core.UriMask.Validate(uriPattern, nameof(uriPattern));
Kind = kind;
UriPattern = uriPattern;
}
/// Where the intercept fires.
public InterceptKind Kind { get; }
/// URI mask (exact, trailing *, or regex:); null for and an unrestricted .
public string? UriPattern { get; }
/// Optional condition; when it does not hold the intercepted point runs untouched.
public IPredicate? Condition { get; private set; }
/// For : the original send is not performed.
public bool SkipsOriginal { get; private set; }
/// Applies the intercept only when the route-language condition holds ("property.dryRun").
public InterceptDefinition When(string condition)
{
ArgumentException.ThrowIfNullOrWhiteSpace(condition);
return When(PredicateFactory.FromString(condition));
}
/// Applies the intercept only when holds.
public InterceptDefinition When(IPredicate predicate)
{
Condition = predicate ?? throw new ArgumentNullException(nameof(predicate));
return this;
}
/// Applies the intercept only when holds.
public InterceptDefinition When(Func predicate)
{
ArgumentNullException.ThrowIfNull(predicate);
return When(new LambdaPredicate(predicate));
}
/// Do not send to the original endpoint after the intercept steps (only for ).
public InterceptDefinition SkipSendToOriginalEndpoint()
{
if (Kind != InterceptKind.SendToEndpoint)
throw new InvalidOperationException("SkipSendToOriginalEndpoint() applies to InterceptSendToEndpoint(...) only.");
SkipsOriginal = true;
return this;
}
/// Closes the scope and returns to the route it was declared on.
public IRouteDefinition EndIntercept()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndIntercept() on a builder-level intercept: it has no parent route — stop chaining instead."));
///
public IRouteDefinition End() => EndIntercept();
/// Compiles the intercept body (its steps as a pipeline). The context wraps route steps with it.
public override IProcessor CreateProcessor(IRouteContext context) => NodePipeline.Body(context, Outputs);
}