-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathFilter.cs
More file actions
416 lines (376 loc) · 14.3 KB
/
Filter.cs
File metadata and controls
416 lines (376 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.SCIM
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
public sealed class Filter : IFilter
{
private const string ComparisonValueTemplate = "\"{0}\"";
private const string EncodingSpacePer2396 = "+";
public const string NullValue = "null";
private const string ReservedPerRfc2396 = ";/?:@&=+$,";
private const string ReservedPerRfc3986 = Filter.ReservedPerRfc2396 + "#[]!'()*";
private const string Space = " ";
private const string Template = "filter={0}";
private const string TemplateComparison = "{0} {1} {2}";
private const string TemplateConjunction = "{0} {1} {2}";
private const string TemplateNesting = "({0})";
private static readonly Lazy<char[]> ReservedCharactersPerRfc3986 =
new Lazy<char[]>(
() =>
Filter.ReservedPerRfc3986.ToCharArray());
private static readonly Lazy<IReadOnlyDictionary<string, string>> ReservedCharacterEncodingsPerRfc3986 =
new Lazy<IReadOnlyDictionary<string, string>>(
() =>
Filter.InitializeReservedCharacter3986Encodings());
private static readonly Lazy<IReadOnlyDictionary<string, string>> ReservedCharacterEncodingsPerRfc2396 =
new Lazy<IReadOnlyDictionary<string, string>>(
() =>
Filter.InitializeReservedCharacter2396Encodings());
private string comparisonValue;
private string comparisonValueEncoded;
private AttributeDataType? dataType;
private Filter()
{
}
public Filter(string attributePath, ComparisonOperator filterOperator, string comparisonValue)
{
if (string.IsNullOrWhiteSpace(attributePath))
{
throw new ArgumentNullException(nameof(attributePath));
}
if (string.IsNullOrWhiteSpace(comparisonValue))
{
throw new ArgumentNullException(nameof(comparisonValue));
}
this.AttributePath = attributePath;
this.FilterOperator = filterOperator;
this.ComparisonValue = comparisonValue;
this.DataType = AttributeDataType.@string;
}
public Filter(IFilter other)
: this(other?.AttributePath, other.FilterOperator, other?.ComparisonValue)
{
if (null == other)
{
throw new ArgumentNullException(nameof(other));
}
this.DataType = other.DataType;
if (other.AdditionalFilter != null)
{
this.AdditionalFilter = new Filter(other.AdditionalFilter);
}
}
private enum ComparisonOperatorValue
{
bitAnd,
eq,
ne,
co,
sw,
ew,
ge,
gt,
includes,
isMemberOf,
lt,
matchesExpression,
le,
notBitAnd,
notMatchesExpression
}
private enum LogicalOperatorValue
{
and,
or
}
public IFilter AdditionalFilter
{
get;
set;
}
public string AttributePath
{
get;
private set;
}
public string ComparisonValue
{
get
{
return this.comparisonValue;
}
private set
{
Filter.Validate(this.DataType, value);
this.comparisonValue = value;
string encodedValue = this.comparisonValue;
foreach (KeyValuePair<string, string> encoding in Filter.ReservedCharacterEncodingsPerRfc2396.Value)
{
encodedValue = encodedValue.Replace(encoding.Key, encoding.Value, StringComparison.InvariantCulture);
}
this.comparisonValueEncoded = encodedValue;
}
}
public string ComparisonValueEncoded
{
get
{
return this.comparisonValueEncoded;
}
}
public AttributeDataType? DataType
{
get
{
return this.dataType;
}
set
{
Filter.Validate(value, this.ComparisonValue);
this.dataType = value;
}
}
public ComparisonOperator FilterOperator
{
get;
set;
}
private static IReadOnlyDictionary<string, string> InitializeReservedCharacter2396Encodings()
{
Dictionary<string, string> result =
Filter.ReservedCharacterEncodingsPerRfc3986.Value
.ToDictionary(
(KeyValuePair<string, string> item) => item.Key,
(KeyValuePair<string, string> item) => item.Value);
result.Add(Filter.Space, Filter.EncodingSpacePer2396);
return result;
}
private static IReadOnlyDictionary<string, string> InitializeReservedCharacter3986Encodings()
{
Dictionary<string, string> result =
new Dictionary<string, string>(Filter.ReservedCharactersPerRfc3986.Value.Length);
foreach (char character in Filter.ReservedCharactersPerRfc3986.Value)
{
string from = character.ToString(CultureInfo.InvariantCulture);
string to = HttpUtility.UrlEncode(from);
result.Add(from, to);
}
return result;
}
public string Serialize()
{
ComparisonOperatorValue operatorValue;
switch (this.FilterOperator)
{
case ComparisonOperator.BitAnd:
operatorValue = ComparisonOperatorValue.bitAnd;
break;
case ComparisonOperator.EndsWith:
operatorValue = ComparisonOperatorValue.ew;
break;
case ComparisonOperator.Equals:
operatorValue = ComparisonOperatorValue.eq;
break;
case ComparisonOperator.EqualOrGreaterThan:
operatorValue = ComparisonOperatorValue.ge;
break;
case ComparisonOperator.GreaterThan:
operatorValue = ComparisonOperatorValue.gt;
break;
case ComparisonOperator.EqualOrLessThan:
operatorValue = ComparisonOperatorValue.le;
break;
case ComparisonOperator.LessThan:
operatorValue = ComparisonOperatorValue.lt;
break;
case ComparisonOperator.Includes:
operatorValue = ComparisonOperatorValue.includes;
break;
case ComparisonOperator.IsMemberOf:
operatorValue = ComparisonOperatorValue.isMemberOf;
break;
case ComparisonOperator.MatchesExpression:
operatorValue = ComparisonOperatorValue.matchesExpression;
break;
case ComparisonOperator.NotBitAnd:
operatorValue = ComparisonOperatorValue.notBitAnd;
break;
case ComparisonOperator.NotEquals:
operatorValue = ComparisonOperatorValue.ne;
break;
case ComparisonOperator.NotMatchesExpression:
operatorValue = ComparisonOperatorValue.notMatchesExpression;
break;
default:
string notSupportedValue = Enum.GetName(typeof(ComparisonOperator), this.FilterOperator);
throw new NotSupportedException(notSupportedValue);
}
string rightHandSide;
AttributeDataType effectiveDataType = this.DataType ?? AttributeDataType.@string;
switch (effectiveDataType)
{
case AttributeDataType.boolean:
case AttributeDataType.@decimal:
case AttributeDataType.integer:
rightHandSide = this.ComparisonValue;
break;
default:
rightHandSide =
string.Format(
CultureInfo.InvariantCulture,
Filter.ComparisonValueTemplate,
this.ComparisonValue);
break;
}
string filter =
string.Format(
CultureInfo.InvariantCulture,
Filter.TemplateComparison,
this.AttributePath,
operatorValue,
rightHandSide);
string result;
if (this.AdditionalFilter != null)
{
string additionalFilter = this.AdditionalFilter.Serialize();
result =
string.Format(
CultureInfo.InvariantCulture,
Filter.TemplateConjunction,
filter,
Filter.LogicalOperatorValue.and,
additionalFilter);
}
else
{
result = filter;
}
return result;
}
public override string ToString()
{
string result = this.Serialize();
return result;
}
public static string ToString(IReadOnlyCollection<IFilter> filters)
{
if (null == filters)
{
throw new ArgumentNullException(nameof(filters));
}
string placeholder = Guid.NewGuid().ToString();
string allFilters = null;
foreach (IFilter filter in filters)
{
Filter clone = new Filter(filter);
clone.ComparisonValue = placeholder;
string currentFilter = clone.Serialize();
string encodedFilter =
HttpUtility
.UrlEncode(currentFilter)
.Replace(placeholder, filter.ComparisonValueEncoded, StringComparison.InvariantCulture);
if (string.IsNullOrWhiteSpace(allFilters))
{
allFilters =
filters.Count > 1 ?
string.Format(CultureInfo.InvariantCulture, Filter.TemplateNesting, encodedFilter) :
encodedFilter;
}
else
{
string rightHandSide =
filter.AdditionalFilter != null || filters.Count > 1 ?
string.Format(CultureInfo.InvariantCulture, Filter.TemplateNesting, encodedFilter) :
encodedFilter;
allFilters =
string.Format(
CultureInfo.InvariantCulture,
Filter.TemplateConjunction,
allFilters,
Filter.LogicalOperatorValue.or,
rightHandSide);
}
}
string result =
string.Format(
CultureInfo.InvariantCulture,
Filter.Template,
allFilters);
return result;
}
public static bool TryParse(string filterExpression, out IReadOnlyCollection<IFilter> filters)
{
string expression = filterExpression?.Trim()?.Unquote();
if (string.IsNullOrWhiteSpace(expression))
{
throw new ArgumentNullException(nameof(filterExpression));
}
try
{
IReadOnlyCollection<IFilter> buffer = new FilterExpression(expression).ToFilters();
filters = buffer;
return true;
}
catch (ArgumentOutOfRangeException)
{
filters = null;
return false;
}
catch (ArgumentException)
{
filters = null;
return false;
}
catch (InvalidOperationException)
{
filters = null;
return false;
}
}
private static void Validate(AttributeDataType? dataType, string value)
{
if (!dataType.HasValue || string.IsNullOrWhiteSpace(value))
{
return;
}
switch (dataType.Value)
{
case AttributeDataType.boolean:
if (!bool.TryParse(value, out bool _))
{
throw new InvalidOperationException(
SystemForCrossDomainIdentityManagementProtocolResources.ExceptionInvalidValue);
}
break;
case AttributeDataType.@decimal:
if (!double.TryParse(value, out double _))
{
throw new InvalidOperationException(
SystemForCrossDomainIdentityManagementProtocolResources.ExceptionInvalidValue);
}
break;
case AttributeDataType.integer:
if (!long.TryParse(value, out long _))
{
throw new InvalidOperationException(
SystemForCrossDomainIdentityManagementProtocolResources.ExceptionInvalidValue);
}
break;
case AttributeDataType.binary:
case AttributeDataType.complex:
case AttributeDataType.dateTime:
case AttributeDataType.reference:
case AttributeDataType.@string:
break;
default:
string unsupported = Enum.GetName(typeof(AttributeDataType), dataType.Value);
throw new NotSupportedException(unsupported);
}
}
}
}