| | 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; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Xml; |
| | 9 | | using System.Xml.Linq; |
| | 10 | |
|
| | 11 | | internal static class TopicDescriptionExtensions |
| | 12 | | { |
| | 13 | | public static TopicDescription ParseFromContent(string xml) |
| | 14 | | { |
| | 15 | | try |
| | 16 | | { |
| 0 | 17 | | var xDoc = XElement.Parse(xml); |
| 0 | 18 | | if (!xDoc.IsEmpty) |
| | 19 | | { |
| 0 | 20 | | if (xDoc.Name.LocalName == "entry") |
| | 21 | | { |
| 0 | 22 | | return ParseFromEntryElement(xDoc); |
| | 23 | | } |
| | 24 | | } |
| 0 | 25 | | } |
| 0 | 26 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 27 | | { |
| 0 | 28 | | throw new ServiceBusException(false, ex); |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | throw new MessagingEntityNotFoundException("Topic was not found"); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public static IList<TopicDescription> ParseCollectionFromContent(string xml) |
| | 35 | | { |
| | 36 | | try |
| | 37 | | { |
| 0 | 38 | | var xDoc = XElement.Parse(xml); |
| 0 | 39 | | if (!xDoc.IsEmpty) |
| | 40 | | { |
| 0 | 41 | | if (xDoc.Name.LocalName == "feed") |
| | 42 | | { |
| 0 | 43 | | var topicList = new List<TopicDescription>(); |
| | 44 | |
|
| 0 | 45 | | var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace)); |
| 0 | 46 | | foreach (var entry in entryList) |
| | 47 | | { |
| 0 | 48 | | topicList.Add(ParseFromEntryElement(entry)); |
| | 49 | | } |
| | 50 | |
|
| 0 | 51 | | return topicList; |
| | 52 | | } |
| | 53 | | } |
| 0 | 54 | | } |
| 0 | 55 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 56 | | { |
| 0 | 57 | | throw new ServiceBusException(false, ex); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | throw new MessagingEntityNotFoundException("No topics were found"); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | private static TopicDescription ParseFromEntryElement(XElement xEntry) |
| | 64 | | { |
| 0 | 65 | | var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value; |
| 0 | 66 | | var topicDesc = new TopicDescription(name); |
| | 67 | |
|
| 0 | 68 | | var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))? |
| 0 | 69 | | .Element(XName.Get("TopicDescription", ManagementClientConstants.ServiceBusNamespace)); |
| | 70 | |
|
| 0 | 71 | | if (qdXml == null) |
| | 72 | | { |
| 0 | 73 | | throw new MessagingEntityNotFoundException("Topic was not found"); |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | foreach (var element in qdXml.Elements()) |
| | 77 | | { |
| 0 | 78 | | switch (element.Name.LocalName) |
| | 79 | | { |
| | 80 | | case "MaxSizeInMegabytes": |
| 0 | 81 | | topicDesc.MaxSizeInMB = long.Parse(element.Value); |
| 0 | 82 | | break; |
| | 83 | | case "RequiresDuplicateDetection": |
| 0 | 84 | | topicDesc.RequiresDuplicateDetection = bool.Parse(element.Value); |
| 0 | 85 | | break; |
| | 86 | | case "DuplicateDetectionHistoryTimeWindow": |
| 0 | 87 | | topicDesc.duplicateDetectionHistoryTimeWindow = XmlConvert.ToTimeSpan(element.Value); |
| 0 | 88 | | break; |
| | 89 | | case "DefaultMessageTimeToLive": |
| 0 | 90 | | topicDesc.DefaultMessageTimeToLive = XmlConvert.ToTimeSpan(element.Value); |
| 0 | 91 | | break; |
| | 92 | | case "EnableBatchedOperations": |
| 0 | 93 | | topicDesc.EnableBatchedOperations = bool.Parse(element.Value); |
| 0 | 94 | | break; |
| | 95 | | case "Status": |
| 0 | 96 | | topicDesc.Status = (EntityStatus)Enum.Parse(typeof(EntityStatus), element.Value); |
| 0 | 97 | | break; |
| | 98 | | case "UserMetadata": |
| 0 | 99 | | topicDesc.UserMetadata = element.Value; |
| 0 | 100 | | break; |
| | 101 | | case "AutoDeleteOnIdle": |
| 0 | 102 | | topicDesc.AutoDeleteOnIdle = XmlConvert.ToTimeSpan(element.Value); |
| 0 | 103 | | break; |
| | 104 | | case "EnablePartitioning": |
| 0 | 105 | | topicDesc.EnablePartitioning = bool.Parse(element.Value); |
| 0 | 106 | | break; |
| | 107 | | case "SupportOrdering": |
| 0 | 108 | | topicDesc.SupportOrdering = bool.Parse(element.Value); |
| 0 | 109 | | break; |
| | 110 | | case "AuthorizationRules": |
| 0 | 111 | | topicDesc.AuthorizationRules = AuthorizationRules.ParseFromXElement(element); |
| 0 | 112 | | break; |
| | 113 | | case "AccessedAt": |
| | 114 | | case "CreatedAt": |
| | 115 | | case "MessageCount": |
| | 116 | | case "SizeInBytes": |
| | 117 | | case "UpdatedAt": |
| | 118 | | case "CountDetails": |
| | 119 | | case "SubscriptionCount": |
| | 120 | | // Ignore known properties |
| | 121 | | // Do nothing |
| | 122 | | break; |
| | 123 | | default: |
| | 124 | | // For unknown properties, keep them as-is for forward proof. |
| 0 | 125 | | if (topicDesc.UnknownProperties == null) |
| | 126 | | { |
| 0 | 127 | | topicDesc.UnknownProperties = new List<object>(); |
| | 128 | | } |
| | 129 | |
|
| 0 | 130 | | topicDesc.UnknownProperties.Add(element); |
| | 131 | | break; |
| | 132 | | } |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | return topicDesc; |
| | 136 | | } |
| | 137 | |
|
| | 138 | | public static XDocument Serialize(this TopicDescription description) |
| | 139 | | { |
| 0 | 140 | | var topicDescriptionElements = new List<object> |
| 0 | 141 | | { |
| 0 | 142 | | description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeTo |
| 0 | 143 | | new XElement(XName.Get("MaxSizeInMegabytes", ManagementClientConstants.ServiceBusNamespace), XmlConvert. |
| 0 | 144 | | new XElement(XName.Get("RequiresDuplicateDetection", ManagementClientConstants.ServiceBusNamespace), Xml |
| 0 | 145 | | description.RequiresDuplicateDetection && description.DuplicateDetectionHistoryTimeWindow != default ? |
| 0 | 146 | | new XElement(XName.Get("DuplicateDetectionHistoryTimeWindow", ManagementClientConstants.ServiceBusNa |
| 0 | 147 | | : null, |
| 0 | 148 | | new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlCon |
| 0 | 149 | | description.AuthorizationRules?.Serialize(), |
| 0 | 150 | | new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToSt |
| 0 | 151 | | description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.Serv |
| 0 | 152 | | new XElement(XName.Get("SupportOrdering", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToS |
| 0 | 153 | | description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", Managemen |
| 0 | 154 | | new XElement(XName.Get("EnablePartitioning", ManagementClientConstants.ServiceBusNamespace), XmlConvert. |
| 0 | 155 | | }; |
| | 156 | |
|
| 0 | 157 | | if (description.UnknownProperties != null) |
| | 158 | | { |
| 0 | 159 | | topicDescriptionElements.AddRange(description.UnknownProperties); |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | XDocument doc = new XDocument( |
| 0 | 163 | | new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace), |
| 0 | 164 | | new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace), |
| 0 | 165 | | new XAttribute("type", "application/xml"), |
| 0 | 166 | | new XElement(XName.Get("TopicDescription", ManagementClientConstants.ServiceBusNamespace), |
| 0 | 167 | | topicDescriptionElements |
| 0 | 168 | | )) |
| 0 | 169 | | )); |
| | 170 | |
|
| 0 | 171 | | return doc; |
| | 172 | | } |
| | 173 | | } |
| | 174 | | } |