| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.IO; |
| | 7 | | using System.Text; |
| | 8 | | using System.Text.Json; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | | using Azure.Core; |
| | 12 | | using Azure.Core.Pipeline; |
| | 13 | | using Azure.Core.Serialization; |
| | 14 | | using Azure.Messaging.EventGrid.Models; |
| | 15 | |
|
| | 16 | | namespace Azure.Messaging.EventGrid |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Class used to decode events from the Event Grid service. |
| | 20 | | /// </summary> |
| | 21 | | public class EventGridConsumer |
| | 22 | | { |
| | 23 | | private readonly ObjectSerializer _dataSerializer; |
| | 24 | | private readonly IDictionary<string, Type> _customEventTypeMappings; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the <see cref="EventGridConsumer"/> class. |
| | 28 | | /// </summary> |
| | 29 | | public EventGridConsumer() : |
| 6 | 30 | | this(new EventGridConsumerOptions()) |
| | 31 | | { |
| 6 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Initializes a new instance of the <see cref="EventGridConsumer"/> class. |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="options"> Options for configuring deserialization. </param> |
| 18 | 38 | | public EventGridConsumer(EventGridConsumerOptions options) |
| | 39 | | { |
| 18 | 40 | | Argument.AssertNotNull(options, nameof(options)); |
| 18 | 41 | | _dataSerializer = options.DataSerializer; |
| 18 | 42 | | _customEventTypeMappings = new Dictionary<string, Type>(); |
| 60 | 43 | | foreach (KeyValuePair<string, Type> kvp in options.CustomEventTypeMappings) |
| | 44 | | { |
| 12 | 45 | | _customEventTypeMappings.Add(kvp); |
| | 46 | | } |
| 18 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Deserializes JSON encoded events and returns an array of events encoded in the EventGrid event schema. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="requestContent"> |
| | 53 | | /// The JSON encoded representation of either a single event or an array or events, encoded in the EventGrid eve |
| | 54 | | /// </param> |
| | 55 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 56 | | /// <returns>A list of EventGrid Events.</returns> |
| | 57 | | public virtual EventGridEvent[] DeserializeEventGridEvents(string requestContent, CancellationToken cancellation |
| 168 | 58 | | => DeserializeEventGridEventsInternal(requestContent, false /*async*/, cancellationToken).EnsureCompleted(); |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Deserializes JSON encoded events and returns an array of events encoded in the EventGrid event schema. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="requestContent"> |
| | 64 | | /// The JSON encoded representation of either a single event or an array or events, encoded in the EventGrid eve |
| | 65 | | /// </param> |
| | 66 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 67 | | /// <returns>A list of EventGrid Events.</returns> |
| | 68 | | public virtual async Task<EventGridEvent[]> DeserializeEventGridEventsAsync(string requestContent, CancellationT |
| 0 | 69 | | => await DeserializeEventGridEventsInternal(requestContent, true /*async*/, cancellationToken).ConfigureAwai |
| | 70 | |
|
| | 71 | | private async Task<EventGridEvent[]> DeserializeEventGridEventsInternal(string requestContent, bool async, Cance |
| | 72 | | { |
| 168 | 73 | | List<EventGridEventInternal> egInternalEvents = new List<EventGridEventInternal>(); |
| 168 | 74 | | List<EventGridEvent> egEvents = new List<EventGridEvent>(); |
| | 75 | |
|
| | 76 | | // Deserialize raw JSON string into separate events, deserialize event envelope properties |
| 168 | 77 | | JsonDocument requestDocument = await ParseJsonToDocument(requestContent, async, cancellationToken).Configure |
| 680 | 78 | | foreach (JsonElement property in requestDocument.RootElement.EnumerateArray()) |
| | 79 | | { |
| 172 | 80 | | egInternalEvents.Add(EventGridEventInternal.DeserializeEventGridEventInternal(property)); |
| | 81 | | } |
| | 82 | |
|
| | 83 | | // Deserialize 'Data' property from JsonElement for each event |
| 680 | 84 | | foreach (EventGridEventInternal egEventInternal in egInternalEvents) |
| | 85 | | { |
| 172 | 86 | | JsonElement dataElement = egEventInternal.Data; |
| 172 | 87 | | object egEventData = null; |
| | 88 | |
|
| | 89 | | // Reserialize JsonElement to stream |
| 172 | 90 | | MemoryStream dataStream = SerializePayloadToStream(dataElement, cancellationToken); |
| | 91 | |
|
| | 92 | | // First, let's attempt to find the mapping for the event type in the custom event mapping. |
| 172 | 93 | | if (_customEventTypeMappings.TryGetValue(egEventInternal.EventType, out Type typeOfEventData)) |
| | 94 | | { |
| 8 | 95 | | if (!TryGetPrimitiveFromJsonElement(dataElement, out egEventData)) |
| | 96 | | { |
| 4 | 97 | | if (async) |
| | 98 | | { |
| 0 | 99 | | egEventData = await _dataSerializer.DeserializeAsync(dataStream, typeOfEventData, cancellati |
| | 100 | | } |
| | 101 | | else |
| | 102 | | { |
| 4 | 103 | | egEventData = _dataSerializer.Deserialize(dataStream, typeOfEventData, cancellationToken); |
| | 104 | | } |
| | 105 | | } |
| | 106 | | } |
| | 107 | | // If a custom mapping doesn't exist, let's attempt to find the mapping for the deserialization function |
| 164 | 108 | | else if (SystemEventTypeMappings.SystemEventDeserializers.TryGetValue(egEventInternal.EventType, out Fun |
| | 109 | | { |
| 160 | 110 | | egEventData = systemDeserializationFunction(dataElement); |
| | 111 | | } |
| | 112 | | else |
| | 113 | | { |
| | 114 | | // If event data is not a primitive/string, return as BinaryData |
| 4 | 115 | | if (!TryGetPrimitiveFromJsonElement(dataElement, out egEventData)) |
| | 116 | | { |
| 2 | 117 | | egEventData = BinaryData.FromStream(dataStream); |
| | 118 | | } |
| | 119 | | } |
| | 120 | |
|
| 172 | 121 | | egEvents.Add(new EventGridEvent( |
| 172 | 122 | | egEventInternal.Subject, |
| 172 | 123 | | egEventData, |
| 172 | 124 | | egEventInternal.EventType, |
| 172 | 125 | | egEventInternal.DataVersion) |
| 172 | 126 | | { |
| 172 | 127 | | Id = egEventInternal.Id, |
| 172 | 128 | | EventTime = egEventInternal.EventTime |
| 172 | 129 | | }); |
| 172 | 130 | | } |
| | 131 | |
|
| 168 | 132 | | return egEvents.ToArray(); |
| 168 | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Deserializes JSON encoded events and returns an array of events encoded in the CloudEvent schema. |
| | 137 | | /// </summary> |
| | 138 | | /// <param name="requestContent"> |
| | 139 | | /// The JSON encoded representation of either a single event or an array or events, encoded in the CloudEvent sc |
| | 140 | | /// </param> |
| | 141 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 142 | | /// <returns>A list of CloudEvents.</returns> |
| | 143 | | public virtual CloudEvent[] DeserializeCloudEvents(string requestContent, CancellationToken cancellationToken = |
| 10 | 144 | | => DeserializeCloudEventsInternal(requestContent, false /*async*/, cancellationToken).EnsureCompleted(); |
| | 145 | |
|
| | 146 | | /// <summary> |
| | 147 | | /// Deserializes JSON encoded events and returns an array of events encoded in the CloudEvent schema. |
| | 148 | | /// </summary> |
| | 149 | | /// <param name="requestContent"> |
| | 150 | | /// The JSON encoded representation of either a single event or an array or events, encoded in the CloudEvent sc |
| | 151 | | /// </param> |
| | 152 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 153 | | /// <returns>A list of CloudEvents.</returns> |
| | 154 | | public virtual async Task<CloudEvent[]> DeserializeCloudEventsAsync(string requestContent, CancellationToken can |
| 0 | 155 | | => await DeserializeCloudEventsInternal(requestContent, true /*async*/, cancellationToken).ConfigureAwait(fa |
| | 156 | |
|
| | 157 | | private async Task<CloudEvent[]> DeserializeCloudEventsInternal(string requestContent, bool async, CancellationT |
| | 158 | | { |
| 10 | 159 | | List<CloudEventInternal> cloudEventsInternal = new List<CloudEventInternal>(); |
| 10 | 160 | | List<CloudEvent> cloudEvents = new List<CloudEvent>(); |
| | 161 | |
|
| | 162 | | // Deserialize raw JSON string into separate events, deserialize event envelope properties |
| 10 | 163 | | JsonDocument requestDocument = await ParseJsonToDocument(requestContent, async, cancellationToken).Configure |
| 48 | 164 | | foreach (JsonElement property in requestDocument.RootElement.EnumerateArray()) |
| | 165 | | { |
| 14 | 166 | | cloudEventsInternal.Add(CloudEventInternal.DeserializeCloudEventInternal(property)); |
| | 167 | | } |
| | 168 | |
|
| | 169 | | // Deserialize 'Data' property from JsonElement for each event |
| 48 | 170 | | foreach (CloudEventInternal cloudEventInternal in cloudEventsInternal) |
| | 171 | | { |
| 14 | 172 | | object cloudEventData = null; |
| 14 | 173 | | if (cloudEventInternal.DataBase64 != null) |
| | 174 | | { |
| 2 | 175 | | cloudEventData = Convert.FromBase64String(cloudEventInternal.DataBase64); |
| | 176 | | } |
| | 177 | | else |
| | 178 | | { |
| 12 | 179 | | JsonElement? dataElement = cloudEventInternal.Data; |
| | 180 | |
|
| 12 | 181 | | if (dataElement.HasValue && dataElement.Value.ValueKind != JsonValueKind.Null) |
| | 182 | | { |
| | 183 | | // Reserialize JsonElement to stream |
| 8 | 184 | | MemoryStream dataStream = SerializePayloadToStream(dataElement, cancellationToken); |
| | 185 | |
|
| | 186 | | // First, let's attempt to find the mapping for the event type in the custom event mapping. |
| 8 | 187 | | if (_customEventTypeMappings.TryGetValue(cloudEventInternal.Type, out Type typeOfEventData)) |
| | 188 | | { |
| 0 | 189 | | if (!TryGetPrimitiveFromJsonElement(dataElement.Value, out cloudEventData)) |
| | 190 | | { |
| 0 | 191 | | if (async) |
| | 192 | | { |
| 0 | 193 | | cloudEventData = await _dataSerializer.DeserializeAsync(dataStream, typeOfEventData, |
| | 194 | | } |
| | 195 | | else |
| | 196 | | { |
| 0 | 197 | | cloudEventData = _dataSerializer.Deserialize(dataStream, typeOfEventData, cancellati |
| | 198 | | } |
| | 199 | | } |
| | 200 | | } |
| | 201 | | // If a custom mapping doesn't exist, let's attempt to find the mapping for the deserialization |
| 8 | 202 | | else if (SystemEventTypeMappings.SystemEventDeserializers.TryGetValue(cloudEventInternal.Type, o |
| | 203 | | { |
| 8 | 204 | | cloudEventData = systemDeserializationFunction(dataElement.Value); |
| | 205 | | } |
| | 206 | | // If no custom mapping was added, either return a primitive/string, or an object wrapped as Bin |
| | 207 | | else |
| | 208 | | { |
| | 209 | | // If event data is not a primitive/string, return as BinaryData |
| 0 | 210 | | if (!TryGetPrimitiveFromJsonElement(dataElement.Value, out cloudEventData)) |
| | 211 | | { |
| 0 | 212 | | cloudEventData = BinaryData.FromStream(dataStream); |
| | 213 | | } |
| | 214 | | } |
| | 215 | | } |
| | 216 | | else // Event has null data |
| | 217 | | { |
| 4 | 218 | | cloudEventData = null; |
| 4 | 219 | | cloudEventInternal.Type = ""; |
| | 220 | | } |
| | 221 | | } |
| | 222 | |
|
| 14 | 223 | | cloudEvents.Add(new CloudEvent( |
| 14 | 224 | | cloudEventInternal.Source, |
| 14 | 225 | | cloudEventInternal.Type) |
| 14 | 226 | | { |
| 14 | 227 | | Id = cloudEventInternal.Id, |
| 14 | 228 | | Data = cloudEventData, |
| 14 | 229 | | Time = cloudEventInternal.Time, |
| 14 | 230 | | DataSchema = cloudEventInternal.Dataschema, |
| 14 | 231 | | DataContentType = cloudEventInternal.Datacontenttype, |
| 14 | 232 | | Subject = cloudEventInternal.Subject |
| 14 | 233 | | }); |
| 14 | 234 | | } |
| | 235 | |
|
| 10 | 236 | | return cloudEvents.ToArray(); |
| 10 | 237 | | } |
| | 238 | |
|
| | 239 | | private static bool TryGetPrimitiveFromJsonElement(JsonElement jsonElement, out object elementValue) |
| | 240 | | { |
| 12 | 241 | | elementValue = null; |
| 12 | 242 | | if (jsonElement.ValueKind == JsonValueKind.True || jsonElement.ValueKind == JsonValueKind.False) |
| | 243 | | { |
| 2 | 244 | | elementValue = jsonElement.GetBoolean(); |
| | 245 | | } |
| 10 | 246 | | else if (jsonElement.ValueKind == JsonValueKind.Number) |
| | 247 | | { |
| 0 | 248 | | if (jsonElement.TryGetInt32(out var vali)) |
| | 249 | | { |
| 0 | 250 | | elementValue = vali; |
| | 251 | | } |
| 0 | 252 | | if (jsonElement.TryGetInt64(out var vall)) |
| | 253 | | { |
| 0 | 254 | | elementValue = vall; |
| | 255 | | } |
| 0 | 256 | | if (jsonElement.TryGetDouble(out var val)) |
| | 257 | | { |
| 0 | 258 | | elementValue = val; |
| | 259 | | } |
| | 260 | | } |
| 10 | 261 | | else if (jsonElement.ValueKind == JsonValueKind.String) |
| | 262 | | { |
| 4 | 263 | | elementValue = jsonElement.GetString(); |
| | 264 | | } |
| 6 | 265 | | else if (jsonElement.ValueKind == JsonValueKind.Undefined) |
| | 266 | | { |
| 0 | 267 | | elementValue = ""; |
| | 268 | | } |
| | 269 | |
|
| 12 | 270 | | return elementValue != null; |
| | 271 | | } |
| | 272 | |
|
| | 273 | | private static async Task<JsonDocument> ParseJsonToDocument(string requestContent, bool async, CancellationToken |
| | 274 | | { |
| 178 | 275 | | MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(requestContent)); |
| 178 | 276 | | if (async) |
| | 277 | | { |
| 0 | 278 | | return await JsonDocument.ParseAsync(stream, default, cancellationToken).ConfigureAwait(false); |
| | 279 | | } |
| | 280 | | else |
| | 281 | | { |
| 178 | 282 | | return JsonDocument.Parse(stream, default); |
| | 283 | | } |
| 178 | 284 | | } |
| | 285 | |
|
| | 286 | | private static MemoryStream SerializePayloadToStream(JsonElement? dataElement, CancellationToken cancellationTok |
| | 287 | | { |
| 180 | 288 | | MemoryStream dataStream = new MemoryStream(); |
| 180 | 289 | | JsonObjectSerializer serializer = new JsonObjectSerializer(); |
| 180 | 290 | | serializer.Serialize(dataStream, dataElement, dataElement.GetType(), cancellationToken); |
| 180 | 291 | | dataStream.Position = 0; |
| 180 | 292 | | return dataStream; |
| | 293 | | } |
| | 294 | | } |
| | 295 | | } |