private IMessage SerializeAndEmbedHeadersIfApplicable(IMessage message) { var transformed = new MessageValues(message); object payload; if (embedHeaders) { transformed.TryGetValue(MessageHeaders.CONTENT_TYPE, out var contentType); // transform content type headers to String, so that they can be properly // embedded in JSON if (contentType != null) { transformed[MessageHeaders.CONTENT_TYPE] = contentType.ToString(); } payload = EmbeddedHeaderUtils.EmbedHeaders(transformed, embeddedHeaders); } else { payload = transformed.Payload; } return(IntegrationServices.MessageBuilderFactory.WithPayload(payload).CopyHeaders(transformed.Headers).Build()); }
public static byte[] EmbedHeaders(MessageValues original, params string[] headers) { try { var headerValues = new byte[headers.Length][]; var n = 0; var headerCount = 0; var headersLength = 0; foreach (var header in headers) { original.TryGetValue(header, out var value); if (value != null) { var json = JsonConvert.SerializeObject(value); headerValues[n] = Encoding.UTF8.GetBytes(json); headerCount++; headersLength += header.Length + headerValues[n++].Length; } else { headerValues[n++] = null; } } // 0xff, n(1), [ [lenHdr(1), hdr, lenValue(4), value] ... ] var byteBuffer = new MemoryStream(); byteBuffer.WriteByte((byte)0xff); // signal new format byteBuffer.WriteByte((byte)headerCount); for (var i = 0; i < headers.Length; i++) { if (headerValues[i] != null) { byteBuffer.WriteByte((byte)headers[i].Length); var buffer = Encoding.UTF8.GetBytes(headers[i]); byteBuffer.Write(buffer, 0, buffer.Length); buffer = GetBigEndianBytes(headerValues[i].Length); byteBuffer.Write(buffer, 0, buffer.Length); byteBuffer.Write(headerValues[i], 0, headerValues[i].Length); } } var payloadBuffer = (byte[])original.Payload; byteBuffer.Write(payloadBuffer, 0, payloadBuffer.Length); return(byteBuffer.ToArray()); } catch (Exception) { // Log throw; } }