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