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));
            }
        }
Exemple #4
0
        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);
                    }
                }
            }
        }
    }
        public CloudEvent DecodeJObject(JObject jObject, IEnumerable <ICloudEventExtension> extensions)
        {
            if (jObject == null)
            {
                throw new ArgumentNullException(nameof(jObject));
            }

            var specVersion = GetSpecVersion(jObject);
            var cloudEvent  = new CloudEvent(specVersion, extensions);
            var attributes  = cloudEvent.GetAttributes();

            foreach (var keyValuePair in jObject)
            {
                // skip the version since we set that above
                if (keyValuePair.Key.Equals(CloudEventAttributes.SpecVersionAttributeName(CloudEventsSpecVersion.V0_1), StringComparison.OrdinalIgnoreCase) ||
                    keyValuePair.Key.Equals(CloudEventAttributes.SpecVersionAttributeName(CloudEventsSpecVersion.V0_2), StringComparison.OrdinalIgnoreCase) ||
                    keyValuePair.Key.Equals(CloudEventAttributes.SpecVersionAttributeName(CloudEventsSpecVersion.V1_0), StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (
                    specVersion == CloudEventsSpecVersion.V1_0 &&
                    keyValuePair.Key.Equals("data_base64", StringComparison.OrdinalIgnoreCase)
                    )
                {
                    attributes["data"] = Convert.FromBase64String(keyValuePair.Value.ToString());
                    continue;
                }

                if (keyValuePair.Key.Equals(CloudEventAttributes.DataAttributeName(specVersion), StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        attributes[keyValuePair.Key] = keyValuePair.Value.ToObject <T>(_serializer);
                        continue;
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException($"Failed to deserialize data of type {typeof(T).FullName}", ex);
                    }
                }

                switch (keyValuePair.Value.Type)
                {
                case JTokenType.String:
                    attributes[keyValuePair.Key] = keyValuePair.Value.ToObject <string>(_serializer);
                    break;

                case JTokenType.Date:
                    var timeValue = ((JValue)keyValuePair.Value).Value;     // The value type may be DateTime or DateTimeOffset
                    if (keyValuePair.Key.Equals(CloudEventAttributes.TimeAttributeName(specVersion), StringComparison.OrdinalIgnoreCase) &&
                        timeValue is DateTimeOffset dateTimeOffset)
                    {
                        attributes[keyValuePair.Key] = dateTimeOffset.UtcDateTime;
                    }
                    else
                    {
                        attributes[keyValuePair.Key] = timeValue;
                    }

                    break;

                case JTokenType.Uri:
                    attributes[keyValuePair.Key] = keyValuePair.Value.ToObject <Uri>(_serializer);
                    break;

                case JTokenType.Null:
                    attributes[keyValuePair.Key] = null;
                    break;

                case JTokenType.Integer:
                    attributes[keyValuePair.Key] = keyValuePair.Value.ToObject <int>(_serializer);
                    break;

                default:
                    attributes[keyValuePair.Key] = (dynamic)keyValuePair.Value;
                    break;
                }
            }

            return(cloudEvent);
        }