| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Xml.Linq; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Xml; |
| | 8 | | using System.Globalization; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.ServiceBus.Management |
| | 11 | | { |
| | 12 | | internal static class QueuePropertiesExtensions |
| | 13 | | { |
| | 14 | | public static XDocument Serialize(this QueueProperties description) |
| | 15 | | { |
| 28 | 16 | | var queueDescriptionElements = new List<object>() |
| 28 | 17 | | { |
| 28 | 18 | | new XElement(XName.Get("LockDuration", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToStri |
| 28 | 19 | | new XElement(XName.Get("MaxSizeInMegabytes", ManagementClientConstants.ServiceBusNamespace), XmlConvert. |
| 28 | 20 | | new XElement(XName.Get("RequiresDuplicateDetection", ManagementClientConstants.ServiceBusNamespace), Xml |
| 28 | 21 | | new XElement(XName.Get("RequiresSession", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToS |
| 28 | 22 | | description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeTo |
| 28 | 23 | | new XElement(XName.Get("DeadLetteringOnMessageExpiration", ManagementClientConstants.ServiceBusNamespace |
| 28 | 24 | | description.RequiresDuplicateDetection && description.DuplicateDetectionHistoryTimeWindow != default ? |
| 28 | 25 | | new XElement(XName.Get("DuplicateDetectionHistoryTimeWindow", ManagementClientConstants.ServiceBusNa |
| 28 | 26 | | : null, |
| 28 | 27 | | new XElement(XName.Get("MaxDeliveryCount", ManagementClientConstants.ServiceBusNamespace), XmlConvert.To |
| 28 | 28 | | new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlCon |
| 28 | 29 | | description.AuthorizationRules?.Serialize(), |
| 28 | 30 | | new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToSt |
| 28 | 31 | | description.ForwardTo != null ? new XElement(XName.Get("ForwardTo", ManagementClientConstants.ServiceBus |
| 28 | 32 | | description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.Serv |
| 28 | 33 | | description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", Managemen |
| 28 | 34 | | new XElement(XName.Get("EnablePartitioning", ManagementClientConstants.ServiceBusNamespace), XmlConvert. |
| 28 | 35 | | description.ForwardDeadLetteredMessagesTo != null ? new XElement(XName.Get("ForwardDeadLetteredMessagesT |
| 28 | 36 | | }; |
| | 37 | |
|
| 28 | 38 | | if (description.UnknownProperties != null) |
| | 39 | | { |
| 4 | 40 | | queueDescriptionElements.AddRange(description.UnknownProperties); |
| | 41 | | } |
| | 42 | |
|
| 28 | 43 | | return new XDocument( |
| 28 | 44 | | new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace), |
| 28 | 45 | | new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace), |
| 28 | 46 | | new XAttribute("type", "application/xml"), |
| 28 | 47 | | new XElement(XName.Get("QueueDescription", ManagementClientConstants.ServiceBusNamespace), |
| 28 | 48 | | queueDescriptionElements.ToArray())))); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// |
| | 53 | | /// </summary> |
| | 54 | | public static QueueProperties ParseFromContent(string xml) |
| | 55 | | { |
| | 56 | | try |
| | 57 | | { |
| 48 | 58 | | var xDoc = XElement.Parse(xml); |
| 48 | 59 | | if (!xDoc.IsEmpty) |
| | 60 | | { |
| 48 | 61 | | if (xDoc.Name.LocalName == "entry") |
| | 62 | | { |
| 32 | 63 | | return ParseFromEntryElement(xDoc); |
| | 64 | | } |
| | 65 | | } |
| 16 | 66 | | } |
| 4 | 67 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 68 | | { |
| 0 | 69 | | throw new ServiceBusException(false, ex.Message); |
| | 70 | | } |
| | 71 | |
|
| 16 | 72 | | throw new ServiceBusException("Queue was not found", ServiceBusFailureReason.MessagingEntityNotFound); |
| 28 | 73 | | } |
| | 74 | |
|
| | 75 | | private static QueueProperties ParseFromEntryElement(XElement xEntry) |
| | 76 | | { |
| 36 | 77 | | var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value; |
| 36 | 78 | | var properties = new QueueProperties(name); |
| | 79 | |
|
| 36 | 80 | | var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))? |
| 36 | 81 | | .Element(XName.Get("QueueDescription", ManagementClientConstants.ServiceBusNamespace)); |
| | 82 | |
|
| 36 | 83 | | if (qdXml == null) |
| | 84 | | { |
| 4 | 85 | | throw new ServiceBusException("Queue was not found", ServiceBusFailureReason.MessagingEntityNotFound); |
| | 86 | | } |
| | 87 | |
|
| 1496 | 88 | | foreach (var element in qdXml.Elements()) |
| | 89 | | { |
| 716 | 90 | | switch (element.Name.LocalName) |
| | 91 | | { |
| | 92 | | case "MaxSizeInMegabytes": |
| 32 | 93 | | properties.MaxSizeInMegabytes = int.Parse(element.Value, CultureInfo.InvariantCulture); |
| 32 | 94 | | break; |
| | 95 | | case "RequiresDuplicateDetection": |
| 32 | 96 | | properties.RequiresDuplicateDetection = bool.Parse(element.Value); |
| 32 | 97 | | break; |
| | 98 | | case "RequiresSession": |
| 32 | 99 | | properties.RequiresSession = bool.Parse(element.Value); |
| 32 | 100 | | break; |
| | 101 | | case "DeadLetteringOnMessageExpiration": |
| 32 | 102 | | properties.DeadLetteringOnMessageExpiration = bool.Parse(element.Value); |
| 32 | 103 | | break; |
| | 104 | | case "DuplicateDetectionHistoryTimeWindow": |
| 32 | 105 | | properties.DuplicateDetectionHistoryTimeWindow = XmlConvert.ToTimeSpan(element.Value); |
| 32 | 106 | | break; |
| | 107 | | case "LockDuration": |
| 32 | 108 | | properties.LockDuration = XmlConvert.ToTimeSpan(element.Value); |
| 32 | 109 | | break; |
| | 110 | | case "DefaultMessageTimeToLive": |
| 32 | 111 | | properties.DefaultMessageTimeToLive = XmlConvert.ToTimeSpan(element.Value); |
| 32 | 112 | | break; |
| | 113 | | case "MaxDeliveryCount": |
| 32 | 114 | | properties.MaxDeliveryCount = int.Parse(element.Value, CultureInfo.InvariantCulture); |
| 32 | 115 | | break; |
| | 116 | | case "EnableBatchedOperations": |
| 32 | 117 | | properties.EnableBatchedOperations = bool.Parse(element.Value); |
| 32 | 118 | | break; |
| | 119 | | case "Status": |
| 32 | 120 | | properties.Status = element.Value; |
| 32 | 121 | | break; |
| | 122 | | case "AutoDeleteOnIdle": |
| 32 | 123 | | properties.AutoDeleteOnIdle = XmlConvert.ToTimeSpan(element.Value); |
| 32 | 124 | | break; |
| | 125 | | case "EnablePartitioning": |
| 32 | 126 | | properties.EnablePartitioning = bool.Parse(element.Value); |
| 32 | 127 | | break; |
| | 128 | | case "UserMetadata": |
| 20 | 129 | | properties.UserMetadata = element.Value; |
| 20 | 130 | | break; |
| | 131 | | case "ForwardTo": |
| 0 | 132 | | if (!string.IsNullOrWhiteSpace(element.Value)) |
| | 133 | | { |
| 0 | 134 | | properties.ForwardTo = element.Value; |
| | 135 | | } |
| 0 | 136 | | break; |
| | 137 | | case "ForwardDeadLetteredMessagesTo": |
| 0 | 138 | | if (!string.IsNullOrWhiteSpace(element.Value)) |
| | 139 | | { |
| 0 | 140 | | properties.ForwardDeadLetteredMessagesTo = element.Value; |
| | 141 | | } |
| 0 | 142 | | break; |
| | 143 | | case "AuthorizationRules": |
| 32 | 144 | | properties.AuthorizationRules = AuthorizationRules.ParseFromXElement(element); |
| 32 | 145 | | break; |
| | 146 | | case "AccessedAt": |
| | 147 | | case "CreatedAt": |
| | 148 | | case "MessageCount": |
| | 149 | | case "SizeInBytes": |
| | 150 | | case "UpdatedAt": |
| | 151 | | case "CountDetails": |
| | 152 | | // Ignore known properties |
| | 153 | | // Do nothing |
| | 154 | | break; |
| | 155 | | default: |
| | 156 | | // For unknown properties, keep them as-is for forward proof. |
| 144 | 157 | | if (properties.UnknownProperties == null) |
| | 158 | | { |
| 32 | 159 | | properties.UnknownProperties = new List<object>(); |
| | 160 | | } |
| | 161 | |
|
| 144 | 162 | | properties.UnknownProperties.Add(element); |
| | 163 | | break; |
| | 164 | | } |
| | 165 | | } |
| | 166 | |
|
| 32 | 167 | | return properties; |
| | 168 | | } |
| | 169 | |
|
| | 170 | | public static List<QueueProperties> ParseCollectionFromContent(string xml) |
| | 171 | | { |
| | 172 | | try |
| | 173 | | { |
| 4 | 174 | | var xDoc = XElement.Parse(xml); |
| 4 | 175 | | if (!xDoc.IsEmpty) |
| | 176 | | { |
| 4 | 177 | | if (xDoc.Name.LocalName == "feed") |
| | 178 | | { |
| 4 | 179 | | var queueList = new List<QueueProperties>(); |
| | 180 | |
|
| 4 | 181 | | var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace)); |
| 16 | 182 | | foreach (var entry in entryList) |
| | 183 | | { |
| 4 | 184 | | queueList.Add(ParseFromEntryElement(entry)); |
| | 185 | | } |
| | 186 | |
|
| 4 | 187 | | return queueList; |
| | 188 | | } |
| | 189 | | } |
| 0 | 190 | | } |
| 0 | 191 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 192 | | { |
| 0 | 193 | | throw new ServiceBusException(false, ex.Message); |
| | 194 | | } |
| | 195 | |
|
| 0 | 196 | | throw new ServiceBusException("No queues were found", ServiceBusFailureReason.MessagingEntityNotFound); |
| 4 | 197 | | } |
| | 198 | |
|
| | 199 | | public static void NormalizeDescription(this QueueProperties description, string baseAddress) |
| | 200 | | { |
| 28 | 201 | | if (!string.IsNullOrWhiteSpace(description.ForwardTo)) |
| | 202 | | { |
| 0 | 203 | | description.ForwardTo = NormalizeForwardToAddress(description.ForwardTo, baseAddress); |
| | 204 | | } |
| | 205 | |
|
| 28 | 206 | | if (!string.IsNullOrWhiteSpace(description.ForwardDeadLetteredMessagesTo)) |
| | 207 | | { |
| 0 | 208 | | description.ForwardDeadLetteredMessagesTo = NormalizeForwardToAddress(description.ForwardDeadLetteredMes |
| | 209 | | } |
| 28 | 210 | | } |
| | 211 | |
|
| | 212 | | private static string NormalizeForwardToAddress(string forwardTo, string baseAddress) |
| | 213 | | { |
| 0 | 214 | | baseAddress = new UriBuilder(baseAddress).Uri.ToString(); |
| | 215 | |
|
| 0 | 216 | | if (!Uri.TryCreate(forwardTo, UriKind.Absolute, out Uri forwardToUri)) |
| | 217 | | { |
| 0 | 218 | | forwardToUri = new Uri(new Uri(baseAddress), forwardTo); |
| | 219 | | } |
| | 220 | |
|
| 0 | 221 | | return forwardToUri.AbsoluteUri; |
| | 222 | | } |
| | 223 | | } |
| | 224 | | } |