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