| | 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.Linq; |
| | 7 | | using Microsoft.Azure.Amqp; |
| | 8 | | using Microsoft.Azure.Amqp.Encoding; |
| | 9 | | using Microsoft.Azure.Amqp.Framing; |
| | 10 | |
|
| | 11 | | namespace Azure.Messaging.ServiceBus.Amqp |
| | 12 | | { |
| | 13 | | internal sealed class AmqpResponseMessage |
| | 14 | | { |
| 0 | 15 | | private AmqpResponseMessage(AmqpMessage responseMessage) |
| | 16 | | { |
| 0 | 17 | | AmqpMessage = responseMessage; |
| 0 | 18 | | StatusCode = AmqpMessage.GetResponseStatusCode(); |
| 0 | 19 | | if (AmqpMessage.ApplicationProperties.Map.TryGetValue<string>(ManagementConstants.Properties.TrackingId, out |
| | 20 | | { |
| 0 | 21 | | TrackingId = trackingId; |
| | 22 | | } |
| | 23 | |
|
| 0 | 24 | | if (responseMessage.ValueBody != null) |
| | 25 | | { |
| 0 | 26 | | Map = responseMessage.ValueBody.Value as AmqpMap; |
| | 27 | | } |
| 0 | 28 | | } |
| | 29 | |
|
| 0 | 30 | | public AmqpMessage AmqpMessage { get; } |
| | 31 | |
|
| 0 | 32 | | public AmqpResponseStatusCode StatusCode { get; } |
| | 33 | |
|
| 0 | 34 | | public string TrackingId { get; } |
| | 35 | |
|
| 0 | 36 | | public AmqpMap Map { get; } |
| | 37 | |
|
| | 38 | | public static AmqpResponseMessage CreateResponse(AmqpMessage response) |
| | 39 | | { |
| 0 | 40 | | return new AmqpResponseMessage(response); |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public TValue GetValue<TValue>(MapKey key) |
| | 44 | | { |
| 0 | 45 | | if (Map == null) |
| | 46 | | { |
| 0 | 47 | | throw new ArgumentException(AmqpValue.Name); |
| | 48 | | } |
| | 49 | |
|
| 0 | 50 | | var valueObject = Map[key]; |
| 0 | 51 | | if (valueObject == null) |
| | 52 | | { |
| 0 | 53 | | throw new ArgumentException(key.ToString()); |
| | 54 | | } |
| | 55 | |
|
| 0 | 56 | | if (!(valueObject is TValue)) |
| | 57 | | { |
| 0 | 58 | | throw new ArgumentException(key.ToString()); |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | return (TValue)Map[key]; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public IEnumerable<TValue> GetListValue<TValue>(MapKey key) |
| | 65 | | { |
| 0 | 66 | | if (Map == null) |
| | 67 | | { |
| 0 | 68 | | throw new ArgumentException(AmqpValue.Name); |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | var list = (List<object>)Map[key]; |
| | 72 | |
|
| 0 | 73 | | return list.Cast<TValue>(); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | public AmqpSymbol GetResponseErrorCondition() |
| | 77 | | { |
| 0 | 78 | | var condition = AmqpMessage.ApplicationProperties.Map[ManagementConstants.Response.ErrorCondition]; |
| | 79 | |
|
| 0 | 80 | | return condition is AmqpSymbol amqpSymbol ? amqpSymbol : null; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public Exception ToMessagingContractException() |
| | 84 | | { |
| 0 | 85 | | return AmqpMessage.ToMessagingContractException(StatusCode); |
| | 86 | | } |
| | 87 | | } |
| | 88 | | } |