Path: redb-route/redb.Route.IbmMq/IbmMqXmsHeaderMapper.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.IbmMq/IbmMqXmsHeaderMapper.cs
----
using IBM.XMS;
using redb.Route.Abstractions;
using RouteMessage = redb.Route.Core.Message;
using XmsMessage = IBM.XMS.IMessage;
namespace redb.Route.IbmMq;
///
/// Maps an XMS message's JMS/MQMD metadata and user headers onto a redb route message, so the
/// event-driven (XMS) receive path carries the same redbIbmMq.* and application headers as the
/// poll path ( +
/// ).
///
/// User headers are read from the single JSON-encoded property the
/// redb producer writes — the exact counterpart of the poll consumer's RFH2 read — so a redb→redb hop
/// round-trips application headers identically on either receive mode.
///
///
internal static class IbmMqXmsHeaderMapper
{
///
/// The metadata and the user headers lets in; is the
/// one they come into (for a request-reply's reply, the request's).
///
public static void CopyToHeaders(XmsMessage msg, RouteMessage routeMsg, string destination, IbmMqEndpointOptions options,
IHeaderFilterStrategy headerFilter, IExchange exchange)
{
// MQMD-equivalent metadata — gated by MqmdReadEnabled exactly like the poll path.
if (options.MqmdReadEnabled)
{
routeMsg.Headers[IbmMqHeaders.Destination] = destination;
routeMsg.Headers[IbmMqHeaders.QueueManager] = options.QueueManager;
var msgId = MessageIdToHex(TryGet(() => msg.JMSMessageID));
if (!string.IsNullOrEmpty(msgId))
routeMsg.Headers[IbmMqHeaders.MsgId] = msgId;
var correlBytes = TryGet(() => msg.JMSCorrelationIDAsBytes);
if (correlBytes is { Length: > 0 })
routeMsg.Headers[IbmMqHeaders.CorrelId] = IbmMqMessageHelper.BytesToHex(correlBytes);
var priority = TryGetStruct(() => msg.JMSPriority);
if (priority.HasValue)
routeMsg.Headers[IbmMqHeaders.Priority] = priority.Value;
var replyToName = TryGet(() => msg.JMSReplyTo?.Name);
if (!string.IsNullOrEmpty(replyToName))
routeMsg.Headers[IbmMqHeaders.ReplyToQueue] = BareQueueName(replyToName);
var deliveryCount = TryGetStruct(() => msg.GetIntProperty(XMSC.JMSX_DELIVERY_COUNT));
if (deliveryCount is > 0)
routeMsg.Headers[IbmMqHeaders.BackoutCount] = deliveryCount.Value - 1;
}
CopyUserHeaders(msg, routeMsg, headerFilter, exchange);
}
/// Reads the redb producer's JSON header catalogue and applies each user header the strategy lets in.
private static void CopyUserHeaders(XmsMessage msg, RouteMessage routeMsg, IHeaderFilterStrategy headerFilter, IExchange exchange)
{
var json = TryGet(() => msg.GetStringProperty(IbmMqHeaders.HeaderCatalogue));
if (string.IsNullOrEmpty(json)) return;
try
{
var dict = System.Text.Json.JsonSerializer.Deserialize>(json);
if (dict is null) return;
foreach (var (key, value) in dict)
{
if (headerFilter.FilterIncoming(key, value, exchange)) continue;
routeMsg.Headers[key] = value;
}
}
catch { /* malformed JSON — skip, matching the poll path */ }
}
/// JMS message id ("ID:" + hex) → the raw hex the poll path stores for MQMD MsgId.
private static string? MessageIdToHex(string? jmsMessageId)
{
if (string.IsNullOrEmpty(jmsMessageId)) return null;
return jmsMessageId.StartsWith("ID:", StringComparison.Ordinal) ? jmsMessageId[3..] : jmsMessageId;
}
/// Bare MQ queue name from an XMS destination name (queue://qmgr/NAME or plain).
private static string BareQueueName(string name)
{
var slash = name.LastIndexOf('/');
return (slash >= 0 ? name[(slash + 1)..] : name).Trim();
}
private static T? TryGet(Func f) where T : class
{
try { return f(); } catch { return null; }
}
private static T? TryGetStruct(Func f) where T : struct
{
try { return f(); } catch { return null; }
}
}