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