mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

vharseko
24.59.2023 6877de268aedfc0715bcc3578a7695c4ee278a7f
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2009 Sun Microsystems, Inc.
 * Portions Copyright 2012-2016 ForgeRock AS.
 */
package org.opends.server.api;
 
import java.util.Collection;
import java.util.List;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.config.server.ConfigException;
import org.forgerock.opendj.ldap.Assertion;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ConditionResult;
import org.forgerock.opendj.ldap.DecodeException;
import org.forgerock.opendj.ldap.schema.MatchingRule;
import org.forgerock.opendj.server.config.server.VirtualAttributeCfg;
import org.opends.server.core.SearchOperation;
import org.opends.server.types.Attribute;
import org.opends.server.types.Entry;
import org.opends.server.types.InitializationException;
import org.opends.server.types.VirtualAttributeRule;
 
/**
 * This class defines the set of methods and structures that must be
 * implemented by a Directory Server module that implements the
 * functionality required for one or more virtual attributes.
 *
 * @param  <T>  The type of configuration handled by this virtual
 *              attribute provider.
 */
@org.opends.server.types.PublicAPI(
     stability=org.opends.server.types.StabilityLevel.VOLATILE,
     mayInstantiate=false,
     mayExtend=true,
     mayInvoke=false)
public abstract class VirtualAttributeProvider
       <T extends VirtualAttributeCfg>
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  /**
   * Initializes this virtual attribute based on the information in
   * the provided configuration entry.
   *
   * @param  configuration  The configuration to use to initialize
   *                        this virtual attribute provider.
   *
   * @throws  ConfigException  If an unrecoverable problem arises in
   *                           the process of performing the
   *                           initialization.
   *
   * @throws  InitializationException  If a problem occurs during
   *                                   initialization that is not
   *                                   related to the server
   *                                   configuration.
   */
  public void initializeVirtualAttributeProvider(T configuration)
      throws ConfigException, InitializationException
  {
    // No initialization required
  }
 
 
 
  /**
   * Indicates whether the provided configuration is acceptable for
   * this virtual attribute provider.  It should be possible to call
   * this method on an uninitialized virtual attribute provider
   * instance in order to determine whether the virtual attribute
   * provider would be able to use the provided configuration.
   *
   * @param  configuration        The virtual attribute provider
   *                              configuration for which to make the
   *                              determination.
   * @param  unacceptableReasons  A list that may be used to hold the
   *                              reasons that the provided
   *                              configuration is not acceptable.
   *
   * @return  {@code true} if the provided configuration is acceptable
   *          for this virtual attribute provider, or {@code false} if
   *          not.
   */
  public boolean isConfigurationAcceptable(
                      VirtualAttributeCfg configuration,
                      List<LocalizableMessage> unacceptableReasons)
  {
    // This default implementation does not perform any special validation.
    // It should be overridden by virtual attribute provider implementations
    // that wish to perform more detailed validation.
    return true;
  }
 
 
 
  /**
   * Performs any finalization that may be necessary whenever this
   * virtual attribute provider is taken out of service.
   */
  public void finalizeVirtualAttributeProvider()
  {
    // No implementation required by default.
  }
 
 
 
  /**
   * Indicates whether this virtual attribute provider may generate
   * multiple values.
   *
   * @return  {@code true} if this virtual attribute provider may
   *          generate multiple values, or {@code false} if not.
   */
  public abstract boolean isMultiValued();
 
 
 
  /**
   * Generates an unmodifiable attribute with the values for the provided entry.
   *
   * @param entry
   *          The entry for which the values are to be generated.
   * @param rule
   *          The virtual attribute rule which defines the constraints
   *          for the virtual attribute.
   * @return The unmodifiable attribute with the values generated for the
   *         provided entry. It may be empty, but it must not be {@code null}.
   */
  public abstract Attribute getValues(Entry entry, VirtualAttributeRule rule);
 
 
 
  /**
   * Indicates whether this virtual attribute provider will generate
   * at least one value for the provided entry.
   *
   * @param  entry  The entry for which to make the determination.
   * @param  rule   The virtual attribute rule which defines the
   *                constraints for the virtual attribute.
   *
   * @return  {@code true} if this virtual attribute provider will
   *          generate at least one value for the provided entry, or
   *          {@code false} if not.
   */
  public boolean hasValue(Entry entry, VirtualAttributeRule rule)
  {
    return !getValues(entry, rule).isEmpty();
  }
 
 
 
  /**
   * Indicates whether this virtual attribute provider will generate
   * the provided value.
   *
   * @param  entry  The entry for which to make the determination.
   * @param  rule   The virtual attribute rule which defines the
   *                constraints for the virtual attribute.
   * @param  value  The value for which to make the determination.
   *
   * @return  {@code true} if this virtual attribute provider will
   *          generate the specified value for the provided entry, or
   *          {@code false} if not.
   */
  public boolean hasValue(Entry entry, VirtualAttributeRule rule, ByteString value)
  {
    return getValues(entry, rule).contains(value);
  }
 
  /**
   * Indicates whether this virtual attribute provider matches the assertion
   * value.
   *
   * @param entry
   *          The entry for which to make the determination.
   * @param rule
   *          The virtual attribute rule which defines the constraints for the
   *          virtual attribute.
   * @param assertionValue
   *          The assertion value for which to make the determination.
   * @return {@code true} if this virtual attribute provider matches the
   *         specified assertion value for the provided entry, or {@code false}
   *         if not.
   */
  public ConditionResult matchesEqualityAssertion(Entry entry,
      VirtualAttributeRule rule, ByteString assertionValue)
  {
    return getValues(entry, rule).matchesEqualityAssertion(assertionValue);
  }
 
 
  /**
   * Indicates whether this virtual attribute provider will generate
   * all of the values in the provided collection.
   *
   * @param  entry   The entry for which to make the determination.
   * @param  rule    The virtual attribute rule which defines the
   *                 constraints for the virtual attribute.
   * @param  values  The set of values for which to make the
   *                 determination.
   *
   * @return  {@code true} if this attribute provider will generate
   *          all of the values in the provided collection, or
   *          {@code false} if it will not generate at least one of
   *          them.
   */
  public boolean hasAllValues(Entry entry, VirtualAttributeRule rule, Collection<?> values)
  {
    return getValues(entry, rule).containsAll(values);
  }
 
 
 
  /**
   * Indicates whether this virtual attribute provider will generate
   * any value which matches the provided substring.
   *
   * @param  entry       The entry for which to make the
   *                     determination.
   * @param  rule        The virtual attribute rule which defines the
   *                     constraints for the virtual attribute.
   * @param  subInitial  The subInitial component to use in the
   *                     determination.
   * @param  subAny      The subAny components to use in the
   *                     determination.
   * @param  subFinal    The subFinal component to use in the
   *                     determination.
   *
   * @return  {@code UNDEFINED} if this attribute does not have a
   *          substring matching rule, {@code TRUE} if at least one
   *          value matches the provided substring, or {@code FALSE}
   *          otherwise.
   */
  public ConditionResult matchesSubstring(Entry entry,
                                          VirtualAttributeRule rule,
                                          ByteString subInitial,
                                          List<ByteString> subAny,
                                          ByteString subFinal)
  {
    MatchingRule matchingRule = rule.getAttributeType().getSubstringMatchingRule();
    if (matchingRule == null)
    {
      return ConditionResult.UNDEFINED;
    }
 
    Assertion assertion;
    try
    {
      assertion = matchingRule.getSubstringAssertion(subInitial, subAny, subFinal);
    }
    catch(DecodeException e) {
      logger.traceException(e);
      return ConditionResult.UNDEFINED;
    }
 
    ConditionResult result = ConditionResult.FALSE;
    for (ByteString value : getValues(entry, rule))
    {
      try
      {
        if (assertion.matches(matchingRule.normalizeAttributeValue(value)).toBoolean())
        {
          return ConditionResult.TRUE;
        }
      }
      catch (Exception e)
      {
        logger.traceException(e);
 
        // We couldn't normalize one of the attribute values.
        // We will return "undefined" if we can't find a definite match
        result = ConditionResult.UNDEFINED;
      }
    }
 
    return result;
  }
 
 
  /**
   * Indicates whether this virtual attribute provider will generate any value
   * for the provided entry that is greater than or equal to the given value.
   *
   * @param entry
   *          The entry for which to make the determination.
   * @param rule
   *          The virtual attribute rule which defines the constraints for the
   *          virtual attribute.
   * @param assertionValue
   *          The assertion value for which to make the determination.
   * @return {@code UNDEFINED} if the associated attribute type does not have an
   *         ordering matching rule, {@code TRUE} if at least one of the
   *         generated values will be greater than or equal to the specified
   *         assertion value, or {@code FALSE} if none of the generated values
   *         will be greater than or equal to the specified value.
   */
  public ConditionResult greaterThanOrEqualTo(Entry entry,
                              VirtualAttributeRule rule,
                              ByteString assertionValue)
  {
    MatchingRule matchingRule = rule.getAttributeType().getOrderingMatchingRule();
    if (matchingRule == null)
    {
      return ConditionResult.UNDEFINED;
    }
 
    Assertion assertion = null;
    try
    {
      assertion = matchingRule.getGreaterOrEqualAssertion(assertionValue);
    }
    catch (Exception e)
    {
      logger.traceException(e);
      return ConditionResult.UNDEFINED;
    }
 
    ConditionResult result = ConditionResult.FALSE;
    for (ByteString v : getValues(entry, rule))
    {
      try
      {
        if (assertion.matches(matchingRule.normalizeAttributeValue(v)).toBoolean())
        {
          return ConditionResult.TRUE;
        }
      }
      catch (Exception e)
      {
        logger.traceException(e);
        // We couldn't normalize one of the attribute values.
        // We will return "undefined" if we can't find a definite match
        result = ConditionResult.UNDEFINED;
      }
    }
 
    return result;
  }
 
 
 
  /**
   * Indicates whether this virtual attribute provider will generate any value
   * for the provided entry that is less than or equal to the given value.
   *
   * @param entry
   *          The entry for which to make the determination.
   * @param rule
   *          The virtual attribute rule which defines the constraints for the
   *          virtual attribute.
   * @param assertionValue
   *          The assertion value for which to make the determination.
   * @return {@code UNDEFINED} if the associated attribute type does not have an
   *         ordering matching rule, {@code TRUE} if at least one of the
   *         generated values will be less than or equal to the specified
   *         assertion value, or {@code FALSE} if none of the generated values
   *         will be greater than or equal to the specified value.
   */
  public ConditionResult lessThanOrEqualTo(Entry entry,
                              VirtualAttributeRule rule,
                              ByteString assertionValue)
  {
    MatchingRule matchingRule = rule.getAttributeType().getOrderingMatchingRule();
    if (matchingRule == null)
    {
      return ConditionResult.UNDEFINED;
    }
 
    Assertion assertion = null;
    try
    {
      assertion = matchingRule.getLessOrEqualAssertion(assertionValue);
    }
    catch (Exception e)
    {
      logger.traceException(e);
      return ConditionResult.UNDEFINED;
    }
 
    ConditionResult result = ConditionResult.FALSE;
    for (ByteString v : getValues(entry, rule))
    {
      try
      {
        if (assertion.matches(matchingRule.normalizeAttributeValue(v)).toBoolean())
        {
          return ConditionResult.TRUE;
        }
      }
      catch (Exception e)
      {
        logger.traceException(e);
 
        // We couldn't normalize one of the attribute values.
        // We will return "undefined" if we can't find a definite match
        result = ConditionResult.UNDEFINED;
      }
    }
 
    return result;
  }
 
 
 
  /**
   * Indicates whether this virtual attribute provider will generate
   * any value for the provided entry that is approximately equal to
   * the given value.
   *
   * @param  entry  The entry for which to make the determination.
   * @param  rule   The virtual attribute rule which defines the
   *                constraints for the virtual attribute.
   * @param  assertionValue
   *          The assertion value for which to make the determination.
   *
   * @return  {@code UNDEFINED} if the associated attribute type does
   *          not have an approximate matching rule, {@code TRUE} if at
   *          least one of the generated values will be approximately
   *          equal to the specified value, or {@code FALSE} if none
   *          of the generated values will be approximately equal to
   *          the specified assertion value.
   */
  public ConditionResult approximatelyEqualTo(Entry entry,
                              VirtualAttributeRule rule,
                              ByteString assertionValue)
  {
    MatchingRule matchingRule = rule.getAttributeType().getApproximateMatchingRule();
    if (matchingRule == null)
    {
      return ConditionResult.UNDEFINED;
    }
 
    Assertion assertion = null;
    try
    {
      assertion = matchingRule.getAssertion(assertionValue);
    }
    catch (Exception e)
    {
      logger.traceException(e);
      return ConditionResult.UNDEFINED;
    }
 
    ConditionResult result = ConditionResult.FALSE;
    for (ByteString v : getValues(entry, rule))
    {
      try
      {
        if  (assertion.matches(matchingRule.normalizeAttributeValue(v)).toBoolean())
        {
          return ConditionResult.TRUE;
        }
      }
      catch (Exception e)
      {
        logger.traceException(e);
        // We couldn't normalize one of the attribute values.
        // We will return "undefined" if we can't find a definite match
        result = ConditionResult.UNDEFINED;
      }
    }
    return result;
  }
 
 
 
  /**
   * Indicates whether this attribute may be included in search
   * filters as part of the criteria for locating entries.
   *
   * @param rule             The virtual attribute rule which defines
   *                         the constraints for the virtual
   *                         attribute.
   * @param searchOperation  The search operation for which to make
   *                         the determination.
   * @param isPreIndexed     Indicates if we expect the search on the virtual
   *                         attribute to be faster than an index search.
   * @return {@code true} if this attribute may be included in search
   * filters, or {@code false} if not.
   */
  public abstract boolean isSearchable(VirtualAttributeRule rule,
                                       SearchOperation searchOperation,
                                       boolean isPreIndexed);
 
 
 
  /**
   * Processes the provided search operation in which the search
   * criteria includes an operation targeted at this virtual
   * attribute.  This method should only be called if
   * {@code isSearchable} returns true and it is not possible to
   * construct a manageable candidate list by processing other
   * elements of the search criteria.
   *
   * @param  rule             The virtual attribute rule which defines
   *                          the constraints for the virtual
   *                          attribute.
   * @param  searchOperation  The search operation to be processed.
   */
  public abstract void processSearch(VirtualAttributeRule rule,
                                     SearchOperation searchOperation);
}