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