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