| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.IO; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Runtime.Serialization; |
| | 10 | | using Azure.Core; |
| | 11 | | using Azure.Messaging.ServiceBus.Amqp.Framing; |
| | 12 | | using Azure.Messaging.ServiceBus.Management; |
| | 13 | | using Azure.Messaging.ServiceBus.Primitives; |
| | 14 | | using Microsoft.Azure.Amqp; |
| | 15 | | using Microsoft.Azure.Amqp.Encoding; |
| | 16 | | using Microsoft.Azure.Amqp.Framing; |
| | 17 | | using SBMessage = Azure.Messaging.ServiceBus.ServiceBusMessage; |
| | 18 | |
|
| | 19 | | namespace Azure.Messaging.ServiceBus.Amqp |
| | 20 | | { |
| | 21 | | internal static class AmqpMessageConverter |
| | 22 | | { |
| | 23 | | private const string EnqueuedTimeUtcName = "x-opt-enqueued-time"; |
| | 24 | | private const string ScheduledEnqueueTimeUtcName = "x-opt-scheduled-enqueue-time"; |
| | 25 | | private const string SequenceNumberName = "x-opt-sequence-number"; |
| | 26 | | private const string EnqueueSequenceNumberName = "x-opt-enqueue-sequence-number"; |
| | 27 | | private const string LockedUntilName = "x-opt-locked-until"; |
| | 28 | | private const string PartitionKeyName = "x-opt-partition-key"; |
| | 29 | | private const string PartitionIdName = "x-opt-partition-id"; |
| | 30 | | private const string ViaPartitionKeyName = "x-opt-via-partition-key"; |
| | 31 | | private const string DeadLetterSourceName = "x-opt-deadletter-source"; |
| | 32 | | private const string TimeSpanName = AmqpConstants.Vendor + ":timespan"; |
| | 33 | | private const string UriName = AmqpConstants.Vendor + ":uri"; |
| | 34 | | private const string DateTimeOffsetName = AmqpConstants.Vendor + ":datetime-offset"; |
| | 35 | | private const int GuidSize = 16; |
| | 36 | |
|
| | 37 | | /// <summary>The size, in bytes, to use as a buffer for stream operations.</summary> |
| | 38 | | private const int StreamBufferSizeInBytes = 512; |
| | 39 | |
|
| | 40 | | public static AmqpMessage BatchSBMessagesAsAmqpMessage(IEnumerable<SBMessage> source) |
| | 41 | | { |
| 26 | 42 | | Argument.AssertNotNull(source, nameof(source)); |
| 26 | 43 | | return BuildAmqpBatchFromMessage(source); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Builds a batch <see cref="AmqpMessage" /> from a set of <see cref="SBMessage" /> |
| | 48 | | /// optionally propagating the custom properties. |
| | 49 | | /// </summary> |
| | 50 | | /// |
| | 51 | | /// <param name="source">The set of messages to use as the body of the batch message.</param> |
| | 52 | | /// |
| | 53 | | /// <returns>The batch <see cref="AmqpMessage" /> containing the source messages.</returns> |
| | 54 | | /// |
| | 55 | | private static AmqpMessage BuildAmqpBatchFromMessage(IEnumerable<SBMessage> source) |
| | 56 | | { |
| 26 | 57 | | AmqpMessage firstAmqpMessage = null; |
| 26 | 58 | | SBMessage firstMessage = null; |
| | 59 | |
|
| 26 | 60 | | return BuildAmqpBatchFromMessages( |
| 26 | 61 | | source.Select(sbMessage => |
| 26 | 62 | | { |
| 0 | 63 | | if (firstAmqpMessage == null) |
| 26 | 64 | | { |
| 0 | 65 | | firstAmqpMessage = SBMessageToAmqpMessage(sbMessage); |
| 0 | 66 | | firstMessage = sbMessage; |
| 0 | 67 | | return firstAmqpMessage; |
| 26 | 68 | | } |
| 26 | 69 | | else |
| 26 | 70 | | { |
| 0 | 71 | | return SBMessageToAmqpMessage(sbMessage); |
| 26 | 72 | | } |
| 26 | 73 | | }).ToList(), firstMessage); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Builds a batch <see cref="AmqpMessage" /> from a set of <see cref="AmqpMessage" />. |
| | 78 | | /// </summary> |
| | 79 | | /// |
| | 80 | | /// <param name="batchMessages">The set of messages to use as the body of the batch message.</param> |
| | 81 | | /// <param name="firstMessage"></param> |
| | 82 | | /// |
| | 83 | | /// <returns>The batch <see cref="AmqpMessage" /> containing the source messages.</returns> |
| | 84 | | /// |
| | 85 | | private static AmqpMessage BuildAmqpBatchFromMessages( |
| | 86 | | IList<AmqpMessage> batchMessages, |
| | 87 | | SBMessage firstMessage = null) |
| | 88 | | { |
| | 89 | | AmqpMessage batchEnvelope; |
| | 90 | |
|
| 26 | 91 | | if (batchMessages.Count == 1) |
| | 92 | | { |
| 0 | 93 | | batchEnvelope = batchMessages[0]; |
| | 94 | | } |
| | 95 | | else |
| | 96 | | { |
| 26 | 97 | | batchEnvelope = AmqpMessage.Create(batchMessages.Select(message => |
| 26 | 98 | | { |
| 0 | 99 | | message.Batchable = true; |
| 0 | 100 | | using var messageStream = message.ToStream(); |
| 0 | 101 | | return new Data { Value = ReadStreamToArraySegment(messageStream) }; |
| 0 | 102 | | })); |
| | 103 | |
|
| 26 | 104 | | batchEnvelope.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; |
| | 105 | | } |
| | 106 | |
|
| 26 | 107 | | if (firstMessage?.MessageId != null) |
| | 108 | | { |
| 0 | 109 | | batchEnvelope.Properties.MessageId = firstMessage.MessageId; |
| | 110 | | } |
| 26 | 111 | | if (firstMessage?.SessionId != null) |
| | 112 | | { |
| 0 | 113 | | batchEnvelope.Properties.GroupId = firstMessage.SessionId; |
| | 114 | | } |
| | 115 | |
|
| 26 | 116 | | if (firstMessage?.PartitionKey != null) |
| | 117 | | { |
| 0 | 118 | | batchEnvelope.MessageAnnotations.Map[AmqpMessageConverter.PartitionKeyName] = |
| 0 | 119 | | firstMessage.PartitionKey; |
| | 120 | | } |
| | 121 | |
|
| 26 | 122 | | if (firstMessage?.ViaPartitionKey != null) |
| | 123 | | { |
| 0 | 124 | | batchEnvelope.MessageAnnotations.Map[AmqpMessageConverter.ViaPartitionKeyName] = |
| 0 | 125 | | firstMessage.ViaPartitionKey; |
| | 126 | | } |
| | 127 | |
|
| 26 | 128 | | batchEnvelope.Batchable = true; |
| 26 | 129 | | return batchEnvelope; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | /// <summary> |
| | 133 | | /// Converts a stream to an <see cref="ArraySegment{T}" /> representation. |
| | 134 | | /// </summary> |
| | 135 | | /// |
| | 136 | | /// <param name="stream">The stream to read and capture in memory.</param> |
| | 137 | | /// |
| | 138 | | /// <returns>The <see cref="ArraySegment{T}" /> containing the stream data.</returns> |
| | 139 | | /// |
| | 140 | | private static ArraySegment<byte> ReadStreamToArraySegment(Stream stream) |
| | 141 | | { |
| 0 | 142 | | if (stream == null) |
| | 143 | | { |
| 0 | 144 | | return new ArraySegment<byte>(); |
| | 145 | | } |
| | 146 | |
|
| 0 | 147 | | using var memStream = new MemoryStream(StreamBufferSizeInBytes); |
| 0 | 148 | | stream.CopyTo(memStream, StreamBufferSizeInBytes); |
| | 149 | |
|
| 0 | 150 | | return new ArraySegment<byte>(memStream.ToArray()); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | public static AmqpMessage SBMessageToAmqpMessage(SBMessage sbMessage) |
| | 154 | | { |
| 74 | 155 | | var amqpMessage = sbMessage.ToAmqpMessage(); |
| 74 | 156 | | amqpMessage.Properties.MessageId = sbMessage.MessageId; |
| 74 | 157 | | amqpMessage.Properties.CorrelationId = sbMessage.CorrelationId; |
| 74 | 158 | | amqpMessage.Properties.ContentType = sbMessage.ContentType; |
| 74 | 159 | | amqpMessage.Properties.Subject = sbMessage.Label; |
| 74 | 160 | | amqpMessage.Properties.To = sbMessage.To; |
| 74 | 161 | | amqpMessage.Properties.ReplyTo = sbMessage.ReplyTo; |
| 74 | 162 | | amqpMessage.Properties.GroupId = sbMessage.SessionId; |
| 74 | 163 | | amqpMessage.Properties.ReplyToGroupId = sbMessage.ReplyToSessionId; |
| | 164 | |
|
| 74 | 165 | | if (sbMessage.TimeToLive != TimeSpan.MaxValue) |
| | 166 | | { |
| 2 | 167 | | amqpMessage.Header.Ttl = (uint)sbMessage.TimeToLive.TotalMilliseconds; |
| 2 | 168 | | amqpMessage.Properties.CreationTime = DateTime.UtcNow; |
| | 169 | |
|
| 2 | 170 | | if (AmqpConstants.MaxAbsoluteExpiryTime - amqpMessage.Properties.CreationTime.Value > sbMessage.TimeToLi |
| | 171 | | { |
| 2 | 172 | | amqpMessage.Properties.AbsoluteExpiryTime = amqpMessage.Properties.CreationTime.Value + sbMessage.Ti |
| | 173 | | } |
| | 174 | | else |
| | 175 | | { |
| 0 | 176 | | amqpMessage.Properties.AbsoluteExpiryTime = AmqpConstants.MaxAbsoluteExpiryTime; |
| | 177 | | } |
| | 178 | | } |
| | 179 | |
|
| 74 | 180 | | if ((sbMessage.ScheduledEnqueueTime != null) && sbMessage.ScheduledEnqueueTime > DateTimeOffset.MinValue) |
| | 181 | | { |
| 0 | 182 | | amqpMessage.MessageAnnotations.Map.Add(ScheduledEnqueueTimeUtcName, sbMessage.ScheduledEnqueueTime.UtcDa |
| | 183 | | } |
| | 184 | |
|
| 74 | 185 | | if (sbMessage.PartitionKey != null) |
| | 186 | | { |
| 2 | 187 | | amqpMessage.MessageAnnotations.Map.Add(PartitionKeyName, sbMessage.PartitionKey); |
| | 188 | | } |
| | 189 | |
|
| 74 | 190 | | if (sbMessage.ViaPartitionKey != null) |
| | 191 | | { |
| 2 | 192 | | amqpMessage.MessageAnnotations.Map.Add(ViaPartitionKeyName, sbMessage.ViaPartitionKey); |
| | 193 | | } |
| | 194 | |
|
| 74 | 195 | | if (sbMessage.Properties != null && sbMessage.Properties.Count > 0) |
| | 196 | | { |
| 2 | 197 | | if (amqpMessage.ApplicationProperties == null) |
| | 198 | | { |
| 0 | 199 | | amqpMessage.ApplicationProperties = new ApplicationProperties(); |
| | 200 | | } |
| | 201 | |
|
| 8 | 202 | | foreach (var pair in sbMessage.Properties) |
| | 203 | | { |
| 2 | 204 | | if (TryGetAmqpObjectFromNetObject(pair.Value, MappingType.ApplicationProperty, out var amqpObject)) |
| | 205 | | { |
| 2 | 206 | | amqpMessage.ApplicationProperties.Map.Add(pair.Key, amqpObject); |
| | 207 | | } |
| | 208 | | else |
| | 209 | | { |
| 0 | 210 | | throw new NotSupportedException(Resources.InvalidAmqpMessageProperty.FormatForUser(pair.Key.GetT |
| | 211 | | } |
| | 212 | | } |
| | 213 | | } |
| | 214 | |
|
| 74 | 215 | | return amqpMessage; |
| | 216 | | } |
| | 217 | |
|
| | 218 | | public static ServiceBusReceivedMessage AmqpMessageToSBMessage(AmqpMessage amqpMessage, bool isPeeked = false) |
| | 219 | | { |
| 8 | 220 | | Argument.AssertNotNull(amqpMessage, nameof(amqpMessage)); |
| | 221 | |
|
| 8 | 222 | | ServiceBusReceivedMessage sbMessage = amqpMessage.ToServiceBusReceivedMessage(); |
| 8 | 223 | | var sections = amqpMessage.Sections; |
| 8 | 224 | | if ((sections & SectionFlag.Header) != 0) |
| | 225 | | { |
| 6 | 226 | | if (amqpMessage.Header.Ttl != null) |
| | 227 | | { |
| 2 | 228 | | sbMessage.SentMessage.TimeToLive = TimeSpan.FromMilliseconds(amqpMessage.Header.Ttl.Value); |
| | 229 | | } |
| | 230 | |
|
| 6 | 231 | | if (amqpMessage.Header.DeliveryCount != null) |
| | 232 | | { |
| 4 | 233 | | sbMessage.DeliveryCount = isPeeked ? (int)(amqpMessage.Header.DeliveryCount.Value) : (int)(amqpMessa |
| | 234 | | } |
| | 235 | | } |
| | 236 | |
|
| 8 | 237 | | if ((sections & SectionFlag.Properties) != 0) |
| | 238 | | { |
| 2 | 239 | | if (amqpMessage.Properties.MessageId != null) |
| | 240 | | { |
| 2 | 241 | | sbMessage.SentMessage.MessageId = amqpMessage.Properties.MessageId.ToString(); |
| | 242 | | } |
| | 243 | |
|
| 2 | 244 | | if (amqpMessage.Properties.CorrelationId != null) |
| | 245 | | { |
| 2 | 246 | | sbMessage.SentMessage.CorrelationId = amqpMessage.Properties.CorrelationId.ToString(); |
| | 247 | | } |
| | 248 | |
|
| 2 | 249 | | if (amqpMessage.Properties.ContentType.Value != null) |
| | 250 | | { |
| 2 | 251 | | sbMessage.SentMessage.ContentType = amqpMessage.Properties.ContentType.Value; |
| | 252 | | } |
| | 253 | |
|
| 2 | 254 | | if (amqpMessage.Properties.Subject != null) |
| | 255 | | { |
| 2 | 256 | | sbMessage.SentMessage.Label = amqpMessage.Properties.Subject; |
| | 257 | | } |
| | 258 | |
|
| 2 | 259 | | if (amqpMessage.Properties.To != null) |
| | 260 | | { |
| 2 | 261 | | sbMessage.SentMessage.To = amqpMessage.Properties.To.ToString(); |
| | 262 | | } |
| | 263 | |
|
| 2 | 264 | | if (amqpMessage.Properties.ReplyTo != null) |
| | 265 | | { |
| 2 | 266 | | sbMessage.SentMessage.ReplyTo = amqpMessage.Properties.ReplyTo.ToString(); |
| | 267 | | } |
| | 268 | |
|
| 2 | 269 | | if (amqpMessage.Properties.GroupId != null) |
| | 270 | | { |
| 2 | 271 | | sbMessage.SentMessage.SessionId = amqpMessage.Properties.GroupId; |
| | 272 | | } |
| | 273 | |
|
| 2 | 274 | | if (amqpMessage.Properties.ReplyToGroupId != null) |
| | 275 | | { |
| 2 | 276 | | sbMessage.SentMessage.ReplyToSessionId = amqpMessage.Properties.ReplyToGroupId; |
| | 277 | | } |
| | 278 | | } |
| | 279 | |
|
| | 280 | | // Do application properties before message annotations, because the application properties |
| | 281 | | // can be updated by entries from message annotation. |
| 8 | 282 | | if ((sections & SectionFlag.ApplicationProperties) != 0) |
| | 283 | | { |
| 8 | 284 | | foreach (var pair in amqpMessage.ApplicationProperties.Map) |
| | 285 | | { |
| 2 | 286 | | if (TryGetNetObjectFromAmqpObject(pair.Value, MappingType.ApplicationProperty, out var netObject)) |
| | 287 | | { |
| 2 | 288 | | sbMessage.SentMessage.Properties[pair.Key.ToString()] = netObject; |
| | 289 | | } |
| | 290 | | } |
| | 291 | | } |
| | 292 | |
|
| 8 | 293 | | if ((sections & SectionFlag.MessageAnnotations) != 0) |
| | 294 | | { |
| 12 | 295 | | foreach (var pair in amqpMessage.MessageAnnotations.Map) |
| | 296 | | { |
| 4 | 297 | | var key = pair.Key.ToString(); |
| | 298 | | switch (key) |
| | 299 | | { |
| | 300 | | case EnqueuedTimeUtcName: |
| 0 | 301 | | sbMessage.EnqueuedTime = (DateTime)pair.Value; |
| 0 | 302 | | break; |
| | 303 | | case ScheduledEnqueueTimeUtcName: |
| 0 | 304 | | sbMessage.SentMessage.ScheduledEnqueueTime = (DateTime)pair.Value; |
| 0 | 305 | | break; |
| | 306 | | case SequenceNumberName: |
| 0 | 307 | | sbMessage.SequenceNumber = (long)pair.Value; |
| 0 | 308 | | break; |
| | 309 | | case EnqueueSequenceNumberName: |
| 0 | 310 | | sbMessage.EnqueuedSequenceNumber = (long)pair.Value; |
| 0 | 311 | | break; |
| | 312 | | case LockedUntilName: |
| 0 | 313 | | sbMessage.LockedUntil = (DateTime)pair.Value >= DateTimeOffset.MaxValue.UtcDateTime ? |
| 0 | 314 | | DateTimeOffset.MaxValue : (DateTime)pair.Value; |
| 0 | 315 | | break; |
| | 316 | | case PartitionKeyName: |
| 2 | 317 | | sbMessage.SentMessage.PartitionKey = (string)pair.Value; |
| 2 | 318 | | break; |
| | 319 | | case PartitionIdName: |
| 0 | 320 | | sbMessage.PartitionId = (short)pair.Value; |
| 0 | 321 | | break; |
| | 322 | | case ViaPartitionKeyName: |
| 2 | 323 | | sbMessage.SentMessage.ViaPartitionKey = (string)pair.Value; |
| 2 | 324 | | break; |
| | 325 | | case DeadLetterSourceName: |
| 0 | 326 | | sbMessage.DeadLetterSource = (string)pair.Value; |
| 0 | 327 | | break; |
| | 328 | | default: |
| 0 | 329 | | if (TryGetNetObjectFromAmqpObject(pair.Value, MappingType.ApplicationProperty, out var netOb |
| | 330 | | { |
| 0 | 331 | | sbMessage.SentMessage.Properties[key] = netObject; |
| | 332 | | } |
| | 333 | | break; |
| | 334 | | } |
| | 335 | | } |
| | 336 | | } |
| | 337 | |
|
| 8 | 338 | | if (amqpMessage.DeliveryTag.Count == GuidSize) |
| | 339 | | { |
| 0 | 340 | | var guidBuffer = new byte[GuidSize]; |
| 0 | 341 | | Buffer.BlockCopy(amqpMessage.DeliveryTag.Array, amqpMessage.DeliveryTag.Offset, guidBuffer, 0, GuidSize) |
| 0 | 342 | | sbMessage.LockTokenGuid = new Guid(guidBuffer); |
| | 343 | | } |
| | 344 | |
|
| 8 | 345 | | amqpMessage.Dispose(); |
| | 346 | |
|
| 8 | 347 | | return sbMessage; |
| | 348 | | } |
| | 349 | |
|
| | 350 | | public static AmqpMap GetRuleDescriptionMap(RuleProperties description) |
| | 351 | | { |
| 0 | 352 | | var ruleDescriptionMap = new AmqpMap(); |
| | 353 | |
|
| 0 | 354 | | switch (description.Filter) |
| | 355 | | { |
| | 356 | | case SqlRuleFilter sqlRuleFilter: |
| 0 | 357 | | var filterMap = GetSqlRuleFilterMap(sqlRuleFilter); |
| 0 | 358 | | ruleDescriptionMap[ManagementConstants.Properties.SqlRuleFilter] = filterMap; |
| 0 | 359 | | break; |
| | 360 | | case CorrelationRuleFilter correlationFilter: |
| 0 | 361 | | var correlationFilterMap = GetCorrelationRuleFilterMap(correlationFilter); |
| 0 | 362 | | ruleDescriptionMap[ManagementConstants.Properties.CorrelationRuleFilter] = correlationFilterMap; |
| 0 | 363 | | break; |
| | 364 | | default: |
| 0 | 365 | | throw new NotSupportedException( |
| 0 | 366 | | Resources.RuleFilterNotSupported.FormatForUser( |
| 0 | 367 | | description.Filter.GetType(), |
| 0 | 368 | | nameof(SqlRuleFilter), |
| 0 | 369 | | nameof(CorrelationRuleFilter))); |
| | 370 | | } |
| | 371 | |
|
| 0 | 372 | | var amqpAction = GetRuleActionMap(description.Action as SqlRuleAction); |
| 0 | 373 | | ruleDescriptionMap[ManagementConstants.Properties.SqlRuleAction] = amqpAction; |
| 0 | 374 | | ruleDescriptionMap[ManagementConstants.Properties.RuleName] = description.Name; |
| | 375 | |
|
| 0 | 376 | | return ruleDescriptionMap; |
| | 377 | | } |
| | 378 | |
|
| | 379 | | public static RuleProperties GetRuleDescription(AmqpRuleDescriptionCodec amqpDescription) |
| | 380 | | { |
| 0 | 381 | | var filter = GetFilter(amqpDescription.Filter); |
| 0 | 382 | | var ruleAction = GetRuleAction(amqpDescription.Action); |
| | 383 | |
|
| 0 | 384 | | var ruleDescription = new RuleProperties(amqpDescription.RuleName, filter) |
| 0 | 385 | | { |
| 0 | 386 | | Action = ruleAction |
| 0 | 387 | | }; |
| | 388 | |
|
| 0 | 389 | | return ruleDescription; |
| | 390 | | } |
| | 391 | |
|
| | 392 | | public static RuleFilter GetFilter(AmqpRuleFilterCodec amqpFilter) |
| | 393 | | { |
| | 394 | | RuleFilter filter; |
| | 395 | |
|
| 0 | 396 | | switch (amqpFilter.DescriptorCode) |
| | 397 | | { |
| | 398 | | case AmqpSqlRuleFilterCodec.Code: |
| 0 | 399 | | var amqpSqlFilter = (AmqpSqlRuleFilterCodec)amqpFilter; |
| 0 | 400 | | filter = new SqlRuleFilter(amqpSqlFilter.Expression); |
| 0 | 401 | | break; |
| | 402 | |
|
| | 403 | | case AmqpTrueRuleFilterCodec.Code: |
| 0 | 404 | | filter = new TrueRuleFilter(); |
| 0 | 405 | | break; |
| | 406 | |
|
| | 407 | | case AmqpFalseRuleFilterCodec.Code: |
| 0 | 408 | | filter = new FalseRuleFilter(); |
| 0 | 409 | | break; |
| | 410 | |
|
| | 411 | | case AmqpCorrelationRuleFilterCodec.Code: |
| 0 | 412 | | var amqpCorrelationFilter = (AmqpCorrelationRuleFilterCodec)amqpFilter; |
| 0 | 413 | | var correlationFilter = new CorrelationRuleFilter |
| 0 | 414 | | { |
| 0 | 415 | | CorrelationId = amqpCorrelationFilter.CorrelationId, |
| 0 | 416 | | MessageId = amqpCorrelationFilter.MessageId, |
| 0 | 417 | | To = amqpCorrelationFilter.To, |
| 0 | 418 | | ReplyTo = amqpCorrelationFilter.ReplyTo, |
| 0 | 419 | | Label = amqpCorrelationFilter.Label, |
| 0 | 420 | | SessionId = amqpCorrelationFilter.SessionId, |
| 0 | 421 | | ReplyToSessionId = amqpCorrelationFilter.ReplyToSessionId, |
| 0 | 422 | | ContentType = amqpCorrelationFilter.ContentType |
| 0 | 423 | | }; |
| | 424 | |
|
| 0 | 425 | | foreach (var property in amqpCorrelationFilter.Properties) |
| | 426 | | { |
| 0 | 427 | | correlationFilter.Properties.Add(property.Key.Key.ToString(), property.Value); |
| | 428 | | } |
| | 429 | |
|
| 0 | 430 | | filter = correlationFilter; |
| 0 | 431 | | break; |
| | 432 | |
|
| | 433 | | default: |
| 0 | 434 | | throw new NotSupportedException($"Unknown filter descriptor code: {amqpFilter.DescriptorCode}"); |
| | 435 | | } |
| | 436 | |
|
| 0 | 437 | | return filter; |
| | 438 | | } |
| | 439 | |
|
| | 440 | | private static RuleAction GetRuleAction(AmqpRuleActionCodec amqpAction) |
| | 441 | | { |
| | 442 | | RuleAction action; |
| | 443 | |
|
| 0 | 444 | | if (amqpAction.DescriptorCode == AmqpEmptyRuleActionCodec.Code) |
| | 445 | | { |
| 0 | 446 | | action = null; |
| | 447 | | } |
| 0 | 448 | | else if (amqpAction.DescriptorCode == AmqpSqlRuleActionCodec.Code) |
| | 449 | | { |
| 0 | 450 | | var amqpSqlRuleAction = (AmqpSqlRuleActionCodec)amqpAction; |
| 0 | 451 | | var sqlRuleAction = new SqlRuleAction(amqpSqlRuleAction.SqlExpression); |
| | 452 | |
|
| 0 | 453 | | action = sqlRuleAction; |
| | 454 | | } |
| | 455 | | else |
| | 456 | | { |
| 0 | 457 | | throw new NotSupportedException($"Unknown action descriptor code: {amqpAction.DescriptorCode}"); |
| | 458 | | } |
| | 459 | |
|
| 0 | 460 | | return action; |
| | 461 | | } |
| | 462 | |
|
| | 463 | | internal static bool TryGetAmqpObjectFromNetObject(object netObject, MappingType mappingType, out object amqpObj |
| | 464 | | { |
| 2 | 465 | | amqpObject = null; |
| 2 | 466 | | if (netObject == null) |
| | 467 | | { |
| 0 | 468 | | return true; |
| | 469 | | } |
| | 470 | |
|
| 2 | 471 | | switch (SerializationUtilities.GetTypeId(netObject)) |
| | 472 | | { |
| | 473 | | case PropertyValueType.Byte: |
| | 474 | | case PropertyValueType.SByte: |
| | 475 | | case PropertyValueType.Int16: |
| | 476 | | case PropertyValueType.Int32: |
| | 477 | | case PropertyValueType.Int64: |
| | 478 | | case PropertyValueType.UInt16: |
| | 479 | | case PropertyValueType.UInt32: |
| | 480 | | case PropertyValueType.UInt64: |
| | 481 | | case PropertyValueType.Single: |
| | 482 | | case PropertyValueType.Double: |
| | 483 | | case PropertyValueType.Boolean: |
| | 484 | | case PropertyValueType.Decimal: |
| | 485 | | case PropertyValueType.Char: |
| | 486 | | case PropertyValueType.Guid: |
| | 487 | | case PropertyValueType.DateTime: |
| | 488 | | case PropertyValueType.String: |
| 2 | 489 | | amqpObject = netObject; |
| 2 | 490 | | break; |
| | 491 | | case PropertyValueType.Stream: |
| 0 | 492 | | if (mappingType == MappingType.ApplicationProperty) |
| | 493 | | { |
| 0 | 494 | | amqpObject = StreamToBytes((Stream)netObject); |
| | 495 | | } |
| 0 | 496 | | break; |
| | 497 | | case PropertyValueType.Uri: |
| 0 | 498 | | amqpObject = new DescribedType((AmqpSymbol)UriName, ((Uri)netObject).AbsoluteUri); |
| 0 | 499 | | break; |
| | 500 | | case PropertyValueType.DateTimeOffset: |
| 0 | 501 | | amqpObject = new DescribedType((AmqpSymbol)DateTimeOffsetName, ((DateTimeOffset)netObject).UtcTicks) |
| 0 | 502 | | break; |
| | 503 | | case PropertyValueType.TimeSpan: |
| 0 | 504 | | amqpObject = new DescribedType((AmqpSymbol)TimeSpanName, ((TimeSpan)netObject).Ticks); |
| 0 | 505 | | break; |
| | 506 | | case PropertyValueType.Unknown: |
| 0 | 507 | | if (netObject is Stream netObjectAsStream) |
| | 508 | | { |
| 0 | 509 | | if (mappingType == MappingType.ApplicationProperty) |
| | 510 | | { |
| 0 | 511 | | amqpObject = StreamToBytes(netObjectAsStream); |
| | 512 | | } |
| | 513 | | } |
| 0 | 514 | | else if (mappingType == MappingType.ApplicationProperty) |
| | 515 | | { |
| 0 | 516 | | throw new SerializationException(Resources.FailedToSerializeUnsupportedType.FormatForUser(netObj |
| | 517 | | } |
| 0 | 518 | | else if (netObject is byte[] netObjectAsByteArray) |
| | 519 | | { |
| 0 | 520 | | amqpObject = new ArraySegment<byte>(netObjectAsByteArray); |
| | 521 | | } |
| 0 | 522 | | else if (netObject is IList) |
| | 523 | | { |
| | 524 | | // Array is also an IList |
| 0 | 525 | | amqpObject = netObject; |
| | 526 | | } |
| 0 | 527 | | else if (netObject is IDictionary netObjectAsDictionary) |
| | 528 | | { |
| 0 | 529 | | amqpObject = new AmqpMap(netObjectAsDictionary); |
| | 530 | | } |
| | 531 | | break; |
| | 532 | | } |
| | 533 | |
|
| 2 | 534 | | return amqpObject != null; |
| | 535 | | } |
| | 536 | |
|
| | 537 | | private static bool TryGetNetObjectFromAmqpObject(object amqpObject, MappingType mappingType, out object netObje |
| | 538 | | { |
| 2 | 539 | | netObject = null; |
| 2 | 540 | | if (amqpObject == null) |
| | 541 | | { |
| 0 | 542 | | return true; |
| | 543 | | } |
| | 544 | |
|
| 2 | 545 | | switch (SerializationUtilities.GetTypeId(amqpObject)) |
| | 546 | | { |
| | 547 | | case PropertyValueType.Byte: |
| | 548 | | case PropertyValueType.SByte: |
| | 549 | | case PropertyValueType.Int16: |
| | 550 | | case PropertyValueType.Int32: |
| | 551 | | case PropertyValueType.Int64: |
| | 552 | | case PropertyValueType.UInt16: |
| | 553 | | case PropertyValueType.UInt32: |
| | 554 | | case PropertyValueType.UInt64: |
| | 555 | | case PropertyValueType.Single: |
| | 556 | | case PropertyValueType.Double: |
| | 557 | | case PropertyValueType.Boolean: |
| | 558 | | case PropertyValueType.Decimal: |
| | 559 | | case PropertyValueType.Char: |
| | 560 | | case PropertyValueType.Guid: |
| | 561 | | case PropertyValueType.DateTime: |
| | 562 | | case PropertyValueType.String: |
| 2 | 563 | | netObject = amqpObject; |
| 2 | 564 | | break; |
| | 565 | | case PropertyValueType.Unknown: |
| 0 | 566 | | if (amqpObject is AmqpSymbol amqpObjectAsAmqpSymbol) |
| | 567 | | { |
| 0 | 568 | | netObject = (amqpObjectAsAmqpSymbol).Value; |
| | 569 | | } |
| 0 | 570 | | else if (amqpObject is ArraySegment<byte> amqpObjectAsArraySegment) |
| | 571 | | { |
| 0 | 572 | | ArraySegment<byte> binValue = amqpObjectAsArraySegment; |
| 0 | 573 | | if (binValue.Count == binValue.Array.Length) |
| | 574 | | { |
| 0 | 575 | | netObject = binValue.Array; |
| | 576 | | } |
| | 577 | | else |
| | 578 | | { |
| 0 | 579 | | var buffer = new byte[binValue.Count]; |
| 0 | 580 | | Buffer.BlockCopy(binValue.Array, binValue.Offset, buffer, 0, binValue.Count); |
| 0 | 581 | | netObject = buffer; |
| | 582 | | } |
| | 583 | | } |
| 0 | 584 | | else if (amqpObject is DescribedType amqpObjectAsDescribedType) |
| | 585 | | { |
| 0 | 586 | | if (amqpObjectAsDescribedType.Descriptor is AmqpSymbol) |
| | 587 | | { |
| 0 | 588 | | var amqpSymbol = (AmqpSymbol)amqpObjectAsDescribedType.Descriptor; |
| 0 | 589 | | if (amqpSymbol.Equals((AmqpSymbol)UriName)) |
| | 590 | | { |
| 0 | 591 | | netObject = new Uri((string)amqpObjectAsDescribedType.Value); |
| | 592 | | } |
| 0 | 593 | | else if (amqpSymbol.Equals((AmqpSymbol)TimeSpanName)) |
| | 594 | | { |
| 0 | 595 | | netObject = new TimeSpan((long)amqpObjectAsDescribedType.Value); |
| | 596 | | } |
| 0 | 597 | | else if (amqpSymbol.Equals((AmqpSymbol)DateTimeOffsetName)) |
| | 598 | | { |
| 0 | 599 | | netObject = new DateTimeOffset(new DateTime((long)amqpObjectAsDescribedType.Value, DateT |
| | 600 | | } |
| | 601 | | } |
| | 602 | | } |
| 0 | 603 | | else if (mappingType == MappingType.ApplicationProperty) |
| | 604 | | { |
| 0 | 605 | | throw new SerializationException(Resources.FailedToSerializeUnsupportedType.FormatForUser(amqpOb |
| | 606 | | } |
| 0 | 607 | | else if (amqpObject is AmqpMap map) |
| | 608 | | { |
| 0 | 609 | | var dictionary = new Dictionary<string, object>(); |
| 0 | 610 | | foreach (var pair in map) |
| | 611 | | { |
| 0 | 612 | | dictionary.Add(pair.Key.ToString(), pair.Value); |
| | 613 | | } |
| | 614 | |
|
| 0 | 615 | | netObject = dictionary; |
| | 616 | | } |
| | 617 | | else |
| | 618 | | { |
| 0 | 619 | | netObject = amqpObject; |
| | 620 | | } |
| | 621 | | break; |
| | 622 | | } |
| | 623 | |
|
| 2 | 624 | | return netObject != null; |
| | 625 | | } |
| | 626 | |
|
| | 627 | | private static ArraySegment<byte> StreamToBytes(Stream stream) |
| | 628 | | { |
| | 629 | | ArraySegment<byte> buffer; |
| 0 | 630 | | if (stream == null || stream.Length < 1) |
| | 631 | | { |
| 0 | 632 | | buffer = default; |
| | 633 | | } |
| | 634 | | else |
| | 635 | | { |
| 0 | 636 | | using (var memoryStream = new MemoryStream(512)) |
| | 637 | | { |
| 0 | 638 | | stream.CopyTo(memoryStream, 512); |
| 0 | 639 | | buffer = new ArraySegment<byte>(memoryStream.ToArray()); |
| 0 | 640 | | } |
| | 641 | | } |
| | 642 | |
|
| 0 | 643 | | return buffer; |
| | 644 | | } |
| | 645 | |
|
| | 646 | | private static Data ToData(AmqpMessage message) |
| | 647 | | { |
| 0 | 648 | | ArraySegment<byte>[] payload = message.GetPayload(); |
| 0 | 649 | | var buffer = new BufferListStream(payload); |
| 0 | 650 | | ArraySegment<byte> value = buffer.ReadBytes((int)buffer.Length); |
| 0 | 651 | | return new Data { Value = value }; |
| | 652 | | } |
| | 653 | |
|
| | 654 | | internal static AmqpMap GetSqlRuleFilterMap(SqlRuleFilter sqlRuleFilter) |
| | 655 | | { |
| 0 | 656 | | var amqpFilterMap = new AmqpMap |
| 0 | 657 | | { |
| 0 | 658 | | [ManagementConstants.Properties.Expression] = sqlRuleFilter.SqlExpression |
| 0 | 659 | | }; |
| 0 | 660 | | return amqpFilterMap; |
| | 661 | | } |
| | 662 | |
|
| | 663 | | internal static AmqpMap GetCorrelationRuleFilterMap(CorrelationRuleFilter correlationRuleFilter) |
| | 664 | | { |
| 0 | 665 | | var correlationRuleFilterMap = new AmqpMap |
| 0 | 666 | | { |
| 0 | 667 | | [ManagementConstants.Properties.CorrelationId] = correlationRuleFilter.CorrelationId, |
| 0 | 668 | | [ManagementConstants.Properties.MessageId] = correlationRuleFilter.MessageId, |
| 0 | 669 | | [ManagementConstants.Properties.To] = correlationRuleFilter.To, |
| 0 | 670 | | [ManagementConstants.Properties.ReplyTo] = correlationRuleFilter.ReplyTo, |
| 0 | 671 | | [ManagementConstants.Properties.Label] = correlationRuleFilter.Label, |
| 0 | 672 | | [ManagementConstants.Properties.SessionId] = correlationRuleFilter.SessionId, |
| 0 | 673 | | [ManagementConstants.Properties.ReplyToSessionId] = correlationRuleFilter.ReplyToSessionId, |
| 0 | 674 | | [ManagementConstants.Properties.ContentType] = correlationRuleFilter.ContentType |
| 0 | 675 | | }; |
| | 676 | |
|
| 0 | 677 | | var propertiesMap = new AmqpMap(); |
| 0 | 678 | | foreach (var property in correlationRuleFilter.Properties) |
| | 679 | | { |
| 0 | 680 | | propertiesMap[new MapKey(property.Key)] = property.Value; |
| | 681 | | } |
| | 682 | |
|
| 0 | 683 | | correlationRuleFilterMap[ManagementConstants.Properties.CorrelationRuleFilterProperties] = propertiesMap; |
| | 684 | |
|
| 0 | 685 | | return correlationRuleFilterMap; |
| | 686 | | } |
| | 687 | |
|
| | 688 | | internal static AmqpMap GetRuleActionMap(SqlRuleAction sqlRuleAction) |
| | 689 | | { |
| 0 | 690 | | AmqpMap ruleActionMap = null; |
| 0 | 691 | | if (sqlRuleAction != null) |
| | 692 | | { |
| 0 | 693 | | ruleActionMap = new AmqpMap { [ManagementConstants.Properties.Expression] = sqlRuleAction.SqlExpression |
| | 694 | | } |
| | 695 | |
|
| 0 | 696 | | return ruleActionMap; |
| | 697 | | } |
| | 698 | | } |
| | 699 | | } |