Path: redb-route/redb.Route/Definitions/SetHeaderDefinition.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/SetHeaderDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Leaf definition that sets a header on the exchange to a static value.
///
public sealed class SetHeaderStaticDefinition : ProcessorDefinition
{
private readonly string _key;
private readonly object? _value;
/// Creates a set-header definition with a static value.
public SetHeaderStaticDefinition(string key, object? value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key);
_key = key;
_value = value;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(exchange => exchange.In.Headers[_key] = _value);
}
///
/// Leaf definition that sets a header on the exchange using a factory function.
///
public sealed class SetHeaderFactoryDefinition : ProcessorDefinition
{
private readonly string _key;
private readonly Func _factory;
/// Creates a set-header definition using a factory.
public SetHeaderFactoryDefinition(string key, Func factory)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key);
ArgumentNullException.ThrowIfNull(factory);
_key = key;
_factory = factory;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(exchange => exchange.In.Headers[_key] = _factory(exchange));
}
///
/// Leaf definition that sets a header on the exchange using an .
///
public sealed class SetHeaderExpressionDefinition : ProcessorDefinition
{
private readonly string _name;
private readonly IExpression _expression;
/// The header name to set.
public string Name => _name;
/// The expression producing the header value.
public IExpression Expression => _expression;
/// Creates a set-header definition from an expression.
public SetHeaderExpressionDefinition(string name, IExpression expression)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentNullException.ThrowIfNull(expression);
_name = name;
_expression = expression;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ExpressionHeaderProcessor(_name, _expression);
}
///
/// Leaf definition that removes a header from the exchange.
///
public sealed class RemoveHeaderDefinition : ProcessorDefinition
{
private readonly string _key;
/// Creates a remove-header definition.
public RemoveHeaderDefinition(string key)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key);
_key = key;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(exchange => exchange.In.Headers.Remove(_key));
}
///
/// Leaf definition that removes the body of the exchange (sets it to null).
///
public sealed class RemoveBodyDefinition : ProcessorDefinition
{
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(exchange => exchange.In.Body = null);
}