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.LdapCodecConstants;
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;
64
65
66 private List<Integer> modificationLength;
67
68
69 private List<Integer> valuesLength;
70
71
72 private Attribute currentAttribute;
73
74
75 private ModificationOperation currentOperation;
76
77
78
79
80
81
82
83
84 public ModifyRequestDecorator( LdapApiService codec, ModifyRequest decoratedMessage )
85 {
86 super( codec, decoratedMessage );
87 }
88
89
90
91
92
93
94
95 public void setCurrentOperation( int currentOperation )
96 {
97 this.currentOperation = ModificationOperation.getOperation( currentOperation );
98 }
99
100
101
102
103
104
105
106 public void addAttributeTypeAndValues( String type )
107 {
108 currentAttribute = new DefaultAttribute( type );
109
110 Modification modification = new DefaultModification( currentOperation, currentAttribute );
111 getDecorated().addModification( modification );
112 }
113
114
115
116
117
118 public String getCurrentAttributeType()
119 {
120 return currentAttribute.getUpId();
121 }
122
123
124
125
126
127
128
129
130 public void addAttributeValue( byte[] value ) throws LdapException
131 {
132 currentAttribute.add( value );
133 }
134
135
136
137
138
139
140
141
142 public void addAttributeValue( String value ) throws LdapException
143 {
144 currentAttribute.add( value );
145 }
146
147
148
149
150
151
152
153
154
155 @Override
156 public Dn getName()
157 {
158 return getDecorated().getName();
159 }
160
161
162
163
164
165 @Override
166 public ModifyRequest setName( Dn name )
167 {
168 getDecorated().setName( name );
169
170 return this;
171 }
172
173
174
175
176
177 @Override
178 public Collection<Modification> getModifications()
179 {
180 return getDecorated().getModifications();
181 }
182
183
184
185
186
187 @Override
188 public ModifyRequest addModification( Modification mod )
189 {
190 getDecorated().addModification( mod );
191
192 return this;
193 }
194
195
196
197
198
199 @Override
200 public ModifyRequest removeModification( Modification mod )
201 {
202 getDecorated().removeModification( mod );
203
204 return this;
205 }
206
207
208
209
210
211 @Override
212 public ModifyRequest remove( String attributeName, String... attributeValue )
213 {
214 getDecorated().remove( attributeName, attributeValue );
215
216 return this;
217 }
218
219
220
221
222
223 public ModifyRequest remove( String attributeName, byte[]... attributeValue )
224 {
225 getDecorated().remove( attributeName, attributeValue );
226
227 return this;
228 }
229
230
231
232
233
234 @Override
235 public ModifyRequest remove( Attribute attr )
236 {
237 getDecorated().remove( attr );
238
239 return this;
240 }
241
242
243
244
245
246 @Override
247 public ModifyRequest remove( String attributeName )
248 {
249 getDecorated().remove( attributeName );
250
251 return this;
252 }
253
254
255
256
257
258 @Override
259 public ModifyRequest addModification( Attribute attr, ModificationOperation modOp )
260 {
261 getDecorated().addModification( attr, modOp );
262
263 return this;
264 }
265
266
267
268
269
270 @Override
271 public ModifyRequest add( String attributeName, String... attributeValue )
272 {
273 getDecorated().add( attributeName, attributeValue );
274
275 return this;
276 }
277
278
279
280
281
282 public ModifyRequest add( String attributeName, byte[]... attributeValue )
283 {
284 getDecorated().add( attributeName, attributeValue );
285
286 return this;
287 }
288
289
290
291
292
293 @Override
294 public ModifyRequest add( Attribute attr )
295 {
296 getDecorated().add( attr );
297
298 return this;
299 }
300
301
302
303
304
305 @Override
306 public ModifyRequest replace( String attributeName )
307 {
308 getDecorated().replace( attributeName );
309
310 return this;
311 }
312
313
314
315
316
317 @Override
318 public ModifyRequest replace( String attributeName, String... attributeValue )
319 {
320 getDecorated().replace( attributeName, attributeValue );
321
322 return this;
323 }
324
325
326
327
328
329 public ModifyRequest replace( String attributeName, byte[]... attributeValue )
330 {
331 getDecorated().replace( attributeName, attributeValue );
332
333 return this;
334 }
335
336
337
338
339
340 @Override
341 public ModifyRequest replace( Attribute attr )
342 {
343 getDecorated().replace( attr );
344
345 return this;
346 }
347
348
349
350
351
352 @Override
353 public ModifyRequest setMessageId( int messageId )
354 {
355 super.setMessageId( messageId );
356
357 return this;
358 }
359
360
361
362
363
364 @Override
365 public ModifyRequest addControl( Control control )
366 {
367 return ( ModifyRequest ) super.addControl( control );
368 }
369
370
371
372
373
374 @Override
375 public ModifyRequest addAllControls( Control[] controls )
376 {
377 return ( ModifyRequest ) super.addAllControls( controls );
378 }
379
380
381
382
383
384 @Override
385 public ModifyRequest removeControl( Control control )
386 {
387 return ( ModifyRequest ) super.removeControl( control );
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 @Override
437 public int computeLength()
438 {
439
440 modifyRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( getName() ) )
441 + Dn.getNbBytes( getName() );
442
443
444 changesLength = 0;
445
446 Collection<Modification> modifications = getModifications();
447
448 if ( ( modifications != null ) && ( !modifications.isEmpty() ) )
449 {
450 changeLength = new LinkedList<>();
451 modificationLength = new LinkedList<>();
452 valuesLength = new LinkedList<>();
453
454 for ( Modification modification : modifications )
455 {
456
457 int localModificationSequenceLength = 1 + 1 + 1;
458 int localValuesLength = 0;
459
460
461 int typeLength = modification.getAttribute().getUpId().length();
462 int localModificationLength = 1 + TLV.getNbBytes( typeLength ) + typeLength;
463
464
465 if ( modification.getAttribute().size() != 0 )
466 {
467 for ( Value<?> value : modification.getAttribute() )
468 {
469 localValuesLength += 1 + TLV.getNbBytes( value.getBytes().length ) + value.getBytes().length;
470 }
471 }
472
473 localModificationLength += 1 + TLV.getNbBytes( localValuesLength ) + localValuesLength;
474
475
476 localModificationSequenceLength += 1 + TLV.getNbBytes( localModificationLength )
477 + localModificationLength;
478
479
480 changesLength += 1 + TLV.getNbBytes( localModificationSequenceLength )
481 + localModificationSequenceLength;
482
483
484 valuesLength.add( localValuesLength );
485 modificationLength.add( localModificationLength );
486 changeLength.add( localModificationSequenceLength );
487 }
488
489
490 modifyRequestLength += 1 + TLV.getNbBytes( changesLength ) + changesLength;
491 }
492
493 return 1 + TLV.getNbBytes( modifyRequestLength ) + modifyRequestLength;
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 @Override
528 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
529 {
530 try
531 {
532
533 buffer.put( LdapCodecConstants.MODIFY_REQUEST_TAG );
534 buffer.put( TLV.getBytes( modifyRequestLength ) );
535
536
537 BerValue.encode( buffer, Dn.getBytes( getName() ) );
538
539
540 buffer.put( UniversalTag.SEQUENCE.getValue() );
541 buffer.put( TLV.getBytes( changesLength ) );
542
543
544 Collection<Modification> modifications = getModifications();
545
546 if ( ( modifications != null ) && ( !modifications.isEmpty() ) )
547 {
548 int modificationNumber = 0;
549
550
551 for ( Modification modification : modifications )
552 {
553
554 buffer.put( UniversalTag.SEQUENCE.getValue() );
555 int localModificationSequenceLength = changeLength.get( modificationNumber );
556 buffer.put( TLV.getBytes( localModificationSequenceLength ) );
557
558
559
560 buffer.put( UniversalTag.ENUMERATED.getValue() );
561 buffer.put( ( byte ) 1 );
562 buffer.put( ( byte ) modification.getOperation().getValue() );
563
564
565 buffer.put( UniversalTag.SEQUENCE.getValue() );
566 int localModificationLength = modificationLength.get( modificationNumber );
567 buffer.put( TLV.getBytes( localModificationLength ) );
568
569
570 BerValue.encode( buffer, modification.getAttribute().getUpId() );
571
572
573 buffer.put( UniversalTag.SET.getValue() );
574 int localValuesLength = valuesLength.get( modificationNumber );
575 buffer.put( TLV.getBytes( localValuesLength ) );
576
577 if ( modification.getAttribute().size() != 0 )
578 {
579 for ( org.apache.directory.api.ldap.model.entry.Value<?> value : modification.getAttribute() )
580 {
581 if ( value.isHumanReadable() )
582 {
583 BerValue.encode( buffer, value.getString() );
584 }
585 else
586 {
587 BerValue.encode( buffer, value.getBytes() );
588 }
589 }
590 }
591
592
593 modificationNumber++;
594 }
595 }
596 }
597 catch ( BufferOverflowException boe )
598 {
599 throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
600 }
601
602 return buffer;
603 }
604 }