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