| | 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.Linq; |
| | 8 | | using Azure.Core; |
| | 9 | | using Azure.Messaging.ServiceBus.Primitives; |
| | 10 | |
|
| | 11 | | namespace Azure.Messaging.ServiceBus.Management |
| | 12 | | { |
| | 13 | | internal static class RuleDescriptionExtensions |
| | 14 | | { |
| | 15 | | public static void ValidateDescriptionName(this RuleProperties description) |
| | 16 | | { |
| 0 | 17 | | Argument.AssertNotNullOrWhiteSpace(description.Name, nameof(description.Name)); |
| 0 | 18 | | Argument.AssertNotTooLong(description.Name, Constants.RuleNameMaximumLength, nameof(description.Name)); |
| | 19 | |
|
| 0 | 20 | | if (description.Name.Contains(Constants.PathDelimiter) || description.Name.Contains(@"\")) |
| | 21 | | { |
| | 22 | | #pragma warning disable CA2208 // Instantiate argument exceptions correctly. Specifying the Name property |
| | 23 | | // is more intuitive, than just description. |
| 0 | 24 | | throw new ArgumentException( |
| 0 | 25 | | Resources.InvalidCharacterInEntityName.FormatForUser(Constants.PathDelimiter, description.Name), |
| 0 | 26 | | nameof(description.Name)); |
| | 27 | | #pragma warning restore CA2208 // Instantiate argument exceptions correctly |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | string[] uriSchemeKeys = { "@", "?", "#" }; |
| 0 | 31 | | foreach (var uriSchemeKey in uriSchemeKeys) |
| | 32 | | { |
| 0 | 33 | | if (description.Name.Contains(uriSchemeKey)) |
| | 34 | | { |
| | 35 | | #pragma warning disable CA2208 // Instantiate argument exceptions correctly |
| 0 | 36 | | throw new ArgumentException( |
| 0 | 37 | | Resources.CharacterReservedForUriScheme.FormatForUser(nameof(description.Name), uriSchemeKey), |
| 0 | 38 | | nameof(description.Name)); |
| | 39 | | #pragma warning restore CA2208 // Instantiate argument exceptions correctly |
| | 40 | | } |
| | 41 | | } |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public static RuleProperties ParseFromContent(string xml) |
| | 45 | | { |
| | 46 | | try |
| | 47 | | { |
| 36 | 48 | | var xDoc = XElement.Parse(xml); |
| 36 | 49 | | if (!xDoc.IsEmpty) |
| | 50 | | { |
| 36 | 51 | | if (xDoc.Name.LocalName == "entry") |
| | 52 | | { |
| 36 | 53 | | return ParseFromEntryElement(xDoc); |
| | 54 | | } |
| | 55 | | } |
| 0 | 56 | | } |
| 0 | 57 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 58 | | { |
| | 59 | | //throw new ServiceBusException(false, ex); |
| 0 | 60 | | } |
| 0 | 61 | | return null; |
| | 62 | | //throw new MessagingEntityNotFoundException("Rule was not found"); |
| 36 | 63 | | } |
| | 64 | |
|
| | 65 | | public static List<RuleProperties> ParseCollectionFromContent(string xml) |
| | 66 | | { |
| | 67 | | try |
| | 68 | | { |
| 4 | 69 | | var xDoc = XElement.Parse(xml); |
| 4 | 70 | | if (!xDoc.IsEmpty) |
| | 71 | | { |
| 4 | 72 | | if (xDoc.Name.LocalName == "feed") |
| | 73 | | { |
| 4 | 74 | | var rules = new List<RuleProperties>(); |
| | 75 | |
|
| 4 | 76 | | var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace)); |
| 32 | 77 | | foreach (var entry in entryList) |
| | 78 | | { |
| 12 | 79 | | rules.Add(ParseFromEntryElement(entry)); |
| | 80 | | } |
| | 81 | |
|
| 4 | 82 | | return rules; |
| | 83 | | } |
| | 84 | | } |
| 0 | 85 | | } |
| 0 | 86 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 87 | | { |
| | 88 | | //throw new ServiceBusException(false, ex); |
| 0 | 89 | | } |
| 0 | 90 | | return null; |
| | 91 | | //throw new MessagingEntityNotFoundException("Rule was not found"); |
| 4 | 92 | | } |
| | 93 | |
|
| | 94 | | private static RuleProperties ParseFromEntryElement(XElement xEntry) |
| | 95 | | { |
| 48 | 96 | | var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value; |
| 48 | 97 | | var ruleDescription = new RuleProperties(); |
| | 98 | |
|
| 48 | 99 | | var rdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))? |
| 48 | 100 | | .Element(XName.Get("RuleDescription", ManagementClientConstants.ServiceBusNamespace)); |
| | 101 | |
|
| 48 | 102 | | if (rdXml == null) |
| | 103 | | { |
| | 104 | | //throw new MessagingEntityNotFoundException("Rule was not found"); |
| | 105 | | } |
| | 106 | |
|
| 480 | 107 | | foreach (var element in rdXml.Elements()) |
| | 108 | | { |
| 192 | 109 | | switch (element.Name.LocalName) |
| | 110 | | { |
| | 111 | | case "Name": |
| 48 | 112 | | ruleDescription.Name = element.Value; |
| 48 | 113 | | break; |
| | 114 | | case "Filter": |
| 48 | 115 | | ruleDescription.Filter = RuleFilterExtensions.ParseFromXElement(element); |
| 48 | 116 | | break; |
| | 117 | | case "Action": |
| 48 | 118 | | ruleDescription.Action = RuleActionExtensions.ParseFromXElement(element); |
| 48 | 119 | | break; |
| | 120 | | case "CreatedAt": |
| 48 | 121 | | ruleDescription.CreatedAt = DateTime.Parse(element.Value, CultureInfo.InvariantCulture); |
| | 122 | | break; |
| | 123 | | } |
| | 124 | | } |
| | 125 | |
|
| 48 | 126 | | return ruleDescription; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | public static XDocument Serialize(this RuleProperties description) |
| | 130 | | { |
| 20 | 131 | | XDocument doc = new XDocument( |
| 20 | 132 | | new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace), |
| 20 | 133 | | new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace), |
| 20 | 134 | | new XAttribute("type", "application/xml"), |
| 20 | 135 | | description.SerializeRule()))); |
| | 136 | |
|
| 20 | 137 | | return doc; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | public static XElement SerializeRule(this RuleProperties description, string elementName = "RuleDescription") |
| | 141 | | { |
| 48 | 142 | | return new XElement( |
| 48 | 143 | | XName.Get(elementName, ManagementClientConstants.ServiceBusNamespace), |
| 48 | 144 | | description.Filter?.Serialize(), |
| 48 | 145 | | description.Action?.Serialize(), |
| 48 | 146 | | new XElement(XName.Get("Name", ManagementClientConstants.ServiceBusNamespace), description.Name)); |
| | 147 | | } |
| | 148 | | } |
| | 149 | | } |