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