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