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

Jean-Noel Rouvignac
26.01.2013 d04c1b7a6730690bf4574b428adaa2bde1d94ca3
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
 * 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 2013 ForgeRock AS.
 */
package org.forgerock.opendj.adapter.server2x;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
 
import org.forgerock.opendj.ldap.Attribute;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.DecodeException;
import org.forgerock.opendj.ldap.DereferenceAliasesPolicy;
import org.forgerock.opendj.ldap.ErrorResultException;
import org.forgerock.opendj.ldap.LinkedAttribute;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.controls.Control;
import org.forgerock.opendj.ldap.controls.GenericControl;
import org.forgerock.opendj.ldap.responses.Responses;
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.opends.server.protocols.asn1.ASN1;
import org.opends.server.protocols.asn1.ASN1Exception;
import org.opends.server.protocols.asn1.ASN1Writer;
import org.opends.server.protocols.ldap.LDAPAttribute;
import org.opends.server.protocols.ldap.LDAPControl;
import org.opends.server.protocols.ldap.LDAPFilter;
import org.opends.server.protocols.ldap.LDAPModification;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.ByteStringBuilder;
import org.opends.server.types.DereferencePolicy;
import org.opends.server.types.LDAPException;
import org.opends.server.types.ModificationType;
import org.opends.server.types.Operation;
import org.opends.server.types.SearchResultReference;
import org.opends.server.types.SearchScope;
 
/**
 * Common utility methods.
 */
public final class Converters {
 
    // Prevent instantiation.
    private Converters() {
        throw new AssertionError();
    }
 
    /**
     * Converts from OpenDJ LDAP SDK {@link DereferenceAliasesPolicy} to OpenDJ
     * server {@link DereferencePolicy}.
     *
     * @param dereferenceAliasesPolicy
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.DereferencePolicy to(
            final DereferenceAliasesPolicy dereferenceAliasesPolicy) {
        return DereferencePolicy.values()[dereferenceAliasesPolicy.intValue()];
    }
 
    /**
     * Converts from OpenDJ LDAP SDK {@link DN} to OpenDJ server
     * {@link org.opends.server.types.DN}.
     *
     * @param userDn
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.DN to(final DN userDn) {
        try {
            return org.opends.server.types.DN.decode(userDn.toString());
        } catch (Exception e) {
            throw new IllegalStateException(e.getMessage());
        }
    }
 
    /**
     * Converts from OpenDJ LDAP SDK {@link ByteString} to OpenDJ server
     * {@link org.opends.server.types.ByteString}.
     *
     * @param value
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.ByteString to(ByteString value) {
        if (value != null) {
            return org.opends.server.types.ByteString.wrap(value.toByteArray());
        }
        return null;
    }
 
    /**
     * Converts from OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.SearchScope}
     * to OpenDJ server {@link org.opends.server.types.SearchScope}.
     *
     * @param searchScope
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.SearchScope to(
            final org.forgerock.opendj.ldap.SearchScope searchScope) {
        return SearchScope.values()[searchScope.intValue()];
    }
 
    /**
     * Converts from OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.Filter} to
     * OpenDJ server {@link org.opends.server.types.RawFilter}.
     *
     * @param filter
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.RawFilter to(final org.forgerock.opendj.ldap.Filter filter) {
        org.opends.server.protocols.ldap.LDAPFilter ldapFilter = null;
        try {
            ldapFilter = LDAPFilter.decode(filter.toString());
        } catch (LDAPException e) {
            throw new IllegalStateException(e);
        }
        return ldapFilter;
    }
 
    /**
     * Converts from OpenDJ LDAP SDK
     * {@link org.forgerock.opendj.ldap.responses.SearchResultReference} to OpenDJ
     * server {@link org.opends.server.types.SearchResultReference}.
     *
     * @param searchResultReference
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.SearchResultReference to(
            final org.forgerock.opendj.ldap.responses.SearchResultReference searchResultReference) {
        return new SearchResultReference(searchResultReference.getURIs(), to(searchResultReference
                .getControls()));
    }
 
    /**
     * Converts from OpenDJ LDAP SDK {@link String} to OpenDJ server
     * {@link org.opends.server.types.ByteString}.
     *
     * @param value
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.ByteString to(final String value) {
        return org.opends.server.types.ByteString.valueOf(value);
    }
 
    /**
     * Converts from OpenDJ LDAP SDK {@link Control} to OpenDJ server
     * {@link org.opends.server.protocols.ldap.LDAPControl}.
     *
     * @param control
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.protocols.ldap.LDAPControl to(final Control control) {
        return new LDAPControl(control.getOID(), control.isCritical(), to(control.getValue()));
    }
 
    /**
     * Converts from a <code>List</code> of OpenDJ LDAP SDK
     * {@link org.forgerock.opendj.ldap.controls.Control} to a <code>List</code>
     * of OpenDJ server {@link org.opends.server.types.Control}.
     *
     * @param listOfControl
     *          value to convert
     * @return the converted value
     */
    static final List<org.opends.server.types.Control> to(
            final List<org.forgerock.opendj.ldap.controls.Control> listOfControl) {
        List<org.opends.server.types.Control> toListofControl =
                new ArrayList<org.opends.server.types.Control>(listOfControl.size());
        for (org.forgerock.opendj.ldap.controls.Control c : listOfControl) {
            toListofControl.add(to(c));
        }
        return toListofControl;
    }
 
    /**
     * Converts from OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.Attribute}
     * to OpenDJ server {@link org.opends.server.types.RawAttribute}.
     *
     * @param attribute
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.RawAttribute to(
            final org.forgerock.opendj.ldap.Attribute attribute) {
        ArrayList<org.opends.server.types.ByteString> listAttributeValues =
                new ArrayList<org.opends.server.types.ByteString>(attribute.size());
 
        for (ByteString b : attribute.toArray()) {
            listAttributeValues.add(to(b));
        }
 
        return new LDAPAttribute(attribute.getAttributeDescriptionAsString(), listAttributeValues);
    }
 
    /**
     * Converts from an <code>Iterable</code> of OpenDJ LDAP SDK
     * {@link org.forgerock.opendj.ldap.Attribute} to a <code>List</code> of
     * OpenDJ server {@link org.opends.server.types.RawAttribute}.
     *
     * @param listOfAttributes
     *          value to convert
     * @return the converted value
     */
    static final List<org.opends.server.types.RawAttribute> to(
            final Iterable<org.forgerock.opendj.ldap.Attribute> listOfAttributes) {
        List<org.opends.server.types.RawAttribute> toListofAttributes =
                new ArrayList<org.opends.server.types.RawAttribute>(
                        ((Collection<org.forgerock.opendj.ldap.Attribute>) listOfAttributes).size());
        for (org.forgerock.opendj.ldap.Attribute a : listOfAttributes) {
            toListofAttributes.add(to(a));
        }
        return toListofAttributes;
    }
 
    /**
     * Converts from OpenDJ LDAP SDK
     * {@link org.forgerock.opendj.ldap.Modification} to OpenDJ server
     * {@link org.opends.server.types.RawModification}.
     *
     * @param modification
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.RawModification to(
            final org.forgerock.opendj.ldap.Modification modification) {
        return new LDAPModification(to(modification.getModificationType()), to(modification
                .getAttribute()));
    }
 
    /**
     * Converts from a <code>List</code> of OpenDJ LDAP SDK
     * {@link org.forgerock.opendj.ldap.Modification} to a <code>List</code> of
     * OpenDJ server {@link org.opends.server.types.RawModification}.
     *
     * @param listOfModifications
     *          value to convert
     * @return the converted value
     */
    static final List<org.opends.server.types.RawModification> toModifications(
            final List<org.forgerock.opendj.ldap.Modification> listOfModifications) {
        List<org.opends.server.types.RawModification> toListofModifications =
                new ArrayList<org.opends.server.types.RawModification>(listOfModifications.size());
        for (org.forgerock.opendj.ldap.Modification m : listOfModifications) {
            toListofModifications.add(to(m));
        }
        return toListofModifications;
    }
 
    /**
     * Converts from OpenDJ LDAP SDK
     * {@link org.forgerock.opendj.ldap.ModificationType} to OpenDJ server
     * {@link org.opends.server.types.ModificationType}.
     *
     * @param modificationType
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.ModificationType to(
            final org.forgerock.opendj.ldap.ModificationType modificationType) {
        return ModificationType.values()[modificationType.intValue()];
    }
 
    /**
     * Converts from OpenDJ server {@link org.opends.server.types.ByteString} to
     * OpenDJ LDAP SDK {@link ByteString}.
     *
     * @param value
     *          value to convert
     * @return the converted value
     */
    static final ByteString from(final org.opends.server.types.ByteString value) {
        if (value != null) {
            return ByteString.wrap(value.toByteArray());
        }
        return null;
    }
 
    /**
     * Converts from OpenDJ server
     * {@link org.opends.server.protocols.ldap.LDAPControl} to OpenDJ LDAP SDK
     * {@link Control}.
     *
     * @param ldapControl
     *          value to convert
     * @return the converted value
     */
    static final Control from(final org.opends.server.protocols.ldap.LDAPControl ldapControl) {
        return GenericControl.newControl(ldapControl.getOID(), ldapControl.isCritical(),
                from(ldapControl.getValue()));
    }
 
    /**
     * Converts from OpenDJ server {@link org.opends.server.types.Control} to
     * OpenDJ LDAP SDK {@link Control}.
     *
     * @param control
     *          value to convert
     * @return the converted value
     */
    static final Control from(final org.opends.server.types.Control control) {
 
        String oid = null;
        boolean isCritical = false;
        ByteString value = null;
        // The server control doesn't have a method for extracting directly the value so, we need to ASN1 it.
        ByteStringBuilder builder = new ByteStringBuilder();
        final ASN1Writer writer = ASN1.getWriter(builder);
        try {
            control.write(writer);
        } catch (IOException e) {
            // Nothing to do.
        }
 
        final ByteString sdkByteString = from(builder.toByteString());
        final org.forgerock.opendj.asn1.ASN1Reader sdkReaderASN1 =
                org.forgerock.opendj.asn1.ASN1.getReader(sdkByteString.toByteArray());
 
        // Reads the ASN1 SDK byte string.
        try {
            sdkReaderASN1.readStartSequence();
            oid = sdkReaderASN1.readOctetStringAsString();
            if (sdkReaderASN1.hasNextElement()
                    && (sdkReaderASN1.peekType() == org.forgerock.opendj.asn1.ASN1.UNIVERSAL_BOOLEAN_TYPE)) {
                isCritical = sdkReaderASN1.readBoolean();
            }
            if (sdkReaderASN1.hasNextElement()
                    && (sdkReaderASN1.peekType() == org.forgerock.opendj.asn1.ASN1.UNIVERSAL_OCTET_STRING_TYPE)) {
                value = sdkReaderASN1.readOctetString();
            }
            sdkReaderASN1.readEndSequence();
        } catch (DecodeException e) {
            // Nothing to do.
        } catch (IOException e) {
            // Nothing to do.
        }
        // Creates the control
        return GenericControl.newControl(oid, isCritical, value);
    }
 
    /**
     * Converts from a <code>List</code> of OpenDJ server
     * {@link org.opends.server.types.Control} to a <code>List</code> of OpenDJ
     * LDAP SDK {@link org.forgerock.opendj.ldap.controls.Control}.
     *
     * @param listOfControl
     *          value to convert
     * @return the converted value
     */
    static final List<org.forgerock.opendj.ldap.controls.Control> from(
            final List<org.opends.server.types.Control> listOfControl) {
        List<org.forgerock.opendj.ldap.controls.Control> fromListofControl =
                new ArrayList<org.forgerock.opendj.ldap.controls.Control>(listOfControl.size());
        for (org.opends.server.types.Control c : listOfControl) {
            fromListofControl.add(from(c));
        }
        return fromListofControl;
    }
 
    /**
     * Converts from OpenDJ server
     * {@link org.opends.server.types.SearchResultReference} to OpenDJ LDAP SDK
     * {@link org.forgerock.opendj.ldap.responses.SearchResultReference}.
     *
     * @param srvResultReference
     *          value to convert
     * @return the converted value
     */
    static final org.forgerock.opendj.ldap.responses.SearchResultReference from(
            final org.opends.server.types.SearchResultReference srvResultReference) {
        return Responses.newSearchResultReference(srvResultReference.getReferralURLString());
    }
 
    /**
     * Converts from OpenDJ server {@link org.opends.server.types.Attribute} to
     * OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.Attribute}.
     *
     * @param attribute
     *          value to convert
     * @return the converted value
     */
    static final org.forgerock.opendj.ldap.Attribute from(
            final org.opends.server.types.Attribute attribute) {
        Attribute sdkAttribute = new LinkedAttribute(attribute.getNameWithOptions());
        for (AttributeValue value : attribute) {
            sdkAttribute.add(value);
        }
        return sdkAttribute;
    }
 
    /**
     * Converts from an <code>Iterable</code> of OpenDJ server
     * {@link org.opends.server.types.Attribute} to a <code>List</code> of OpenDJ
     * LDAP SDK {@link org.forgerock.opendj.ldap.Attribute}.
     *
     * @param listOfAttributes
     *          value to convert
     * @return the converted value
     */
    static final List<org.forgerock.opendj.ldap.Attribute> from(
            final Iterable<org.opends.server.types.Attribute> listOfAttributes) {
        List<org.forgerock.opendj.ldap.Attribute> fromListofAttributes =
                new ArrayList<org.forgerock.opendj.ldap.Attribute>(
                        ((Collection<org.opends.server.types.Attribute>) listOfAttributes).size());
        for (org.opends.server.types.Attribute a : listOfAttributes) {
            fromListofAttributes.add(from(a));
        }
        return fromListofAttributes;
    }
 
    /**
     * Converts from OpenDJ server
     * {@link org.opends.server.types.SearchResultEntry} to OpenDJ LDAP SDK
     * {@link org.forgerock.opendj.ldap.responses.SearchResultEntry}.
     *
     * @param srvResultEntry
     *          value to convert
     * @return the converted value
     */
    static final org.forgerock.opendj.ldap.responses.SearchResultEntry from(
            final org.opends.server.types.SearchResultEntry srvResultEntry) {
 
        final SearchResultEntry searchResultEntry =
                Responses.newSearchResultEntry(srvResultEntry.getDN().toString());
        if (srvResultEntry.getAttributes() != null) {
            for (org.opends.server.types.Attribute a : srvResultEntry.getAttributes()) {
                searchResultEntry.addAttribute(from(a));
            }
        }
 
        if (srvResultEntry.getControls() != null) {
            for (org.opends.server.types.Control c : srvResultEntry.getControls()) {
                searchResultEntry.addControl(from(c));
            }
        }
 
        return searchResultEntry;
    }
 
    /**
     * Populates the result object with the operation details and return the
     * result object if it was successful. Otherwise, it throws an
     * {@link ErrorResultException}.
     *
     * @param <T>
     *          the type of the result object
     * @param operation
     *          used to populate the result
     * @param result
     *          the result object to populate from the Operation
     * @return the result if successful, an {@link ErrorResultException} is thrown
     *         otherwise
     * @throws ErrorResultException
     *           when an error occurs
     */
    static final <T extends Result> T getResponseResult(final Operation operation, final T result)
            throws ErrorResultException {
        if (operation.getReferralURLs() != null) {
            for (String ref : operation.getReferralURLs()) {
                result.addReferralURI(ref);
            }
        }
        if (operation.getResponseControls() != null) {
            for (org.opends.server.types.Control c : operation.getResponseControls()) {
                result.addControl(from(c));
            }
        }
        result.setDiagnosticMessage((operation.getErrorMessage() != null ? operation
                .getErrorMessage().toString() : null));
        result.setMatchedDN((operation.getMatchedDN() != null) ? operation.getMatchedDN()
                .toString() : null);
        if (result.isSuccess()) {
            return result;
        } else {
            throw ErrorResultException.newErrorResult(result);
        }
    }
 
    /**
     * Converts the OpenDJ server {@link Operation} object into an OpenDJ LDAP SDK
     * {@link Result} object.
     *
     * @param operation
     *          value to convert
     * @return the converted value
     * @throws ErrorResultException
     *           when an error occurs
     */
    static final Result getResponseResult(final Operation operation) throws ErrorResultException {
        Result result = Responses.newResult(getResultCode(operation));
        return getResponseResult(operation, result);
    }
 
    /**
     * Returns the OpenDJ LDAP SDK {@link ResultCode} extracted out of the OpenDJ
     * server {@link Operation}.
     *
     * @param operation
     *          value to convert
     * @return the converted value
     */
    static final ResultCode getResultCode(final Operation operation) {
        return ResultCode.valueOf(operation.getResultCode().getIntValue());
    }
 
    /**
     * Converts from <code>byte[]</code> to OpenDJ server
     * {@link org.opends.server.types.ByteString}.
     *
     * @param authenticationValue
     *          value to convert
     * @return the converted value
     */
    static final org.opends.server.types.ByteString getCredentials(final byte[] authenticationValue) {
        final org.opends.server.protocols.asn1.ASN1Reader reader =
                ASN1.getReader(authenticationValue);
        org.opends.server.types.ByteString saslCred = org.opends.server.types.ByteString.empty();
        try {
            reader.readOctetStringAsString(); // Reads SASL Mechanism - RFC 4511 4.2
            if (reader.hasNextElement()) {
                saslCred = reader.readOctetString(); // Reads credentials.
            }
        } catch (ASN1Exception e) {
            // Nothing to do.
        }
 
        return saslCred.toByteString();
    }
 
}