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