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.model.message;
21
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.apache.directory.api.i18n.I18n;
30 import org.apache.directory.api.ldap.model.entry.Attribute;
31 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
32 import org.apache.directory.api.ldap.model.entry.DefaultModification;
33 import org.apache.directory.api.ldap.model.entry.Modification;
34 import org.apache.directory.api.ldap.model.entry.ModificationOperation;
35 import org.apache.directory.api.ldap.model.name.Dn;
36 import org.apache.directory.api.util.StringConstants;
37
38
39
40
41
42
43
44 public class ModifyRequestImpl extends AbstractAbandonableRequest implements ModifyRequest
45 {
46 static final long serialVersionUID = -505803669028990304L;
47
48
49 private Dn name;
50
51
52 private List<Modification> mods = new ArrayList<>();
53
54
55 private ModifyResponse response;
56
57
58
59
60
61
62
63
64
65 public ModifyRequestImpl()
66 {
67 super( -1, MessageTypeEnum.MODIFY_REQUEST );
68 }
69
70
71
72
73
74
75
76
77 @Override
78 public Collection<Modification> getModifications()
79 {
80 return Collections.unmodifiableCollection( mods );
81 }
82
83
84
85
86
87 @Override
88 public Dn getName()
89 {
90 return name;
91 }
92
93
94
95
96
97 @Override
98 public ModifyRequest setName( Dn name )
99 {
100 this.name = name;
101
102 return this;
103 }
104
105
106
107
108
109 @Override
110 public ModifyRequest addModification( Modification mod )
111 {
112 mods.add( mod );
113
114 return this;
115 }
116
117
118 private void addModification( ModificationOperation modOp, String attributeName, byte[]... attributeValue )
119 {
120 Attribute attr = new DefaultAttribute( attributeName, attributeValue );
121 addModification( attr, modOp );
122 }
123
124
125 private void addModification( ModificationOperation modOp, String attributeName, String... attributeValue )
126 {
127 Attribute attr = new DefaultAttribute( attributeName, attributeValue );
128 addModification( attr, modOp );
129 }
130
131
132
133
134
135 @Override
136 public ModifyRequest addModification( Attribute attr, ModificationOperation modOp )
137 {
138 mods.add( new DefaultModification( modOp, attr ) );
139
140 return this;
141 }
142
143
144
145
146
147 @Override
148 public ModifyRequest add( String attributeName, String... attributeValue )
149 {
150 addModification( ModificationOperation.ADD_ATTRIBUTE, attributeName, attributeValue );
151
152 return this;
153 }
154
155
156
157
158
159 public ModifyRequest add( String attributeName, byte[]... attributeValue )
160 {
161 addModification( ModificationOperation.ADD_ATTRIBUTE, attributeName, attributeValue );
162
163 return this;
164 }
165
166
167
168
169
170 @Override
171 public ModifyRequest add( Attribute attr )
172 {
173 addModification( attr, ModificationOperation.ADD_ATTRIBUTE );
174
175 return this;
176 }
177
178
179
180
181
182 @Override
183 public ModifyRequest replace( String attributeName )
184 {
185 addModification( ModificationOperation.REPLACE_ATTRIBUTE, attributeName, StringConstants.EMPTY_STRINGS );
186
187 return this;
188 }
189
190
191
192
193
194 @Override
195 public ModifyRequest replace( String attributeName, String... attributeValue )
196 {
197 addModification( ModificationOperation.REPLACE_ATTRIBUTE, attributeName, attributeValue );
198
199 return this;
200 }
201
202
203
204
205
206 public ModifyRequest replace( String attributeName, byte[]... attributeValue )
207 {
208 addModification( ModificationOperation.REPLACE_ATTRIBUTE, attributeName, attributeValue );
209
210 return this;
211 }
212
213
214
215
216
217 @Override
218 public ModifyRequest replace( Attribute attr )
219 {
220 addModification( attr, ModificationOperation.REPLACE_ATTRIBUTE );
221
222 return this;
223 }
224
225
226
227
228
229 @Override
230 public ModifyRequest removeModification( Modification mod )
231 {
232 mods.remove( mod );
233
234 return this;
235 }
236
237
238
239
240
241 @Override
242 public ModifyRequest remove( String attributeName, String... attributeValue )
243 {
244 addModification( ModificationOperation.REMOVE_ATTRIBUTE, attributeName, attributeValue );
245
246 return this;
247 }
248
249
250
251
252
253 public ModifyRequest remove( String attributeName, byte[]... attributeValue )
254 {
255 addModification( ModificationOperation.REMOVE_ATTRIBUTE, attributeName, attributeValue );
256
257 return this;
258 }
259
260
261
262
263
264 @Override
265 public ModifyRequest remove( Attribute attr )
266 {
267 addModification( attr, ModificationOperation.REMOVE_ATTRIBUTE );
268
269 return this;
270 }
271
272
273
274
275
276 @Override
277 public ModifyRequest remove( String attributeName )
278 {
279 addModification( new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, attributeName ) );
280
281 return this;
282 }
283
284
285
286
287
288 @Override
289 public ModifyRequest increment( String attributeName )
290 {
291 addModification( new DefaultModification( ModificationOperation.INCREMENT_ATTRIBUTE, attributeName ) );
292
293 return this;
294 }
295
296
297
298
299
300 @Override
301 public ModifyRequest increment( String attributeName, int increment )
302 {
303 addModification( new DefaultModification( ModificationOperation.INCREMENT_ATTRIBUTE, attributeName,
304 Integer.toString( increment ) ) );
305
306 return this;
307 }
308
309
310
311
312
313 @Override
314 public ModifyRequest increment( Attribute attr )
315 {
316 addModification( attr, ModificationOperation.INCREMENT_ATTRIBUTE );
317
318 return this;
319 }
320
321
322
323
324
325 @Override
326 public ModifyRequest increment( Attribute attr, int increment )
327 {
328 addModification( new DefaultModification( ModificationOperation.INCREMENT_ATTRIBUTE, attr.getId(),
329 Integer.toString( increment ) ) );
330
331 return this;
332 }
333
334
335
336
337
338 @Override
339 public ModifyRequest setMessageId( int messageId )
340 {
341 super.setMessageId( messageId );
342
343 return this;
344 }
345
346
347
348
349
350 @Override
351 public ModifyRequest addControl( Control control )
352 {
353 return ( ModifyRequest ) super.addControl( control );
354 }
355
356
357
358
359
360 @Override
361 public ModifyRequest addAllControls( Control[] controls )
362 {
363 return ( ModifyRequest ) super.addAllControls( controls );
364 }
365
366
367
368
369
370 @Override
371 public ModifyRequest removeControl( Control control )
372 {
373 return ( ModifyRequest ) super.removeControl( control );
374 }
375
376
377
378
379
380
381
382
383
384
385
386
387 @Override
388 public MessageTypeEnum getResponseType()
389 {
390 return MessageTypeEnum.MODIFY_RESPONSE;
391 }
392
393
394
395
396
397
398
399 @Override
400 public ModifyResponse getResultResponse()
401 {
402 if ( response == null )
403 {
404 response = new ModifyResponseImpl( getMessageId() );
405 }
406
407 return response;
408 }
409
410
411
412
413
414 @Override
415 public int hashCode()
416 {
417 int hash = 37;
418 if ( name != null )
419 {
420 hash = hash * 17 + name.hashCode();
421 }
422 hash = hash * 17 + mods.size();
423 for ( int i = 0; i < mods.size(); i++ )
424 {
425 hash = hash * 17 + ( ( DefaultModification ) mods.get( i ) ).hashCode();
426 }
427 hash = hash * 17 + super.hashCode();
428
429 return hash;
430 }
431
432
433
434
435
436
437
438
439
440
441 @Override
442 public boolean equals( Object obj )
443 {
444 if ( obj == this )
445 {
446 return true;
447 }
448
449 if ( !super.equals( obj ) )
450 {
451 return false;
452 }
453
454 ModifyRequest req = ( ModifyRequest ) obj;
455
456 if ( name != null && req.getName() == null )
457 {
458 return false;
459 }
460
461 if ( name == null && req.getName() != null )
462 {
463 return false;
464 }
465
466 if ( name != null && req.getName() != null && !name.equals( req.getName() ) )
467 {
468 return false;
469 }
470
471 if ( req.getModifications().size() != mods.size() )
472 {
473 return false;
474 }
475
476 Iterator<Modification> list = req.getModifications().iterator();
477
478 for ( int i = 0; i < mods.size(); i++ )
479 {
480 Modification item = list.next();
481
482 if ( item == null )
483 {
484 if ( mods.get( i ) != null )
485 {
486 return false;
487 }
488 }
489 else
490
491 if ( !item.equals( mods.get( i ) ) )
492 {
493 return false;
494 }
495 }
496
497 return true;
498 }
499
500
501
502
503
504
505
506 @Override
507 public String toString()
508 {
509
510 StringBuilder sb = new StringBuilder();
511
512 sb.append( " Modify Request\n" );
513 sb.append( " Object : '" ).append( name ).append( "'\n" );
514
515 if ( mods != null )
516 {
517 sb.append( " Modifications : \n" );
518
519 for ( int i = 0; i < mods.size(); i++ )
520 {
521
522 DefaultModification modification = ( DefaultModification ) mods.get( i );
523
524 sb.append( " Modification[" ).append( i ).append( "]\n" );
525 sb.append( " Operation : " );
526
527 switch ( modification.getOperation() )
528 {
529 case ADD_ATTRIBUTE:
530 sb.append( " add\n" );
531 break;
532
533 case REPLACE_ATTRIBUTE:
534 sb.append( " replace\n" );
535 break;
536
537 case REMOVE_ATTRIBUTE:
538 sb.append( " delete\n" );
539 break;
540
541 default:
542 throw new IllegalArgumentException( I18n.err( I18n.ERR_13515_UNEXPECTED_MOD_OP, modification.getOperation() ) );
543 }
544
545 sb.append( " Modification : " );
546 sb.append( modification.getAttribute() );
547 sb.append( "\n" );
548 }
549 }
550
551
552
553
554 return super.toString( sb.toString() );
555 }
556 }