| | 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.Xml.Linq; |
| | 8 | | using System.Collections.Generic; |
| | 9 | |
|
| | 10 | | internal static class SubscriptionRuntimeInfoExtensions |
| | 11 | | { |
| | 12 | | public static SubscriptionRuntimeInfo ParseFromContent(string topicName, string xml) |
| | 13 | | { |
| | 14 | | try |
| | 15 | | { |
| 0 | 16 | | var xDoc = XElement.Parse(xml); |
| 0 | 17 | | if (!xDoc.IsEmpty) |
| | 18 | | { |
| 0 | 19 | | if (xDoc.Name.LocalName == "entry") |
| | 20 | | { |
| 0 | 21 | | return ParseFromEntryElement(topicName, xDoc); |
| | 22 | | } |
| | 23 | | } |
| 0 | 24 | | } |
| 0 | 25 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 26 | | { |
| 0 | 27 | | throw new ServiceBusException(false, ex); |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | throw new MessagingEntityNotFoundException("Subscription was not found"); |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | private static SubscriptionRuntimeInfo ParseFromEntryElement(string topicName, XElement xEntry) |
| | 34 | | { |
| 0 | 35 | | var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value; |
| 0 | 36 | | var subscriptionRuntimeInfo = new SubscriptionRuntimeInfo(topicName, name); |
| | 37 | |
|
| 0 | 38 | | var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))? |
| 0 | 39 | | .Element(XName.Get("SubscriptionDescription", ManagementClientConstants.ServiceBusNamespace)); |
| | 40 | |
|
| 0 | 41 | | if (qdXml == null) |
| | 42 | | { |
| 0 | 43 | | throw new MessagingEntityNotFoundException("Subscription was not found"); |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | foreach (var element in qdXml.Elements()) |
| | 47 | | { |
| 0 | 48 | | switch (element.Name.LocalName) |
| | 49 | | { |
| | 50 | | case "AccessedAt": |
| 0 | 51 | | subscriptionRuntimeInfo.AccessedAt = DateTime.Parse(element.Value); |
| 0 | 52 | | break; |
| | 53 | | case "CreatedAt": |
| 0 | 54 | | subscriptionRuntimeInfo.CreatedAt = DateTime.Parse(element.Value); |
| 0 | 55 | | break; |
| | 56 | | case "UpdatedAt": |
| 0 | 57 | | subscriptionRuntimeInfo.UpdatedAt = DateTime.Parse(element.Value); |
| 0 | 58 | | break; |
| | 59 | | case "MessageCount": |
| 0 | 60 | | subscriptionRuntimeInfo.MessageCount = long.Parse(element.Value); |
| 0 | 61 | | break; |
| | 62 | | case "CountDetails": |
| 0 | 63 | | subscriptionRuntimeInfo.MessageCountDetails = new MessageCountDetails(); |
| 0 | 64 | | foreach (var countElement in element.Elements()) |
| | 65 | | { |
| 0 | 66 | | switch (countElement.Name.LocalName) |
| | 67 | | { |
| | 68 | | case "ActiveMessageCount": |
| 0 | 69 | | subscriptionRuntimeInfo.MessageCountDetails.ActiveMessageCount = long.Parse(countEle |
| 0 | 70 | | break; |
| | 71 | | case "DeadLetterMessageCount": |
| 0 | 72 | | subscriptionRuntimeInfo.MessageCountDetails.DeadLetterMessageCount = long.Parse(coun |
| 0 | 73 | | break; |
| | 74 | | case "ScheduledMessageCount": |
| 0 | 75 | | subscriptionRuntimeInfo.MessageCountDetails.ScheduledMessageCount = long.Parse(count |
| 0 | 76 | | break; |
| | 77 | | case "TransferMessageCount": |
| 0 | 78 | | subscriptionRuntimeInfo.MessageCountDetails.TransferMessageCount = long.Parse(countE |
| 0 | 79 | | break; |
| | 80 | | case "TransferDeadLetterMessageCount": |
| 0 | 81 | | subscriptionRuntimeInfo.MessageCountDetails.TransferDeadLetterMessageCount = long.Pa |
| | 82 | | break; |
| | 83 | | } |
| | 84 | | } |
| | 85 | | break; |
| | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| 0 | 89 | | return subscriptionRuntimeInfo; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | public static List<SubscriptionRuntimeInfo> ParseCollectionFromContent(string topicPath, string xml) |
| | 93 | | { |
| | 94 | | try |
| | 95 | | { |
| 0 | 96 | | var xDoc = XElement.Parse(xml); |
| 0 | 97 | | if (!xDoc.IsEmpty) |
| | 98 | | { |
| 0 | 99 | | if (xDoc.Name.LocalName == "feed") |
| | 100 | | { |
| 0 | 101 | | var subscriptionList = new List<SubscriptionRuntimeInfo>(); |
| | 102 | |
|
| 0 | 103 | | var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace)); |
| 0 | 104 | | foreach (var entry in entryList) |
| | 105 | | { |
| 0 | 106 | | subscriptionList.Add(ParseFromEntryElement(topicPath, entry)); |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | return subscriptionList; |
| | 110 | | } |
| | 111 | | } |
| 0 | 112 | | } |
| 0 | 113 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 114 | | { |
| 0 | 115 | | throw new ServiceBusException(false, ex); |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | throw new MessagingEntityNotFoundException("No subscriptions were found"); |
| 0 | 119 | | } |
| | 120 | | } |
| | 121 | | } |