forked from pingidentity/ldapsdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LDIFModifyChangeRecord.java
690 lines (572 loc) · 18.9 KB
/
LDIFModifyChangeRecord.java
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
* Copyright 2007-2023 Ping Identity Corporation
* All Rights Reserved.
*/
/*
* Copyright 2007-2023 Ping Identity Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright (C) 2007-2023 Ping Identity Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (GPLv2 only)
* or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
package com.unboundid.ldif;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import com.unboundid.asn1.ASN1OctetString;
import com.unboundid.ldap.sdk.ChangeType;
import com.unboundid.ldap.sdk.Control;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.LDAPInterface;
import com.unboundid.ldap.sdk.LDAPResult;
import com.unboundid.ldap.sdk.Modification;
import com.unboundid.ldap.sdk.ModifyRequest;
import com.unboundid.util.ByteStringBuffer;
import com.unboundid.util.Debug;
import com.unboundid.util.NotMutable;
import com.unboundid.util.NotNull;
import com.unboundid.util.Nullable;
import com.unboundid.util.StaticUtils;
import com.unboundid.util.ThreadSafety;
import com.unboundid.util.ThreadSafetyLevel;
import com.unboundid.util.Validator;
/**
* This class defines an LDIF modify change record, which can be used to
* represent an LDAP modify request. See the documentation for the
* {@link LDIFChangeRecord} class for an example demonstrating the process for
* interacting with LDIF change records.
*/
@NotMutable()
@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
public final class LDIFModifyChangeRecord
extends LDIFChangeRecord
{
/**
* The name of the system property that will be used to indicate whether
* to always include a trailing dash after the last change in the LDIF
* representation of a modify change record. By default, the dash will always
* be included.
*/
@NotNull public static final String PROPERTY_ALWAYS_INCLUDE_TRAILING_DASH =
"com.unboundid.ldif.modify.alwaysIncludeTrailingDash";
/**
* Indicates whether to always include a trailing dash after the last change
* in the LDIF representation.
*/
private static boolean alwaysIncludeTrailingDash = true;
static
{
final String propValue =
StaticUtils.getSystemProperty(PROPERTY_ALWAYS_INCLUDE_TRAILING_DASH);
if ((propValue != null) && (propValue.equalsIgnoreCase("false")))
{
alwaysIncludeTrailingDash = false;
}
}
/**
* The serial version UID for this serializable class.
*/
private static final long serialVersionUID = -7558098319600288036L;
// The set of modifications for this modify change record.
@NotNull private final Modification[] modifications;
/**
* Creates a new LDIF modify change record with the provided DN and set of
* modifications.
*
* @param dn The DN for this LDIF add change record. It must not
* be {@code null}.
* @param modifications The set of modifications for this LDIF modify change
* record. It must not be {@code null} or empty.
*/
public LDIFModifyChangeRecord(@NotNull final String dn,
@NotNull final Modification... modifications)
{
this(dn, modifications, null);
}
/**
* Creates a new LDIF modify change record with the provided DN and set of
* modifications.
*
* @param dn The DN for this LDIF add change record. It must not
* be {@code null}.
* @param modifications The set of modifications for this LDIF modify change
* record. It must not be {@code null} or empty.
* @param controls The set of controls for this LDIF modify change
* record. It may be {@code null} or empty if there
* are no controls.
*/
public LDIFModifyChangeRecord(@NotNull final String dn,
@NotNull final Modification[] modifications,
@Nullable final List<Control> controls)
{
super(dn, controls);
Validator.ensureNotNull(modifications);
Validator.ensureTrue(modifications.length > 0,
"LDIFModifyChangeRecord.modifications must not be empty.");
this.modifications = modifications;
}
/**
* Creates a new LDIF modify change record with the provided DN and set of
* modifications.
*
* @param dn The DN for this LDIF add change record. It must not
* be {@code null}.
* @param modifications The set of modifications for this LDIF modify change
* record. It must not be {@code null} or empty.
*/
public LDIFModifyChangeRecord(@NotNull final String dn,
@NotNull final List<Modification> modifications)
{
this(dn, modifications, null);
}
/**
* Creates a new LDIF modify change record with the provided DN and set of
* modifications.
*
* @param dn The DN for this LDIF add change record. It must not
* be {@code null}.
* @param modifications The set of modifications for this LDIF modify change
* record. It must not be {@code null} or empty.
* @param controls The set of controls for this LDIF modify change
* record. It may be {@code null} or empty if there
* are no controls.
*/
public LDIFModifyChangeRecord(@NotNull final String dn,
@NotNull final List<Modification> modifications,
@Nullable final List<Control> controls)
{
super(dn, controls);
Validator.ensureNotNull(modifications);
Validator.ensureFalse(modifications.isEmpty(),
"LDIFModifyChangeRecord.modifications must not be empty.");
this.modifications = new Modification[modifications.size()];
modifications.toArray(this.modifications);
}
/**
* Creates a new LDIF modify change record from the provided modify request.
*
* @param modifyRequest The modify request to use to create this LDIF modify
* change record. It must not be {@code null}.
*/
public LDIFModifyChangeRecord(@NotNull final ModifyRequest modifyRequest)
{
super(modifyRequest.getDN(), modifyRequest.getControlList());
final List<Modification> mods = modifyRequest.getModifications();
modifications = new Modification[mods.size()];
final Iterator<Modification> iterator = mods.iterator();
for (int i=0; i < modifications.length; i++)
{
modifications[i] = iterator.next();
}
}
/**
* Indicates whether the LDIF representation of a modify change record should
* always include a trailing dash after the last (or only) change.
*
* @return {@code true} if the LDIF representation of a modify change record
* should always include a trailing dash after the last (or only)
* change, or {@code false} if not.
*/
public static boolean alwaysIncludeTrailingDash()
{
return alwaysIncludeTrailingDash;
}
/**
* Specifies whether the LDIF representation of a modify change record should
* always include a trailing dash after the last (or only) change.
*
* @param alwaysIncludeTrailingDash Indicates whether the LDIF
* representation of a modify change record
* should always include a trailing dash
* after the last (or only) change.
*/
public static void setAlwaysIncludeTrailingDash(
final boolean alwaysIncludeTrailingDash)
{
LDIFModifyChangeRecord.alwaysIncludeTrailingDash =
alwaysIncludeTrailingDash;
}
/**
* Retrieves the set of modifications for this modify change record.
*
* @return The set of modifications for this modify change record.
*/
@NotNull()
public Modification[] getModifications()
{
return modifications;
}
/**
* Creates a modify request from this LDIF modify change record. Any change
* record controls will be included in the request
*
* @return The modify request created from this LDIF modify change record.
*/
@NotNull()
public ModifyRequest toModifyRequest()
{
return toModifyRequest(true);
}
/**
* Creates a modify request from this LDIF modify change record, optionally
* including any change record controls in the request.
*
* @param includeControls Indicates whether to include any controls in the
* request.
*
* @return The modify request created from this LDIF modify change record.
*/
@NotNull()
public ModifyRequest toModifyRequest(final boolean includeControls)
{
final ModifyRequest modifyRequest =
new ModifyRequest(getDN(), modifications);
if (includeControls)
{
modifyRequest.setControls(getControls());
}
return modifyRequest;
}
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public ChangeType getChangeType()
{
return ChangeType.MODIFY;
}
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public LDIFModifyChangeRecord duplicate(@Nullable final Control... controls)
{
return new LDIFModifyChangeRecord(getDN(), modifications,
StaticUtils.toList(controls));
}
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public LDAPResult processChange(@NotNull final LDAPInterface connection,
final boolean includeControls)
throws LDAPException
{
return connection.modify(toModifyRequest(includeControls));
}
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public String[] toLDIF(final int wrapColumn)
{
List<String> ldifLines = new ArrayList<>(modifications.length*4);
encodeNameAndValue("dn", new ASN1OctetString(getDN()), ldifLines);
for (final Control c : getControls())
{
encodeNameAndValue("control", encodeControlString(c), ldifLines);
}
ldifLines.add("changetype: modify");
for (int i=0; i < modifications.length; i++)
{
final String attrName = modifications[i].getAttributeName();
switch (modifications[i].getModificationType().intValue())
{
case 0:
ldifLines.add("add: " + attrName);
break;
case 1:
ldifLines.add("delete: " + attrName);
break;
case 2:
ldifLines.add("replace: " + attrName);
break;
case 3:
ldifLines.add("increment: " + attrName);
break;
default:
// This should never happen.
continue;
}
for (final ASN1OctetString value : modifications[i].getRawValues())
{
encodeNameAndValue(attrName, value, ldifLines);
}
if (alwaysIncludeTrailingDash || (i < (modifications.length - 1)))
{
ldifLines.add("-");
}
}
if (wrapColumn > 2)
{
ldifLines = LDIFWriter.wrapLines(wrapColumn, ldifLines);
}
final String[] ldifArray = new String[ldifLines.size()];
ldifLines.toArray(ldifArray);
return ldifArray;
}
/**
* {@inheritDoc}
*/
@Override()
public void toLDIF(@NotNull final ByteStringBuffer buffer,
final int wrapColumn)
{
LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN()), buffer,
wrapColumn);
buffer.append(StaticUtils.EOL_BYTES);
for (final Control c : getControls())
{
LDIFWriter.encodeNameAndValue("control", encodeControlString(c), buffer,
wrapColumn);
buffer.append(StaticUtils.EOL_BYTES);
}
LDIFWriter.encodeNameAndValue("changetype", new ASN1OctetString("modify"),
buffer, wrapColumn);
buffer.append(StaticUtils.EOL_BYTES);
for (int i=0; i < modifications.length; i++)
{
final String attrName = modifications[i].getAttributeName();
switch (modifications[i].getModificationType().intValue())
{
case 0:
LDIFWriter.encodeNameAndValue("add", new ASN1OctetString(attrName),
buffer, wrapColumn);
buffer.append(StaticUtils.EOL_BYTES);
break;
case 1:
LDIFWriter.encodeNameAndValue("delete", new ASN1OctetString(attrName),
buffer, wrapColumn);
buffer.append(StaticUtils.EOL_BYTES);
break;
case 2:
LDIFWriter.encodeNameAndValue("replace",
new ASN1OctetString(attrName), buffer,
wrapColumn);
buffer.append(StaticUtils.EOL_BYTES);
break;
case 3:
LDIFWriter.encodeNameAndValue("increment",
new ASN1OctetString(attrName), buffer,
wrapColumn);
buffer.append(StaticUtils.EOL_BYTES);
break;
default:
// This should never happen.
continue;
}
for (final ASN1OctetString value : modifications[i].getRawValues())
{
LDIFWriter.encodeNameAndValue(attrName, value, buffer, wrapColumn);
buffer.append(StaticUtils.EOL_BYTES);
}
if (alwaysIncludeTrailingDash || (i < (modifications.length - 1)))
{
buffer.append('-');
buffer.append(StaticUtils.EOL_BYTES);
}
}
}
/**
* {@inheritDoc}
*/
@Override()
public void toLDIFString(@NotNull final StringBuilder buffer,
final int wrapColumn)
{
LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(getDN()), buffer,
wrapColumn);
buffer.append(StaticUtils.EOL);
for (final Control c : getControls())
{
LDIFWriter.encodeNameAndValue("control", encodeControlString(c), buffer,
wrapColumn);
buffer.append(StaticUtils.EOL);
}
LDIFWriter.encodeNameAndValue("changetype", new ASN1OctetString("modify"),
buffer, wrapColumn);
buffer.append(StaticUtils.EOL);
for (int i=0; i < modifications.length; i++)
{
final String attrName = modifications[i].getAttributeName();
switch (modifications[i].getModificationType().intValue())
{
case 0:
LDIFWriter.encodeNameAndValue("add", new ASN1OctetString(attrName),
buffer, wrapColumn);
buffer.append(StaticUtils.EOL);
break;
case 1:
LDIFWriter.encodeNameAndValue("delete", new ASN1OctetString(attrName),
buffer, wrapColumn);
buffer.append(StaticUtils.EOL);
break;
case 2:
LDIFWriter.encodeNameAndValue("replace",
new ASN1OctetString(attrName), buffer,
wrapColumn);
buffer.append(StaticUtils.EOL);
break;
case 3:
LDIFWriter.encodeNameAndValue("increment",
new ASN1OctetString(attrName), buffer,
wrapColumn);
buffer.append(StaticUtils.EOL);
break;
default:
// This should never happen.
continue;
}
for (final ASN1OctetString value : modifications[i].getRawValues())
{
LDIFWriter.encodeNameAndValue(attrName, value, buffer, wrapColumn);
buffer.append(StaticUtils.EOL);
}
if (alwaysIncludeTrailingDash || (i < (modifications.length - 1)))
{
buffer.append('-');
buffer.append(StaticUtils.EOL);
}
}
}
/**
* {@inheritDoc}
*/
@Override()
public int hashCode()
{
int hashCode;
try
{
hashCode = getParsedDN().hashCode();
}
catch (final Exception e)
{
Debug.debugException(e);
hashCode = StaticUtils.toLowerCase(getDN()).hashCode();
}
for (final Modification m : modifications)
{
hashCode += m.hashCode();
}
return hashCode;
}
/**
* {@inheritDoc}
*/
@Override()
public boolean equals(@Nullable final Object o)
{
if (o == null)
{
return false;
}
if (o == this)
{
return true;
}
if (! (o instanceof LDIFModifyChangeRecord))
{
return false;
}
final LDIFModifyChangeRecord r = (LDIFModifyChangeRecord) o;
final HashSet<Control> c1 = new HashSet<>(getControls());
final HashSet<Control> c2 = new HashSet<>(r.getControls());
if (! c1.equals(c2))
{
return false;
}
try
{
if (! getParsedDN().equals(r.getParsedDN()))
{
return false;
}
}
catch (final Exception e)
{
Debug.debugException(e);
if (! StaticUtils.toLowerCase(getDN()).equals(
StaticUtils.toLowerCase(r.getDN())))
{
return false;
}
}
if (modifications.length != r.modifications.length)
{
return false;
}
for (int i=0; i < modifications.length; i++)
{
if (! modifications[i].equals(r.modifications[i]))
{
return false;
}
}
return true;
}
/**
* {@inheritDoc}
*/
@Override()
public void toString(@NotNull final StringBuilder buffer)
{
buffer.append("LDIFModifyChangeRecord(dn='");
buffer.append(getDN());
buffer.append("', mods={");
for (int i=0; i < modifications.length; i++)
{
if (i > 0)
{
buffer.append(", ");
}
modifications[i].toString(buffer);
}
buffer.append('}');
final List<Control> controls = getControls();
if (! controls.isEmpty())
{
buffer.append(", controls={");
final Iterator<Control> iterator = controls.iterator();
while (iterator.hasNext())
{
iterator.next().toString(buffer);
if (iterator.hasNext())
{
buffer.append(',');
}
}
buffer.append('}');
}
buffer.append(')');
}
}