| | | 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.Xml.Linq; |
| | | 8 | | using Microsoft.Azure.ServiceBus.Filters; |
| | | 9 | | using Microsoft.Azure.ServiceBus.Management; |
| | | 10 | | |
| | | 11 | | internal static class RuleActionExtensions |
| | | 12 | | { |
| | | 13 | | internal static RuleAction ParseFromXElement(XElement xElement) |
| | | 14 | | { |
| | 0 | 15 | | var attribute = xElement.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace)); |
| | 0 | 16 | | if (attribute == null) |
| | | 17 | | { |
| | 0 | 18 | | return null; |
| | | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | switch (attribute.Value) |
| | | 22 | | { |
| | | 23 | | case "SqlRuleAction": |
| | 0 | 24 | | return ParseFromXElementSqlRuleAction(xElement); |
| | | 25 | | |
| | | 26 | | case "EmptyRuleAction": |
| | 0 | 27 | | return null; |
| | | 28 | | |
| | | 29 | | default: |
| | 0 | 30 | | MessagingEventSource.Log.ManagementSerializationException( |
| | 0 | 31 | | $"{nameof(RuleActionExtensions)}_{nameof(ParseFromXElement)}", |
| | 0 | 32 | | xElement.ToString()); |
| | 0 | 33 | | return null; |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | |
| | | 38 | | static RuleAction ParseFromXElementSqlRuleAction(XElement xElement) |
| | | 39 | | { |
| | 0 | 40 | | var expression = xElement.Element(XName.Get("SqlExpression", ManagementClientConstants.ServiceBusNamespace)) |
| | 0 | 41 | | if (string.IsNullOrWhiteSpace(expression)) |
| | | 42 | | { |
| | 0 | 43 | | return null; |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | var action = new SqlRuleAction(expression); |
| | | 47 | | |
| | 0 | 48 | | var parameters = xElement.Element(XName.Get("Parameters", ManagementClientConstants.ServiceBusNamespace)); |
| | 0 | 49 | | if (parameters != null && parameters.HasElements) |
| | | 50 | | { |
| | 0 | 51 | | foreach (var param in parameters.Elements(XName.Get("KeyValueOfstringanyType", ManagementClientConstants |
| | | 52 | | { |
| | 0 | 53 | | var key = param.Element(XName.Get("Key", ManagementClientConstants.ServiceBusNamespace))?.Value; |
| | 0 | 54 | | var value = XmlObjectConvertor.ParseValueObject(param.Element(XName.Get("Value", ManagementClientCon |
| | 0 | 55 | | action.Parameters.Add(key, value); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | return action; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public static XElement Serialize(this RuleAction action) |
| | | 63 | | { |
| | 0 | 64 | | if (action is SqlRuleAction sqlRuleAction) |
| | | 65 | | { |
| | 0 | 66 | | XElement parameterElement = null; |
| | 0 | 67 | | if (sqlRuleAction.parameters != null) |
| | | 68 | | { |
| | 0 | 69 | | parameterElement = new XElement(XName.Get("Parameters", ManagementClientConstants.ServiceBusNamespac |
| | 0 | 70 | | foreach (var param in sqlRuleAction.Parameters) |
| | | 71 | | { |
| | 0 | 72 | | parameterElement.Add( |
| | 0 | 73 | | new XElement(XName.Get("KeyValueOfstringanyType", ManagementClientConstants.ServiceBusNamesp |
| | 0 | 74 | | new XElement(XName.Get("Key", ManagementClientConstants.ServiceBusNamespace), param.Key) |
| | 0 | 75 | | XmlObjectConvertor.SerializeObject(param.Value))); |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | return new XElement( |
| | 0 | 80 | | XName.Get("Action", ManagementClientConstants.ServiceBusNamespace), |
| | 0 | 81 | | new XAttribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace), nameof(S |
| | 0 | 82 | | new XElement(XName.Get("SqlExpression", ManagementClientConstants.ServiceBusNamespace), sqlRuleA |
| | 0 | 83 | | parameterElement); |
| | | 84 | | } |
| | | 85 | | else |
| | | 86 | | { |
| | 0 | 87 | | throw new NotImplementedException($"Rule action of type {action.GetType().Name} is not implemented"); |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |