| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Xml.Linq; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Globalization; |
| | 8 | |
|
| | 9 | | namespace Azure.Messaging.ServiceBus.Management |
| | 10 | | { |
| | 11 | | internal static class TopicRuntimePropertiesExtensions |
| | 12 | | { |
| | 13 | | public static TopicRuntimeProperties ParseFromContent(string xml) |
| | 14 | | { |
| | 15 | | try |
| | 16 | | { |
| 4 | 17 | | var xDoc = XElement.Parse(xml); |
| 4 | 18 | | if (!xDoc.IsEmpty) |
| | 19 | | { |
| 4 | 20 | | if (xDoc.Name.LocalName == "entry") |
| | 21 | | { |
| 4 | 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.Message); |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | throw new ServiceBusException("Topic was not found", ServiceBusFailureReason.MessagingEntityNotFound); |
| 4 | 32 | | } |
| | 33 | |
|
| | 34 | | public static TopicRuntimeProperties ParseFromEntryElement(XElement xEntry) |
| | 35 | | { |
| 8 | 36 | | var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value; |
| 8 | 37 | | var topicRuntimeInfo = new TopicRuntimeProperties(name); |
| | 38 | |
|
| 8 | 39 | | var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))? |
| 8 | 40 | | .Element(XName.Get("TopicDescription", ManagementClientConstants.ServiceBusNamespace)); |
| | 41 | |
|
| 8 | 42 | | if (qdXml == null) |
| | 43 | | { |
| 0 | 44 | | throw new ServiceBusException("Topic was not found", ServiceBusFailureReason.MessagingEntityNotFound); |
| | 45 | | } |
| | 46 | |
|
| 384 | 47 | | foreach (var element in qdXml.Elements()) |
| | 48 | | { |
| 184 | 49 | | switch (element.Name.LocalName) |
| | 50 | | { |
| | 51 | | case "AccessedAt": |
| 8 | 52 | | topicRuntimeInfo.AccessedAt = DateTimeOffset.Parse(element.Value, CultureInfo.InvariantCulture); |
| 8 | 53 | | break; |
| | 54 | | case "CreatedAt": |
| 8 | 55 | | topicRuntimeInfo.CreatedAt = DateTimeOffset.Parse(element.Value, CultureInfo.InvariantCulture); |
| 8 | 56 | | break; |
| | 57 | | case "SizeInBytes": |
| 8 | 58 | | topicRuntimeInfo.SizeInBytes = long.Parse(element.Value, CultureInfo.InvariantCulture); |
| 8 | 59 | | break; |
| | 60 | | case "SubscriptionCount": |
| 8 | 61 | | topicRuntimeInfo.SubscriptionCount = int.Parse(element.Value, CultureInfo.InvariantCulture); |
| 8 | 62 | | break; |
| | 63 | | case "UpdatedAt": |
| 8 | 64 | | topicRuntimeInfo.UpdatedAt = DateTimeOffset.Parse(element.Value, CultureInfo.InvariantCulture); |
| 8 | 65 | | break; |
| | 66 | | case "CountDetails": |
| 96 | 67 | | foreach (var countElement in element.Elements()) |
| | 68 | | { |
| 40 | 69 | | switch (countElement.Name.LocalName) |
| | 70 | | { |
| | 71 | | case "ScheduledMessageCount": |
| 8 | 72 | | topicRuntimeInfo.ScheduledMessageCount = long.Parse(countElement.Value, CultureInfo. |
| | 73 | | break; |
| | 74 | | } |
| | 75 | | } |
| | 76 | |
|
| | 77 | | break; |
| | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| 8 | 81 | | return topicRuntimeInfo; |
| | 82 | | } |
| | 83 | |
|
| | 84 | | public static List<TopicRuntimeProperties> ParseCollectionFromContent(string xml) |
| | 85 | | { |
| | 86 | | try |
| | 87 | | { |
| 4 | 88 | | var xDoc = XElement.Parse(xml); |
| 4 | 89 | | if (!xDoc.IsEmpty) |
| | 90 | | { |
| 4 | 91 | | if (xDoc.Name.LocalName == "feed") |
| | 92 | | { |
| 4 | 93 | | var topicList = new List<TopicRuntimeProperties>(); |
| | 94 | |
|
| 4 | 95 | | var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace)); |
| 16 | 96 | | foreach (var entry in entryList) |
| | 97 | | { |
| 4 | 98 | | topicList.Add(ParseFromEntryElement(entry)); |
| | 99 | | } |
| | 100 | |
|
| 4 | 101 | | return topicList; |
| | 102 | | } |
| | 103 | | } |
| 0 | 104 | | } |
| 0 | 105 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 106 | | { |
| 0 | 107 | | throw new ServiceBusException(false, ex.Message); |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | throw new ServiceBusException("No topics were found", ServiceBusFailureReason.MessagingEntityNotFound); |
| 4 | 111 | | } |
| | 112 | | } |
| | 113 | | } |