1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.codec.decorators;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25 import java.util.Collection;
26 import java.util.LinkedList;
27 import java.util.List;
28
29 import org.apache.directory.api.asn1.EncoderException;
30 import org.apache.directory.api.asn1.ber.tlv.BerValue;
31 import org.apache.directory.api.asn1.ber.tlv.TLV;
32 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
33 import org.apache.directory.api.i18n.I18n;
34 import org.apache.directory.api.ldap.codec.api.LdapApiService;
35 import org.apache.directory.api.ldap.codec.api.LdapConstants;
36 import org.apache.directory.api.ldap.model.entry.Attribute;
37 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
38 import org.apache.directory.api.ldap.model.entry.DefaultModification;
39 import org.apache.directory.api.ldap.model.entry.Modification;
40 import org.apache.directory.api.ldap.model.entry.ModificationOperation;
41 import org.apache.directory.api.ldap.model.entry.Value;
42 import org.apache.directory.api.ldap.model.exception.LdapException;
43 import org.apache.directory.api.ldap.model.message.Control;
44 import org.apache.directory.api.ldap.model.message.ModifyRequest;
45 import org.apache.directory.api.ldap.model.name.Dn;
46
47
48
49
50
51
52
53 public class ModifyRequestDecorator extends SingleReplyRequestDecorator<ModifyRequest>
54 implements ModifyRequest
55 {
56
57 private int modifyRequestLength;
58
59
60 private int changesLength;
61
62
63 private List<Integer> changeLength = new LinkedList<Integer>();
64
65
66 private List<Integer> modificationLength = new LinkedList<Integer>();
67
68
69 private List<Integer> valuesLength = new LinkedList<Integer>();
70
71
72 private Attribute currentAttribute;
73
74
75 private ModificationOperation currentOperation;
76
77
78
79
80
81
82
83 public ModifyRequestDecorator( LdapApiService codec, ModifyRequest decoratedMessage )
84 {
85 super( codec, decoratedMessage );
86 }
87
88
89
90
91
92 public void setModifyRequestLength( int modifyRequestLength )
93 {
94 this.modifyRequestLength = modifyRequestLength;
95 }
96
97
98
99
100
101 public int getModifyRequestLength()
102 {
103 return modifyRequestLength;
104 }
105
106
107
108
109
110 public void setChangesLength( int changesLength )
111 {
112 this.changesLength = changesLength;
113 }
114
115
116
117
118
119 public int getChangesLength()
120 {
121 return changesLength;
122 }
123
124
125
126
127
128 public void setChangeLength( List<Integer> changeLength )
129 {
130 this.changeLength = changeLength;
131 }
132
133
134
135
136
137 public List<Integer> getChangeLength()
138 {
139 return changeLength;
140 }
141
142
143
144
145
146 public void setModificationLength( List<Integer> modificationLength )
147 {
148 this.modificationLength = modificationLength;
149 }
150
151
152
153
154
155 public List<Integer> getModificationLength()
156 {
157 return modificationLength;
158 }
159
160
161
162
163
164 public void setValuesLength( List<Integer> valuesLength )
165 {
166 this.valuesLength = valuesLength;
167 }
168
169
170
171
172
173 public List<Integer> getValuesLength()
174 {
175 return valuesLength;
176 }
177
178
179
180
181
182
183
184 public void setCurrentOperation( int currentOperation )
185 {
186 this.currentOperation = ModificationOperation.getOperation( currentOperation );
187 }
188
189
190
191
192
193
194
195 public void addAttributeTypeAndValues( String type )
196 {
197 currentAttribute = new DefaultAttribute( type );
198
199 Modification modification = new DefaultModification( currentOperation, currentAttribute );
200 getDecorated().addModification( modification );
201 }
202
203
204
205
206
207 public String getCurrentAttributeType()
208 {
209 return currentAttribute.getUpId();
210 }
211
212
213
214
215
216
217
218 public void addAttributeValue( byte[] value ) throws LdapException
219 {
220 currentAttribute.add( value );
221 }
222
223
224
225
226
227
228
229 public void addAttributeValue( String value ) throws LdapException
230 {
231 currentAttribute.add( value );
232 }
233
234
235
236
237
238
239
240
241
242 public Dn getName()
243 {
244 return getDecorated().getName();
245 }
246
247
248
249
250
251 public ModifyRequest setName( Dn name )
252 {
253 getDecorated().setName( name );
254
255 return this;
256 }
257
258
259
260
261
262 public Collection<Modification> getModifications()
263 {
264 return getDecorated().getModifications();
265 }
266
267
268
269
270
271 public ModifyRequest addModification( Modification mod )
272 {
273 getDecorated().addModification( mod );
274
275 return this;
276 }
277
278
279
280
281
282 public ModifyRequest removeModification( Modification mod )
283 {
284 getDecorated().removeModification( mod );
285
286 return this;
287 }
288
289
290
291
292
293 public ModifyRequest remove( String attributeName, String... attributeValue )
294 {
295 getDecorated().remove( attributeName, attributeValue );
296
297 return this;
298 }
299
300
301
302
303
304 public ModifyRequest remove( String attributeName, byte[]... attributeValue )
305 {
306 getDecorated().remove( attributeName, attributeValue );
307
308 return this;
309 }
310
311
312
313
314
315 public ModifyRequest remove( Attribute attr )
316 {
317 getDecorated().remove( attr );
318
319 return this;
320 }
321
322
323
324
325
326 public ModifyRequest addModification( Attribute attr, ModificationOperation modOp )
327 {
328 getDecorated().addModification( attr, modOp );
329
330 return this;
331 }
332
333
334
335
336
337 public ModifyRequest add( String attributeName, String... attributeValue )
338 {
339 getDecorated().add( attributeName, attributeValue );
340
341 return this;
342 }
343
344
345
346
347
348 public ModifyRequest add( String attributeName, byte[]... attributeValue )
349 {
350 getDecorated().add( attributeName, attributeValue );
351
352 return this;
353 }
354
355
356
357
358
359 public ModifyRequest add( Attribute attr )
360 {
361 getDecorated().add( attr );
362
363 return this;
364 }
365
366
367
368
369
370 public ModifyRequest replace( String attributeName )
371 {
372 getDecorated().replace( attributeName );
373
374 return this;
375 }
376
377
378
379
380
381 public ModifyRequest replace( String attributeName, String... attributeValue )
382 {
383 getDecorated().replace( attributeName, attributeValue );
384
385 return this;
386 }
387
388
389
390
391
392 public ModifyRequest replace( String attributeName, byte[]... attributeValue )
393 {
394 getDecorated().replace( attributeName, attributeValue );
395
396 return this;
397 }
398
399
400
401
402
403 public ModifyRequest replace( Attribute attr )
404 {
405 getDecorated().replace( attr );
406
407 return this;
408 }
409
410
411
412
413
414 public ModifyRequest setMessageId( int messageId )
415 {
416 super.setMessageId( messageId );
417
418 return this;
419 }
420
421
422
423
424
425 public ModifyRequest addControl( Control control )
426 {
427 return ( ModifyRequest ) super.addControl( control );
428 }
429
430
431
432
433
434 public ModifyRequest addAllControls( Control[] controls )
435 {
436 return ( ModifyRequest ) super.addAllControls( controls );
437 }
438
439
440
441
442
443 public ModifyRequest removeControl( Control control )
444 {
445 return ( ModifyRequest ) super.removeControl( control );
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 public int computeLength()
494 {
495
496 int modifyRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( getName() ) )
497 + Dn.getNbBytes( getName() );
498
499
500 int changesLength = 0;
501
502 Collection<Modification> modifications = getModifications();
503
504 if ( ( modifications != null ) && ( modifications.size() != 0 ) )
505 {
506 List<Integer> changeLength = new LinkedList<Integer>();
507 List<Integer> modificationLength = new LinkedList<Integer>();
508 List<Integer> valuesLength = new LinkedList<Integer>();
509
510 for ( Modification modification : modifications )
511 {
512
513 int localModificationSequenceLength = 1 + 1 + 1;
514 int localValuesLength = 0;
515
516
517 int typeLength = modification.getAttribute().getUpId().length();
518 int localModificationLength = 1 + TLV.getNbBytes( typeLength ) + typeLength;
519
520
521 if ( modification.getAttribute().size() != 0 )
522 {
523 for ( Value<?> value : modification.getAttribute() )
524 {
525 localValuesLength += 1 + TLV.getNbBytes( value.getBytes().length ) + value.getBytes().length;
526 }
527 }
528
529 localModificationLength += 1 + TLV.getNbBytes( localValuesLength ) + localValuesLength;
530
531
532 localModificationSequenceLength += 1 + TLV.getNbBytes( localModificationLength )
533 + localModificationLength;
534
535
536 changesLength += 1 + TLV.getNbBytes( localModificationSequenceLength )
537 + localModificationSequenceLength;
538
539
540 valuesLength.add( localValuesLength );
541 modificationLength.add( localModificationLength );
542 changeLength.add( localModificationSequenceLength );
543 }
544
545
546 modifyRequestLength += 1 + TLV.getNbBytes( changesLength ) + changesLength;
547 setChangeLength( changeLength );
548 setModificationLength( modificationLength );
549 setValuesLength( valuesLength );
550 }
551
552 setChangesLength( changesLength );
553 setModifyRequestLength( modifyRequestLength );
554
555 return 1 + TLV.getNbBytes( modifyRequestLength ) + modifyRequestLength;
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 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
590 {
591 try
592 {
593
594 buffer.put( LdapConstants.MODIFY_REQUEST_TAG );
595 buffer.put( TLV.getBytes( getModifyRequestLength() ) );
596
597
598 BerValue.encode( buffer, Dn.getBytes( getName() ) );
599
600
601 buffer.put( UniversalTag.SEQUENCE.getValue() );
602 buffer.put( TLV.getBytes( getChangesLength() ) );
603
604
605 Collection<Modification> modifications = getModifications();
606
607 if ( ( modifications != null ) && ( modifications.size() != 0 ) )
608 {
609 int modificationNumber = 0;
610
611
612 for ( Modification modification : modifications )
613 {
614
615 buffer.put( UniversalTag.SEQUENCE.getValue() );
616 int localModificationSequenceLength = getChangeLength().get( modificationNumber );
617 buffer.put( TLV.getBytes( localModificationSequenceLength ) );
618
619
620
621 buffer.put( UniversalTag.ENUMERATED.getValue() );
622 buffer.put( ( byte ) 1 );
623 buffer.put( ( byte ) modification.getOperation().getValue() );
624
625
626 buffer.put( UniversalTag.SEQUENCE.getValue() );
627 int localModificationLength = getModificationLength().get( modificationNumber );
628 buffer.put( TLV.getBytes( localModificationLength ) );
629
630
631 BerValue.encode( buffer, modification.getAttribute().getUpId() );
632
633
634 buffer.put( UniversalTag.SET.getValue() );
635 int localValuesLength = getValuesLength().get( modificationNumber );
636 buffer.put( TLV.getBytes( localValuesLength ) );
637
638 if ( modification.getAttribute().size() != 0 )
639 {
640 for ( org.apache.directory.api.ldap.model.entry.Value<?> value : modification.getAttribute() )
641 {
642 if ( value.isHumanReadable() )
643 {
644 BerValue.encode( buffer, value.getString() );
645 }
646 else
647 {
648 BerValue.encode( buffer, value.getBytes() );
649 }
650 }
651 }
652
653
654 modificationNumber++;
655 }
656 }
657 }
658 catch ( BufferOverflowException boe )
659 {
660 throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
661 }
662
663 return buffer;
664 }
665 }