public static void Update <T>(this IBasicProperties self, MotorCloudEvent <byte[]> cloudEvent, RabbitMQPublisherConfig <T> config, ICloudEventFormatter cloudEventFormatter) { var messagePriority = cloudEvent.Extension <RabbitMQPriorityExtension>()?.Priority ?? config.DefaultPriority; if (messagePriority.HasValue) { self.Priority = messagePriority.Value; } var dictionary = new Dictionary <string, object>(); foreach (var attr in cloudEvent.GetAttributes()) { if (string.Equals(attr.Key, CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion)) || string.Equals(attr.Key, CloudEventAttributes.DataContentTypeAttributeName(cloudEvent.SpecVersion)) || string.Equals(attr.Key, RabbitMQPriorityExtension.PriorityAttributeName) || string.Equals(attr.Key, RabbitMQBindingConfigExtension.ExchangeAttributeName) || string.Equals(attr.Key, RabbitMQBindingConfigExtension.RoutingKeyAttributeName)) { continue; } dictionary.Add($"{CloudEventPrefix}{attr.Key}", cloudEventFormatter.EncodeAttribute(cloudEvent.SpecVersion, attr.Key, attr.Value, cloudEvent.GetExtensions().Values)); } self.Headers = dictionary; }
private void MapHeaders(CloudEvent cloudEvent) { var ignoreKeys = new List <string>(3) { CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion), CloudEventAttributes.IdAttributeName(cloudEvent.SpecVersion), CloudEventAttributes.DataContentTypeAttributeName(cloudEvent.SpecVersion), }; foreach (var attribute in cloudEvent.GetAttributes()) { if (!ignoreKeys.Contains(attribute.Key)) { var key = Constants.PropertyKeyPrefix + attribute.Key; switch (attribute.Value) { case Uri uri: UserProperties.Add(key, uri.ToString()); break; default: UserProperties.Add(key, attribute.Value); break; } } } }
private void MapHeaders(CloudEvent cloudEvent, ICloudEventFormatter formatter) { foreach (var attr in cloudEvent.GetAttributes()) { if (string.Equals(attr.Key, CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion)) || string.Equals(attr.Key, CloudEventAttributes.DataContentTypeAttributeName(cloudEvent.SpecVersion)) || string.Equals(attr.Key, PartitioningExtension.PartitioningKeyAttributeName)) { continue; } Headers.Add(KafkaHeaderPerfix + attr.Key, formatter.EncodeAttribute(cloudEvent.SpecVersion, attr.Key, attr.Value, cloudEvent.Extensions.Values)); } }
public static MotorCloudEvent <byte[]> ExtractCloudEvent <T>(this IBasicProperties self, IApplicationNameService applicationNameService, ICloudEventFormatter cloudEventFormatter, ReadOnlyMemory <byte> body, IReadOnlyCollection <ICloudEventExtension> extensions) { var specVersion = CloudEventsSpecVersion.V1_0; var attributes = new Dictionary <string, object>(); IDictionary <string, object> headers = new Dictionary <string, object>(); if (self.IsHeadersPresent() && self.Headers != null) { headers = self.Headers; } foreach (var header in headers .Where(t => t.Key.StartsWith(CloudEventPrefix)) .Select(t => new KeyValuePair <string, object>( t.Key.Substring(CloudEventPrefix.Length), t.Value))) { if (string.Equals(header.Key, CloudEventAttributes.DataContentTypeAttributeName(specVersion), StringComparison.InvariantCultureIgnoreCase) || string.Equals(header.Key, CloudEventAttributes.SpecVersionAttributeName(specVersion), StringComparison.InvariantCultureIgnoreCase)) { continue; } attributes.Add(header.Key, header.Value); } if (attributes.Count == 0) { return(new MotorCloudEvent <byte[]>(applicationNameService, body.ToArray(), typeof(T).Name, new Uri("rabbitmq://notset"), extensions: extensions.ToArray())); } var cloudEvent = new MotorCloudEvent <byte[]>(applicationNameService, body.ToArray(), extensions); foreach (var attribute in attributes) { cloudEvent.GetAttributes().Add(attribute.Key, cloudEventFormatter.DecodeAttribute( cloudEvent.SpecVersion, attribute.Key, (byte[])attribute.Value, extensions)); } return(cloudEvent); }
public AmqpCloudEventMessage(CloudEvent cloudEvent, ContentMode contentMode, ICloudEventFormatter formatter) { if (contentMode == ContentMode.Structured) { this.BodySection = new Data { Binary = formatter.EncodeStructuredEvent(cloudEvent, out var contentType) }; this.Properties = new Properties() { ContentType = contentType.MediaType }; this.ApplicationProperties = new ApplicationProperties(); MapHeaders(cloudEvent); return; } if (cloudEvent.Data is byte[]) { this.BodySection = new Data { Binary = (byte[])cloudEvent.Data }; } else if (cloudEvent.Data is Stream) { if (cloudEvent.Data is MemoryStream) { this.BodySection = new Data { Binary = ((MemoryStream)cloudEvent.Data).ToArray() }; } else { var buffer = new MemoryStream(); ((Stream)cloudEvent.Data).CopyTo(buffer); this.BodySection = new Data { Binary = buffer.ToArray() }; } } else if (cloudEvent.Data is string) { this.BodySection = new AmqpValue() { Value = cloudEvent.Data }; } this.Properties = new Properties() { ContentType = cloudEvent.DataContentType?.MediaType }; this.ApplicationProperties = new ApplicationProperties(); MapHeaders(cloudEvent); } void MapHeaders(CloudEvent cloudEvent) { foreach (var attribute in cloudEvent.GetAttributes()) { if (!attribute.Key.Equals(CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion)) && !attribute.Key.Equals(CloudEventAttributes.DataContentTypeAttributeName(cloudEvent.SpecVersion))) { if (attribute.Value is Uri) { this.ApplicationProperties.Map.Add("cloudEvents:" + attribute.Key, attribute.Value.ToString()); } else if (attribute.Value is DateTime || attribute.Value is DateTime || attribute.Value is string) { this.ApplicationProperties.Map.Add("cloudEvents:" + attribute.Key, attribute.Value); } else { Map dict = new Map(); foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(attribute.Value)) { dict.Add(descriptor.Name, descriptor.GetValue(attribute.Value)); } this.ApplicationProperties.Map.Add("cloudEvents:" + attribute.Key, dict); } } } } }
public AmqpCloudEventMessage(CloudEvent cloudEvent, ContentMode contentMode, ICloudEventFormatter formatter) { if (contentMode == ContentMode.Structured) { this.BodySection = new Data { Binary = formatter.EncodeStructuredEvent(cloudEvent, out var contentType) }; this.Properties = new Properties() { ContentType = contentType.MediaType }; this.ApplicationProperties = new ApplicationProperties(); MapHeaders(cloudEvent); return; } if (cloudEvent.Data is byte[]) { this.BodySection = new Data { Binary = (byte[])cloudEvent.Data }; } else if (cloudEvent.Data is Stream) { if (cloudEvent.Data is MemoryStream) { this.BodySection = new Data { Binary = ((MemoryStream)cloudEvent.Data).ToArray() }; } else { var buffer = new MemoryStream(); ((Stream)cloudEvent.Data).CopyTo(buffer); this.BodySection = new Data { Binary = buffer.ToArray() }; } } else if (cloudEvent.Data is string) { this.BodySection = new AmqpValue() { Value = cloudEvent.Data }; } this.Properties = new Properties() { ContentType = cloudEvent.DataContentType?.MediaType }; this.ApplicationProperties = new ApplicationProperties(); MapHeaders(cloudEvent); } void MapHeaders(CloudEvent cloudEvent) { foreach (var attribute in cloudEvent.GetAttributes()) { if (!attribute.Key.Equals(CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion)) && !attribute.Key.Equals(CloudEventAttributes.DataContentTypeAttributeName(cloudEvent.SpecVersion))) { string key = "cloudEvents:" + attribute.Key; if (attribute.Value is Uri) { this.ApplicationProperties.Map.Add(key, attribute.Value.ToString()); } else if (attribute.Value is DateTimeOffset dto) { // AMQPNetLite doesn't support DateTimeOffset values, so convert to UTC. // That means we can't roundtrip events with non-UTC timestamps, but that's not awful. this.ApplicationProperties.Map.Add(key, dto.UtcDateTime); } else if (attribute.Value is string) { this.ApplicationProperties.Map.Add(key, attribute.Value); } else { Map dict = new Map(); foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(attribute.Value)) { dict.Add(descriptor.Name, descriptor.GetValue(attribute.Value)); } this.ApplicationProperties.Map.Add("cloudEvents:" + attribute.Key, dict); } } } } }