Path: redb-route/redb.Route.As4/Fluent/As4.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.As4/Fluent/As4.cs ---- using System.Globalization; using System.Text; namespace redb.Route.As4.Fluent; /// /// Entry point of the AS4 fluent DSL. /// /// // Receive server: accepts every partner of the node and matches each message to one agreement /// From(As4.Receive("/as4/in").Port(4090).Tls().ConnectionFactory("node").IdempotentRepository("as4-in")) /// .To("direct://inbound"); /// /// // Send to a partner: the address is the URI, the agreement is the registry /// From("direct://outbound") /// .To(As4.Send("https://ap.partner.example/as4").ConnectionFactory("node").Partner("acme")); /// /// public static class As4 { /// AS4 receive server on the given HTTP path. public static As4Builder Receive(string path) => new(As4BuilderMode.Receive, path); /// AS4 sender posting to the given partner URL. public static As4Builder Send(string url) { ArgumentException.ThrowIfNullOrWhiteSpace(url); return new(As4BuilderMode.Send, url); } /// /// AS4 sender posting to the of its partner: as4:?partner=.... A partner chosen /// per message by an expression gets its own address. /// public static As4Builder Send() => new(As4BuilderMode.Send, ""); } /// Which side of the exchange a builder produces. internal enum As4BuilderMode { /// Receive server (consumer). Receive, /// Sender (producer). Send, } /// /// Fluent builder of AS4 endpoint URIs. Receive: as4:/path?host=..&port=.., the path kept intact. /// Send: as4[s]://host[:port]/path, with HTTPS mapped onto the as4s scheme. /// public sealed class As4Builder { private readonly As4BuilderMode _mode; private readonly string _target; private readonly List<(string Key, string Value)> _options = []; private bool _useTls; internal As4Builder(As4BuilderMode mode, string target) { _mode = mode; _target = target ?? throw new ArgumentNullException(nameof(target)); } /// Registry name of the node (). Required. public As4Builder ConnectionFactory(string name) => Set("connectionFactory", name); /// Registry name of the partner to send to (send only); may be an expression such as ${header.partner}. public As4Builder Partner(string nameOrExpression) => Set("partner", nameOrExpression); /// Overrides the agreement's eb:Service (send only; the agreement must allow it). public As4Builder Service(string valueOrExpression) => Set("service", valueOrExpression); /// Overrides the agreement's eb:Action (send only; the agreement must allow it). public As4Builder Action(string valueOrExpression) => Set("action", valueOrExpression); /// eb:ConversationId of sent messages (send only). public As4Builder ConversationId(string valueOrExpression) => Set("conversationId", valueOrExpression); /// eb:RefToMessageId — the reply of a Two-Way / Push-and-Push exchange (send only). public As4Builder RefToMessageId(string valueOrExpression) => Set("refToMessageId", valueOrExpression); /// Milliseconds to wait for the partner's response carrying the receipt (send only). public As4Builder Timeout(int milliseconds) => Set("timeout", milliseconds.ToString(CultureInfo.InvariantCulture)); /// Named IIdempotentRepository that remembers received message ids (receive only, required). public As4Builder IdempotentRepository(string name) => Set("idempotentRepository", name); /// /// Receive endpoint: milliseconds after which an unconfirmed claim of a message id is taken over (default 600000); a /// copy arriving while a younger claim is in flight is refused as busy. /// public As4Builder InFlightClaimTimeout(int milliseconds) => Set("inFlightClaimTimeout", milliseconds.ToString(CultureInfo.InvariantCulture)); /// Hand a single payload to the route as a Stream instead of byte[] (receive only). public As4Builder StreamBody() => Set("streamBody", "true"); /// Always hand the payloads to the route as a list of As4Payload, with Content-IDs and part properties (receive only). public As4Builder PayloadList() => Set("payloadList", "true"); /// Bind host of the receive server (receive only). public As4Builder Host(string host) => Set("host", host); /// Listen port of the receive server (receive only). public As4Builder Port(int port) => Set("port", port.ToString(CultureInfo.InvariantCulture)); /// /// Serve the receive endpoint over TLS (receive only). Omit the certificate to take it from the node's /// connection factory; a TLS receiver that finds none refuses to start. /// public As4Builder Tls(string? certPath = null, string? certPassword = null) { _useTls = true; if (certPath is not null) Set("sslCertPath", certPath); if (certPassword is not null) Set("sslCertPassword", certPassword); return this; } /// Ask partners for a TLS client certificate (receive only; needs ). public As4Builder ClientCertificate(As4ClientCertificateMode mode, string? allowedThumbprints = null) { Set("clientCertificateMode", mode.ToString()); if (allowedThumbprints is not null) Set("allowedClientThumbprints", allowedThumbprints); return this; } /// /// Admission limit (receive only): at most concurrent pipeline executions; /// overflow beyond the optional FIFO is shed before any MIME or crypto work. /// public As4Builder MaxConcurrentRequests(int max, int queue = 0) { Set("maxConcurrentRequests", max.ToString(CultureInfo.InvariantCulture)); if (queue > 0) Set("requestQueueLimit", queue.ToString(CultureInfo.InvariantCulture)); return this; } /// Status code of a request shed by the admission limit (receive only). Default 503: an AS4 sender retries on it. public As4Builder RejectStatusCode(int statusCode) => Set("rejectStatusCode", statusCode.ToString(CultureInfo.InvariantCulture)); /// Retry-After of a shed request in seconds; 0 = not sent (receive only). public As4Builder RetryAfterSeconds(int seconds) => Set("retryAfterSeconds", seconds.ToString(CultureInfo.InvariantCulture)); /// Upper bound on a request body in bytes (receive only). public As4Builder MaxRequestBodySize(long bytes) => Set("maxRequestBodySize", bytes.ToString(CultureInfo.InvariantCulture)); /// Upper bound on the SOAP envelope in characters (receive only); payloads are not counted. public As4Builder MaxEnvelopeCharacters(long characters) => Set("maxEnvelopeCharacters", characters.ToString(CultureInfo.InvariantCulture)); /// Builds the AS4 URI string. public string Build() => _mode == As4BuilderMode.Receive ? BuildReceive() : BuildSend(); private As4Builder Set(string key, string value) { ArgumentNullException.ThrowIfNull(value); _options.RemoveAll(o => o.Key == key); _options.Add((key, value)); return this; } private string BuildReceive() { var path = _target.StartsWith('/') ? _target : "/" + _target; var sb = new StringBuilder(_useTls ? "as4s:" : "as4:").Append(path); AppendQuery(sb, '?'); return sb.ToString(); } private string BuildSend() { if (_target.Length == 0) { var own = new StringBuilder("as4:"); AppendQuery(own, '?'); return own.ToString(); } var scheme = "as4"; var rest = _target; if (rest.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { scheme = "as4s"; rest = rest["https://".Length..]; } else if (rest.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { rest = rest["http://".Length..]; } else if (rest.StartsWith("as4s://", StringComparison.OrdinalIgnoreCase)) { scheme = "as4s"; rest = rest["as4s://".Length..]; } else if (rest.StartsWith("as4://", StringComparison.OrdinalIgnoreCase)) { rest = rest["as4://".Length..]; } // The query of an as4: URI holds the endpoint's options, as http:'s does: the address's own query (Domibus // domains, ?domain=name) travels in the addressQuery option, as one encoded value. if (rest.Contains('#')) throw new ArgumentException($"AS4 partner address '{_target}' has a fragment; a partner address has none.", nameof(_target)); var query = rest.IndexOf('?'); var addressQuery = query >= 0 ? rest[(query + 1)..] : ""; if (query >= 0) rest = rest[..query]; var sb = new StringBuilder(scheme).Append("://").Append(rest); if (addressQuery.Length > 0) sb.Append("?addressQuery=").Append(Uri.EscapeDataString(addressQuery)); AppendQuery(sb, addressQuery.Length > 0 ? '&' : '?'); return sb.ToString(); } private void AppendQuery(StringBuilder sb, char first) { var sep = first; foreach (var (key, value) in _options) { sb.Append(sep).Append(key).Append('=').Append(Uri.EscapeDataString(value)); sep = '&'; } } /// Lets the builder be passed to From()/To() without calling Build(). public static implicit operator string(As4Builder builder) => builder.Build(); /// public override string ToString() => Build(); }