Esempio n. 1
0
        public static CloudEvent ToCloudEvent(this Message <string, byte[]> message,
                                              ICloudEventFormatter eventFormatter = null, params ICloudEventExtension[] extensions)
        {
            if (!IsCloudEvent(message))
            {
                throw new InvalidOperationException();
            }

            var contentType = ExtractContentType(message);

            CloudEvent cloudEvent;

            if (!string.IsNullOrEmpty(contentType) &&
                contentType.StartsWith(CloudEvent.MediaType, StringComparison.InvariantCultureIgnoreCase))
            {
                // structured mode
                if (eventFormatter == null)
                {
                    if (contentType.EndsWith(JsonEventFormatter.MediaTypeSuffix, StringComparison.InvariantCultureIgnoreCase))
                    {
                        eventFormatter = _jsonFormatter;
                    }
                    else
                    {
                        throw new InvalidOperationException("Not supported CloudEvents media formatter.");
                    }
                }

                cloudEvent = _jsonFormatter.DecodeStructuredEvent(message.Value, extensions);
            }
            else
            {
                // binary mode
                var specVersion = ExtractVersion(message);

                cloudEvent = new CloudEvent(specVersion, extensions);
                var attributes        = cloudEvent.GetAttributes();
                var cloudEventHeaders = message.Headers.Where(h => h.Key.StartsWith(KafkaCloudEventMessage.KafkaHeaderPerfix));

                foreach (var header in cloudEventHeaders)
                {
                    if (string.Equals(header.Key, SpecVersionKafkaHeader1, StringComparison.InvariantCultureIgnoreCase) ||
                        string.Equals(header.Key, SpecVersionKafkaHeader2, StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    var attributeName = header.Key.Substring(KafkaCloudEventMessage.KafkaHeaderPerfix.Length);
                    attributes.Add(attributeName,
                                   eventFormatter.DecodeAttribute(specVersion, attributeName, header.GetValueBytes(), extensions));
                }

                cloudEvent.DataContentType = contentType != null ? new ContentType(contentType) : null;
                cloudEvent.Data            = message.Value;
            }

            InitPartitioningKey(message, cloudEvent);

            return(cloudEvent);
        }
        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);
        }