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

Violette Roche-Montane
09.26.2012 04ff552c42f3200dd71a58f94e9d57436b67c4f3
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2011-2012 ForgeRock AS
 */
 
package org.forgerock.opendj.ldap.schema;
 
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldap.ErrorResultException;
 
/**
 * This class provides various schema validation policy options for controlling
 * how entries should be validated against the directory schema.
 */
public final class SchemaValidationPolicy {
    /**
     * A call-back which will be called during DIT structure rule schema
     * validation in order to retrieve the parent of the entry being validated.
     */
    public static interface EntryResolver {
        /**
         * Returns the named entry in order to enforce DIT structure rules.
         *
         * @param dn
         *            The name of the entry to be returned.
         * @return The named entry.
         * @throws ErrorResultException
         *             If the entry could not be retrieved.
         */
        Entry getEntry(DN dn) throws ErrorResultException;
    }
 
    /**
     * The schema validation policy.
     */
    public static enum Policy {
        /**
         * Schema validation will not be performed.
         */
        IGNORE,
 
        /**
         * Schema validation will be performed, but failures will not cause the
         * overall validation to fail. Error messages will be returned.
         */
        WARN,
 
        /**
         * Schema validation will be performed and failures will cause the
         * overall validation to fail. Error messages will be returned.
         */
        REJECT;
 
        private Policy() {
            // Nothing to do.
        }
 
        /**
         * Returns {@code true} if this policy is {@code IGNORE}.
         *
         * @return {@code true} if this policy is {@code IGNORE}.
         */
        public boolean isIgnore() {
            return this == IGNORE;
        }
 
        /**
         * Returns {@code true} if this policy is {@code REJECT}.
         *
         * @return {@code true} if this policy is {@code REJECT}.
         */
        public boolean isReject() {
            return this == REJECT;
        }
 
        /**
         * Returns {@code true} if this policy is {@code WARN}.
         *
         * @return {@code true} if this policy is {@code WARN}.
         */
        public boolean isWarn() {
            return this == WARN;
        }
 
        /**
         * Returns {@code true} if this policy is {@code WARN} or {@code REJECT}
         * .
         *
         * @return {@code true} if this policy is {@code WARN} or {@code REJECT}
         *         .
         */
        public boolean needsChecking() {
            return this != IGNORE;
        }
    }
 
    /**
     * Creates a copy of the provided schema validation policy.
     *
     * @param policy
     *            The policy to be copied.
     * @return The copy of the provided schema validation policy.
     */
    public static SchemaValidationPolicy copyOf(final SchemaValidationPolicy policy) {
        return defaultPolicy().assign(policy);
    }
 
    /**
     * Creates a new schema validation policy with default settings. More
     * specifically:
     * <ul>
     * <li>Entries not having a single structural object class will be rejected
     * <li>Entries having attributes which are not permitted by its object
     * classes or DIT content rule (if present) will be rejected
     * <li>Entries not conforming to name forms will be rejected
     * <li>DIT structure rules will not be ignored
     * </ul>
     *
     * @return The new schema validation policy.
     */
    public static SchemaValidationPolicy defaultPolicy() {
        return new SchemaValidationPolicy();
    }
 
    /**
     * Creates a new schema validation policy which will not perform any schema
     * validation.
     *
     * @return The new schema validation policy.
     */
    public static SchemaValidationPolicy ignoreAll() {
        return new SchemaValidationPolicy().checkAttributesAndObjectClasses(Policy.IGNORE)
                .checkAttributeValues(Policy.IGNORE).checkDITContentRules(Policy.IGNORE)
                .checkNameForms(Policy.IGNORE).requireSingleStructuralObjectClass(Policy.IGNORE);
    }
 
    private Policy checkNameForms = Policy.REJECT;
 
    private Policy checkDITStructureRules = Policy.IGNORE;
 
    private Policy checkDITContentRules = Policy.REJECT;
 
    private Policy requireSingleStructuralObjectClass = Policy.REJECT;
 
    private Policy checkAttributesAndObjectClasses = Policy.REJECT;
 
    private Policy checkAttributeValues = Policy.REJECT;
 
    private EntryResolver checkDITStructureRulesEntryResolver = null;
 
    // Prevent direct instantiation.
    private SchemaValidationPolicy() {
        // Nothing to do.
    }
 
    /**
     * Returns the policy for verifying that the user attributes in an entry
     * conform to its object classes. More specifically, an entry must contain
     * all required user attributes, and must not contain any user attributes
     * which are not declared as required or optional by its object classes.
     * <p>
     * By default entries which have missing or additional user attributes will
     * be rejected.
     *
     * @return The policy for verifying that the user attributes in an entry
     *         conform to its object classes.
     */
    public Policy checkAttributesAndObjectClasses() {
        return checkAttributesAndObjectClasses;
    }
 
    /**
     * Specifies the policy for verifying that the user attributes in an entry
     * conform to its object classes. More specifically, an entry must contain
     * all required user attributes, and must not contain any user attributes
     * which are not declared as required or optional by its object classes.
     * <p>
     * By default entries which have missing or additional user attributes will
     * be rejected.
     *
     * @param policy
     *            The policy for verifying that the user attributes in an entry
     *            conform to its object classes.
     * @return A reference to this {@code SchemaValidationPolicy}.
     */
    public SchemaValidationPolicy checkAttributesAndObjectClasses(final Policy policy) {
        this.checkAttributesAndObjectClasses = policy;
        return this;
    }
 
    /**
     * Returns the policy for verifying that the user attributes in an entry
     * conform to their associated attribute type descriptions. This may
     * include:
     * <ul>
     * <li>checking that there is at least one value
     * <li>checking that single-valued attributes contain only a single value
     * <li>checking that there are no duplicate values according to the
     * attribute's default equality matching rule
     * <li>checking that attributes which require BER encoding specify the
     * {@code ;binary} attribute option
     * <li>checking that the values are valid according to the attribute's
     * syntax.
     * </ul>
     * Schema validation implementations specify exactly which of the above
     * checks will be performed.
     * <p>
     * By default entries which have invalid attribute values will be rejected.
     *
     * @return The policy for verifying that the user attributes in an entry
     *         conform to their associated attribute type descriptions.
     */
    public Policy checkAttributeValues() {
        return checkAttributeValues;
    }
 
    /**
     * Specifies the policy for verifying that the user attributes in an entry
     * conform to their associated attribute type descriptions. This may
     * include:
     * <ul>
     * <li>checking that there is at least one value
     * <li>checking that single-valued attributes contain only a single value
     * <li>checking that there are no duplicate values according to the
     * attribute's default equality matching rule
     * <li>checking that attributes which require BER encoding specify the
     * {@code ;binary} attribute option
     * <li>checking that the values are valid according to the attribute's
     * syntax.
     * </ul>
     * Schema validation implementations specify exactly which of the above
     * checks will be performed.
     * <p>
     * By default entries which have invalid attribute values will be rejected.
     *
     * @param policy
     *            The policy for verifying that the user attributes in an entry
     *            conform to their associated attribute type descriptions.
     * @return A reference to this {@code SchemaValidationPolicy}.
     */
    public SchemaValidationPolicy checkAttributeValues(final Policy policy) {
        this.checkAttributeValues = policy;
        return this;
    }
 
    /**
     * Returns the policy for validating entries against content rules defined
     * in the schema.
     * <p>
     * By default content rules will be ignored during validation.
     *
     * @return The policy for validating entries against content rules defined
     *         in the schema.
     */
    public Policy checkDITContentRules() {
        return checkDITContentRules;
    }
 
    /**
     * Specifies the policy for validating entries against content rules defined
     * in the schema.
     * <p>
     * By default content rules will be ignored during validation.
     *
     * @param policy
     *            The policy for validating entries against content rules
     *            defined in the schema.
     * @return A reference to this {@code SchemaValidationPolicy}.
     */
    public SchemaValidationPolicy checkDITContentRules(final Policy policy) {
        this.checkDITContentRules = policy;
        return this;
    }
 
    /**
     * Returns the policy for validating entries against structure rules defined
     * in the schema.
     * <p>
     * By default structure rules will be ignored during validation.
     *
     * @return The policy for validating entries against structure rules defined
     *         in the schema.
     */
    public Policy checkDITStructureRules() {
        return checkDITStructureRules;
    }
 
    /**
     * Specifies the policy for validating entries against structure rules
     * defined in the schema.
     * <p>
     * By default structure rules will be ignored during validation.
     *
     * @param policy
     *            The policy for validating entries against structure rules
     *            defined in the schema.
     * @param resolver
     *            The parent entry resolver which should be used for retrieving
     *            the parent entry during DIT structure rule validation.
     * @return A reference to this {@code SchemaValidationPolicy}.
     * @throws IllegalArgumentException
     *             If {@code resolver} was {@code null} and
     *             {@code checkDITStructureRules} is either {@code WARN} or
     *             {@code REJECT}.
     */
    public SchemaValidationPolicy checkDITStructureRules(final Policy policy,
            final EntryResolver resolver) {
        if (checkDITStructureRules.needsChecking() && resolver == null) {
            throw new IllegalArgumentException(
                    "Validation of structure rules enabled by resolver was null");
        }
        this.checkDITStructureRules = policy;
        this.checkDITStructureRulesEntryResolver = resolver;
        return this;
    }
 
    /**
     * Returns parent entry resolver which should be used for retrieving the
     * parent entry during DIT structure rule validation.
     * <p>
     * By default no resolver is defined because structure rules will be ignored
     * during validation.
     *
     * @return The parent entry resolver which should be used for retrieving the
     *         parent entry during DIT structure rule validation.
     */
    public EntryResolver checkDITStructureRulesEntryResolver() {
        return checkDITStructureRulesEntryResolver;
    }
 
    /**
     * Returns the policy for validating entries against name forms defined in
     * the schema.
     * <p>
     * By default name forms will be ignored during validation.
     *
     * @return The policy for validating entries against name forms defined in
     *         the schema.
     */
    public Policy checkNameForms() {
        return checkNameForms;
    }
 
    /**
     * Specifies the policy for validating entries against name forms defined in
     * the schema.
     * <p>
     * By default name forms will be ignored during validation.
     *
     * @param policy
     *            The policy for validating entries against name forms defined
     *            in the schema.
     * @return A reference to this {@code SchemaValidationPolicy}.
     */
    public SchemaValidationPolicy checkNameForms(final Policy policy) {
        this.checkNameForms = policy;
        return this;
    }
 
    /**
     * Returns the policy for verifying that entries have only a single
     * structural object class.
     * <p>
     * By default entries which do not have a structural object class or which
     * have more than one structural object class will be rejected.
     *
     * @return The policy for checking that entries have one and only one
     *         structural object class.
     */
    public Policy requireSingleStructuralObjectClass() {
        return requireSingleStructuralObjectClass;
    }
 
    /**
     * Specifies the policy for verifying that entries have only a single
     * structural object class.
     * <p>
     * By default entries which do not have a structural object class or which
     * have more than one structural object class will be rejected.
     *
     * @param policy
     *            The policy for checking that entries have one and only one
     *            structural object class.
     * @return A reference to this {@code SchemaValidationPolicy}.
     */
    public SchemaValidationPolicy requireSingleStructuralObjectClass(final Policy policy) {
        this.requireSingleStructuralObjectClass = policy;
        return this;
    }
 
    // Assigns the provided options to this set of options.
    SchemaValidationPolicy assign(final SchemaValidationPolicy policy) {
        this.checkAttributeValues = policy.checkAttributeValues;
        this.checkNameForms = policy.checkNameForms;
        this.checkAttributesAndObjectClasses = policy.checkAttributesAndObjectClasses;
        this.checkDITContentRules = policy.checkDITContentRules;
        this.checkDITStructureRules = policy.checkDITStructureRules;
        this.checkDITStructureRulesEntryResolver = policy.checkDITStructureRulesEntryResolver;
        this.requireSingleStructuralObjectClass = policy.requireSingleStructuralObjectClass;
        return this;
    }
 
}