private static byte[] Encode(byte[] data, CFXCodec codec = CFXCodec.raw) { byte[] result = null; if (codec == CFXCodec.gzip) { using (MemoryStream output = new MemoryStream()) { using (MemoryStream input = new MemoryStream(data)) using (GZipStream dstream = new GZipStream(output, CompressionMode.Compress)) { input.CopyTo(dstream); } result = output.ToArray(); } AppLog.Debug($"GZIP Compressed Message(s) of Size {data.Length} bytes to {result.Length} bytes"); } else { result = data; } return(result); }
public static List <CFXEnvelope> EnvelopesFromMessage(Message msg) { if (msg.Body is byte[]) { byte[] msgData = msg.Body as byte[]; CFXCodec codec = CFXCodec.raw; if (string.Compare(msg.Properties.ContentEncoding, "gzip", true) == 0) { codec = CFXCodec.gzip; } msgData = Decode(msgData, codec); List <CFXEnvelope> results; string jsonData = Encoding.UTF8.GetString(msgData); if (IsMessageList(jsonData)) { results = CFXEnvelope.FromJsonList(jsonData); } else { results = new List <CFXEnvelope>(new CFXEnvelope [] { CFXEnvelope.FromJson(jsonData) }); } return(results); } throw new ArgumentException("AMQP Message Body does not contain a valid CFX Envelope"); }
public static Message MessageFromEnvelopes(CFXEnvelope [] envelopes, CFXCodec codec = CFXCodec.raw) { if (envelopes.Length == 1) { return(MessageFromEnvelope(envelopes.First(), codec)); } List <CFXEnvelope> container = new List <CFXEnvelope>(envelopes); byte[] msgData = Encoding.UTF8.GetBytes(CFXJsonSerializer.SerializeObject(container)); msgData = Encode(msgData, codec); Message msg = new Message(msgData); msg.Properties = new Amqp.Framing.Properties { MessageId = Guid.NewGuid().ToString(), CreationTime = DateTime.Now, ContentType = "application/json; charset=\"utf-8\"" }; if (codec == CFXCodec.gzip) { msg.Properties.ContentEncoding = "gzip"; } msg.Header = new Amqp.Framing.Header() { Durable = AmqpCFXEndpoint.DurableMessages.Value }; return(msg); }
public static Message MessageFromEnvelope(CFXEnvelope env, CFXCodec codec = CFXCodec.raw) { byte[] msgData = Encode(env.ToBytes(), codec); Message msg = new Message(msgData); msg.Properties = new Amqp.Framing.Properties { MessageId = env.UniqueID.ToString(), CreationTime = env.TimeStamp, ContentType = "application/json; charset=\"utf-8\"" }; if (codec == CFXCodec.gzip) { msg.Properties.ContentEncoding = "gzip"; } msg.Header = new Amqp.Framing.Header() { Durable = AmqpCFXEndpoint.DurableMessages.Value }; return(msg); }
/// <summary> /// Converts multiple CFXEnvelope(s) into a single AMQP message /// </summary> /// <param name="envelopes">An array of CFX envelopes containing CFX messages</param> /// <param name="codec">The CODEC to be used</param> /// <param name="subjectFormat">The subject format to be applied. If null, default is used.</param> /// <returns>An AMQP message</returns> public static Message MessageFromEnvelopes(CFXEnvelope [] envelopes, CFXCodec codec = CFXCodec.raw, string subjectFormat = null) { if (envelopes.Length < 1) { return(null); } else if (envelopes.Length == 1) { return(MessageFromEnvelope(envelopes.First(), codec, subjectFormat)); } CFXEnvelope env = envelopes.First(); List <CFXEnvelope> container = new List <CFXEnvelope>(envelopes); byte[] msgData = Encoding.UTF8.GetBytes(CFXJsonSerializer.SerializeObject(container)); msgData = Encode(msgData, codec); Message msg = new Message(); msg.BodySection = new Data() { Binary = msgData }; SetHeaders(msg, env, codec, subjectFormat); return(msg); }
public static Message MessageFromEnvelope(CFXEnvelope env, CFXCodec codec = CFXCodec.raw, string subjectFormat = null) { byte[] msgData = Encode(env.ToBytes(), codec); Message msg = new Message(msgData); SetHeaders(msg, env, codec, subjectFormat); return(msg); }
public static CFXEnvelope EnvelopeFromMessage(Message msg) { if (msg.Body is byte[]) { byte[] msgData = msg.Body as byte[]; CFXCodec codec = CFXCodec.raw; if (string.Compare(msg.Properties.ContentEncoding, "gzip", true) == 0) { codec = CFXCodec.gzip; } msgData = Decode(msgData, codec); return(CFXEnvelope.FromBytes(msgData)); } throw new ArgumentException("AMQP Message Body does not contain a valid CFX Envelope"); }
private static void SetHeaders(Message msg, CFXEnvelope env, CFXCodec codec, string subjectFormat) { msg.Header = new Amqp.Framing.Header() { Durable = AmqpCFXEndpoint.DurableMessages.Value }; msg.Properties = new Amqp.Framing.Properties { MessageId = env.UniqueID.ToString(), To = env.Target, ReplyTo = env.Source, CorrelationId = env.RequestID, ContentType = "application/json; charset=\"utf-8\"", CreationTime = env.TimeStamp, }; if (string.IsNullOrWhiteSpace(subjectFormat)) { msg.Properties.Subject = $"{env.Source}.{env.MessageName}"; } else { msg.Properties.Subject = subjectFormat.Replace("${cfx-handle}", env.Source); msg.Properties.Subject = msg.Properties.Subject.Replace("${cfx-topic}", env.MessageBody.GetType().Namespace); msg.Properties.Subject = msg.Properties.Subject.Replace("${cfx-messagename}", env.MessageName); } if (codec == CFXCodec.gzip) { msg.Properties.ContentEncoding = "gzip"; } msg.ApplicationProperties = new ApplicationProperties(); msg.ApplicationProperties["cfx-topic"] = env.MessageBody.GetType().Namespace; msg.ApplicationProperties["cfx-message"] = env.MessageName; msg.ApplicationProperties["cfx-handle"] = env.Source; msg.ApplicationProperties["cfx-target"] = env.Target; }
private static byte[] Decode(byte[] data, CFXCodec codec = CFXCodec.raw) { byte[] result = null; if (codec == CFXCodec.gzip) { using (MemoryStream output = new MemoryStream()) { using (MemoryStream input = new MemoryStream(data)) using (GZipStream dstream = new GZipStream(input, CompressionMode.Decompress)) { dstream.CopyTo(output); } result = output.ToArray(); } } else { result = data; } return(result); }