| | 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.Text; |
| | 8 | | using Azure.Core; |
| | 9 | | using Azure.Messaging.ServiceBus.Primitives; |
| | 10 | |
|
| | 11 | | namespace Azure.Messaging.ServiceBus.Management |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Represents the correlation rule filter expression. |
| | 15 | | /// </summary> |
| | 16 | | /// <remarks> |
| | 17 | | /// <para> |
| | 18 | | /// A CorrelationRuleFilter holds a set of conditions that are matched against one of more of an arriving message's |
| | 19 | | /// A common use is a match against the <see cref="ServiceBusMessage.CorrelationId"/> property, but the application |
| | 20 | | /// <see cref="ServiceBusMessage.ContentType"/>, <see cref="ServiceBusMessage.Label"/>, <see cref="ServiceBusMessage |
| | 21 | | /// <see cref="ServiceBusMessage.ReplyToSessionId"/>, <see cref="ServiceBusMessage.SessionId"/>, <see cref="ServiceB |
| | 22 | | /// A match exists when an arriving message's value for a property is equal to the value specified in the correlatio |
| | 23 | | /// the comparison is case-sensitive. When specifying multiple match properties, the filter combines them as a logic |
| | 24 | | /// meaning all conditions must match for the filter to match. |
| | 25 | | /// </para> |
| | 26 | | /// <para> |
| | 27 | | /// The CorrelationRuleFilter provides an efficient shortcut for declarations of filters that deal only with correla |
| | 28 | | /// In this case the cost of the lexigraphical analysis of the expression can be avoided. |
| | 29 | | /// Not only will correlation filters be optimized at declaration time, but they will also be optimized at runtime. |
| | 30 | | /// Correlation filter matching can be reduced to a hashtable lookup, which aggregates the complexity of the set of |
| | 31 | | /// </para> |
| | 32 | | /// </remarks> |
| | 33 | | public sealed class CorrelationRuleFilter : RuleFilter |
| | 34 | | { |
| | 35 | | /// <summary> |
| | 36 | | /// Initializes a new instance of the <see cref="CorrelationRuleFilter" /> class with default values. |
| | 37 | | /// </summary> |
| 36 | 38 | | public CorrelationRuleFilter() |
| | 39 | | { |
| 36 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Initializes a new instance of the <see cref="CorrelationRuleFilter" /> class with the specified correlation |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="correlationId">The identifier for the correlation.</param> |
| | 46 | | /// <exception cref="System.ArgumentException">Thrown when the <paramref name="correlationId" /> is null or empt |
| | 47 | | public CorrelationRuleFilter(string correlationId) |
| 8 | 48 | | : this() |
| | 49 | | { |
| 8 | 50 | | Argument.AssertNotNullOrWhiteSpace(correlationId, nameof(correlationId)); |
| 8 | 51 | | CorrelationId = correlationId; |
| 8 | 52 | | } |
| | 53 | |
|
| | 54 | | internal override RuleFilter Clone() => |
| 8 | 55 | | new CorrelationRuleFilter(CorrelationId) |
| 8 | 56 | | { |
| 8 | 57 | | MessageId = MessageId, |
| 8 | 58 | | To = To, |
| 8 | 59 | | ReplyTo = ReplyTo, |
| 8 | 60 | | Label = Label, |
| 8 | 61 | | SessionId = SessionId, |
| 8 | 62 | | ReplyToSessionId = ReplyToSessionId, |
| 8 | 63 | | ContentType = ContentType, |
| 8 | 64 | | Properties = (Properties as PropertyDictionary).Clone() |
| 8 | 65 | | }; |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Identifier of the correlation. |
| | 69 | | /// </summary> |
| | 70 | | /// <value>The identifier of the correlation.</value> |
| | 71 | | public string CorrelationId |
| | 72 | | { |
| 60 | 73 | | get; |
| 32 | 74 | | set; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Identifier of the message. |
| | 79 | | /// </summary> |
| | 80 | | /// <value>The identifier of the message.</value> |
| | 81 | | /// <remarks>Max MessageId size is 128 chars.</remarks> |
| | 82 | | public string MessageId |
| | 83 | | { |
| 60 | 84 | | get; |
| 28 | 85 | | set; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Address to send to. |
| | 90 | | /// </summary> |
| | 91 | | /// <value>The address to send to.</value> |
| | 92 | | public string To |
| | 93 | | { |
| 60 | 94 | | get; |
| 28 | 95 | | set; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Address of the queue to reply to. |
| | 100 | | /// </summary> |
| | 101 | | /// <value>The address of the queue to reply to.</value> |
| | 102 | | public string ReplyTo |
| | 103 | | { |
| 60 | 104 | | get; |
| 28 | 105 | | set; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// Application specific label. |
| | 110 | | /// </summary> |
| | 111 | | /// <value>The application specific label.</value> |
| | 112 | | public string Label |
| | 113 | | { |
| 60 | 114 | | get; |
| 28 | 115 | | set; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Session identifier. |
| | 120 | | /// </summary> |
| | 121 | | /// <value>The session identifier.</value> |
| | 122 | | /// <remarks>Max size of sessionId is 128 chars.</remarks> |
| | 123 | | public string SessionId |
| | 124 | | { |
| 60 | 125 | | get; |
| 28 | 126 | | set; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Session identifier to reply to. |
| | 131 | | /// </summary> |
| | 132 | | /// <value>The session identifier to reply to.</value> |
| | 133 | | /// <remarks>Max size of ReplyToSessionId is 128.</remarks> |
| | 134 | | public string ReplyToSessionId |
| | 135 | | { |
| 60 | 136 | | get; |
| 28 | 137 | | set; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Content type of the message. |
| | 142 | | /// </summary> |
| | 143 | | /// <value>The content type of the message.</value> |
| | 144 | | public string ContentType |
| | 145 | | { |
| 60 | 146 | | get; |
| 28 | 147 | | set; |
| | 148 | | } |
| | 149 | |
|
| | 150 | | /// <summary> |
| | 151 | | /// Application specific properties of the message. |
| | 152 | | /// </summary> |
| | 153 | | /// <value>The application specific properties of the message.</value> |
| | 154 | | /// <remarks> |
| | 155 | | /// Only following value types are supported: |
| | 156 | | /// byte, sbyte, char, short, ushort, int, uint, long, ulong, float, double, decimal, |
| | 157 | | /// bool, Guid, string, Uri, DateTime, DateTimeOffset, TimeSpan, Stream, byte[], |
| | 158 | | /// and IList / IDictionary of supported types |
| | 159 | | /// </remarks> |
| 184 | 160 | | public IDictionary<string, object> Properties { get; internal set; } = new PropertyDictionary(); |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// Converts the value of the current instance to its equivalent string representation. |
| | 164 | | /// </summary> |
| | 165 | | /// <returns>A string representation of the current instance.</returns> |
| | 166 | | public override string ToString() |
| | 167 | | { |
| 0 | 168 | | var stringBuilder = new StringBuilder(); |
| | 169 | |
|
| 0 | 170 | | stringBuilder.Append("CorrelationRuleFilter: "); |
| | 171 | |
|
| 0 | 172 | | var isFirstExpression = true; |
| | 173 | |
|
| 0 | 174 | | AppendPropertyExpression(ref isFirstExpression, stringBuilder, "sys.CorrelationId", CorrelationId); |
| 0 | 175 | | AppendPropertyExpression(ref isFirstExpression, stringBuilder, "sys.MessageId", MessageId); |
| 0 | 176 | | AppendPropertyExpression(ref isFirstExpression, stringBuilder, "sys.To", To); |
| 0 | 177 | | AppendPropertyExpression(ref isFirstExpression, stringBuilder, "sys.ReplyTo", ReplyTo); |
| 0 | 178 | | AppendPropertyExpression(ref isFirstExpression, stringBuilder, "sys.Label", Label); |
| 0 | 179 | | AppendPropertyExpression(ref isFirstExpression, stringBuilder, "sys.SessionId", SessionId); |
| 0 | 180 | | AppendPropertyExpression(ref isFirstExpression, stringBuilder, "sys.ReplyToSessionId", ReplyToSessionId); |
| 0 | 181 | | AppendPropertyExpression(ref isFirstExpression, stringBuilder, "sys.ContentType", ContentType); |
| | 182 | |
|
| 0 | 183 | | foreach (var pair in Properties) |
| | 184 | | { |
| 0 | 185 | | string propertyName = pair.Key; |
| 0 | 186 | | object propertyValue = pair.Value; |
| | 187 | |
|
| 0 | 188 | | AppendPropertyExpression(ref isFirstExpression, stringBuilder, propertyName, propertyValue); |
| | 189 | | } |
| | 190 | |
|
| 0 | 191 | | return stringBuilder.ToString(); |
| | 192 | | } |
| | 193 | |
|
| | 194 | | private static void AppendPropertyExpression(ref bool firstExpression, StringBuilder builder, string propertyNam |
| | 195 | | { |
| 0 | 196 | | if (value != null) |
| | 197 | | { |
| 0 | 198 | | if (firstExpression) |
| | 199 | | { |
| 0 | 200 | | firstExpression = false; |
| | 201 | | } |
| | 202 | | else |
| | 203 | | { |
| 0 | 204 | | builder.Append(" AND "); |
| | 205 | | } |
| | 206 | |
|
| 0 | 207 | | builder.AppendFormat(CultureInfo.InvariantCulture, "{0} = '{1}'", propertyName, value); |
| | 208 | | } |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | /// <inheritdoc/> |
| | 212 | | public override int GetHashCode() |
| | 213 | | { |
| 0 | 214 | | int hash = 13; |
| | 215 | | unchecked |
| | 216 | | { |
| 0 | 217 | | hash = (hash * 7) + CorrelationId?.GetHashCode() ?? 0; |
| 0 | 218 | | hash = (hash * 7) + MessageId?.GetHashCode() ?? 0; |
| 0 | 219 | | hash = (hash * 7) + SessionId?.GetHashCode() ?? 0; |
| | 220 | | } |
| | 221 | |
|
| 0 | 222 | | return hash; |
| | 223 | | } |
| | 224 | |
|
| | 225 | | /// <inheritdoc/> |
| | 226 | | public override bool Equals(object obj) |
| | 227 | | { |
| 0 | 228 | | var other = obj as CorrelationRuleFilter; |
| 0 | 229 | | return Equals(other); |
| | 230 | | } |
| | 231 | |
|
| | 232 | | /// <inheritdoc/> |
| | 233 | | public override bool Equals(RuleFilter other) |
| | 234 | | { |
| 16 | 235 | | if (other is CorrelationRuleFilter correlationRuleFilter) |
| | 236 | | { |
| 16 | 237 | | if (string.Equals(CorrelationId, correlationRuleFilter.CorrelationId, StringComparison.OrdinalIgnoreCase |
| 16 | 238 | | && string.Equals(MessageId, correlationRuleFilter.MessageId, StringComparison.OrdinalIgnoreCase) |
| 16 | 239 | | && string.Equals(To, correlationRuleFilter.To, StringComparison.OrdinalIgnoreCase) |
| 16 | 240 | | && string.Equals(ReplyTo, correlationRuleFilter.ReplyTo, StringComparison.OrdinalIgnoreCase) |
| 16 | 241 | | && string.Equals(Label, correlationRuleFilter.Label, StringComparison.OrdinalIgnoreCase) |
| 16 | 242 | | && string.Equals(SessionId, correlationRuleFilter.SessionId, StringComparison.OrdinalIgnoreCase) |
| 16 | 243 | | && string.Equals(ReplyToSessionId, correlationRuleFilter.ReplyToSessionId, StringComparison.OrdinalI |
| 16 | 244 | | && string.Equals(ContentType, correlationRuleFilter.ContentType, StringComparison.OrdinalIgnoreCase) |
| | 245 | | { |
| 16 | 246 | | if (Properties.Count != correlationRuleFilter.Properties.Count) |
| | 247 | | { |
| 0 | 248 | | return false; |
| | 249 | | } |
| | 250 | |
|
| 80 | 251 | | foreach (var param in Properties) |
| | 252 | | { |
| 24 | 253 | | if (!correlationRuleFilter.Properties.TryGetValue(param.Key, out var otherParamValue) || |
| 24 | 254 | | (param.Value == null ^ otherParamValue == null) || |
| 24 | 255 | | (param.Value != null && !param.Value.Equals(otherParamValue))) |
| | 256 | | { |
| 0 | 257 | | return false; |
| | 258 | | } |
| | 259 | | } |
| | 260 | |
|
| 16 | 261 | | return true; |
| | 262 | | } |
| | 263 | | } |
| | 264 | |
|
| 0 | 265 | | return false; |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | /// <summary> |
| | 269 | | /// |
| | 270 | | /// </summary> |
| | 271 | | /// <param name="left"></param> |
| | 272 | | /// <param name="right"></param> |
| | 273 | | /// <returns></returns> |
| | 274 | | public static bool operator ==(CorrelationRuleFilter left, CorrelationRuleFilter right) |
| | 275 | | { |
| 0 | 276 | | if (ReferenceEquals(left, right)) |
| | 277 | | { |
| 0 | 278 | | return true; |
| | 279 | | } |
| | 280 | |
|
| 0 | 281 | | if (ReferenceEquals(left, null) || ReferenceEquals(right, null)) |
| | 282 | | { |
| 0 | 283 | | return false; |
| | 284 | | } |
| | 285 | |
|
| 0 | 286 | | return left.Equals(right); |
| | 287 | | } |
| | 288 | |
|
| | 289 | | /// <summary> |
| | 290 | | /// |
| | 291 | | /// </summary> |
| | 292 | | /// <param name="left"></param> |
| | 293 | | /// <param name="right"></param> |
| | 294 | | /// <returns></returns> |
| | 295 | | public static bool operator !=(CorrelationRuleFilter left, CorrelationRuleFilter right) |
| | 296 | | { |
| 0 | 297 | | return !(left == right); |
| | 298 | | } |
| | 299 | | } |
| | 300 | | } |