| | 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 SubscriptionRuntimePropertiesExtensions |
| | 12 | | { |
| | 13 | | public static SubscriptionRuntimeProperties ParseFromContent(string topicName, 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(topicName, 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("Subscription was not found", ServiceBusFailureReason.MessagingEntityNotFound) |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | private static SubscriptionRuntimeProperties ParseFromEntryElement(string topicName, XElement xEntry) |
| | 35 | | { |
| 0 | 36 | | var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value; |
| 0 | 37 | | var subscriptionRuntimeInfo = new SubscriptionRuntimeProperties(topicName, name); |
| | 38 | |
|
| 0 | 39 | | var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))? |
| 0 | 40 | | .Element(XName.Get("SubscriptionDescription", ManagementClientConstants.ServiceBusNamespace)); |
| | 41 | |
|
| 0 | 42 | | if (qdXml == null) |
| | 43 | | { |
| 0 | 44 | | throw new ServiceBusException("Subscription was not found", ServiceBusFailureReason.MessagingEntityNotFo |
| | 45 | | } |
| | 46 | |
|
| 0 | 47 | | foreach (var element in qdXml.Elements()) |
| | 48 | | { |
| 0 | 49 | | switch (element.Name.LocalName) |
| | 50 | | { |
| | 51 | | case "AccessedAt": |
| 0 | 52 | | subscriptionRuntimeInfo.AccessedAt = DateTimeOffset.Parse(element.Value, CultureInfo.InvariantCu |
| 0 | 53 | | break; |
| | 54 | | case "CreatedAt": |
| 0 | 55 | | subscriptionRuntimeInfo.CreatedAt = DateTimeOffset.Parse(element.Value, CultureInfo.InvariantCul |
| 0 | 56 | | break; |
| | 57 | | case "UpdatedAt": |
| 0 | 58 | | subscriptionRuntimeInfo.UpdatedAt = DateTimeOffset.Parse(element.Value, CultureInfo.InvariantCul |
| 0 | 59 | | break; |
| | 60 | | case "MessageCount": |
| 0 | 61 | | subscriptionRuntimeInfo.TotalMessageCount = long.Parse(element.Value, CultureInfo.InvariantCultu |
| 0 | 62 | | break; |
| | 63 | | case "CountDetails": |
| 0 | 64 | | foreach (var countElement in element.Elements()) |
| | 65 | | { |
| 0 | 66 | | switch (countElement.Name.LocalName) |
| | 67 | | { |
| | 68 | | case "ActiveMessageCount": |
| 0 | 69 | | subscriptionRuntimeInfo.ActiveMessageCount = long.Parse(countElement.Value, CultureI |
| 0 | 70 | | break; |
| | 71 | | case "DeadLetterMessageCount": |
| 0 | 72 | | subscriptionRuntimeInfo.DeadLetterMessageCount = long.Parse(countElement.Value, Cult |
| 0 | 73 | | break; |
| | 74 | | case "TransferMessageCount": |
| 0 | 75 | | subscriptionRuntimeInfo.TransferMessageCount = long.Parse(countElement.Value, Cultur |
| 0 | 76 | | break; |
| | 77 | | case "TransferDeadLetterMessageCount": |
| 0 | 78 | | subscriptionRuntimeInfo.TransferDeadLetterMessageCount = long.Parse(countElement.Val |
| | 79 | | break; |
| | 80 | | } |
| | 81 | | } |
| | 82 | | break; |
| | 83 | | } |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | return subscriptionRuntimeInfo; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public static List<SubscriptionRuntimeProperties> ParseCollectionFromContent(string topicPath, string xml) |
| | 90 | | { |
| | 91 | | try |
| | 92 | | { |
| 0 | 93 | | var xDoc = XElement.Parse(xml); |
| 0 | 94 | | if (!xDoc.IsEmpty) |
| | 95 | | { |
| 0 | 96 | | if (xDoc.Name.LocalName == "feed") |
| | 97 | | { |
| 0 | 98 | | var subscriptionList = new List<SubscriptionRuntimeProperties>(); |
| | 99 | |
|
| 0 | 100 | | var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace)); |
| 0 | 101 | | foreach (var entry in entryList) |
| | 102 | | { |
| 0 | 103 | | subscriptionList.Add(ParseFromEntryElement(topicPath, entry)); |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | return subscriptionList; |
| | 107 | | } |
| | 108 | | } |
| 0 | 109 | | } |
| 0 | 110 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 111 | | { |
| 0 | 112 | | throw new ServiceBusException(false, ex.Message); |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | throw new ServiceBusException("No subscriptions were found", ServiceBusFailureReason.MessagingEntityNotFound |
| 0 | 116 | | } |
| | 117 | | } |
| | 118 | | } |