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.dsmlv2.request;
21
22
23 import java.util.Collection;
24
25 import org.apache.directory.api.dsmlv2.ParserUtils;
26 import org.apache.directory.api.ldap.codec.api.LdapApiService;
27 import org.apache.directory.api.ldap.model.entry.Attribute;
28 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
29 import org.apache.directory.api.ldap.model.entry.DefaultModification;
30 import org.apache.directory.api.ldap.model.entry.Modification;
31 import org.apache.directory.api.ldap.model.entry.ModificationOperation;
32 import org.apache.directory.api.ldap.model.entry.Value;
33 import org.apache.directory.api.ldap.model.exception.LdapException;
34 import org.apache.directory.api.ldap.model.message.Control;
35 import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
36 import org.apache.directory.api.ldap.model.message.ModifyRequest;
37 import org.apache.directory.api.ldap.model.message.ModifyRequestImpl;
38 import org.apache.directory.api.ldap.model.message.ModifyResponse;
39 import org.apache.directory.api.ldap.model.name.Dn;
40 import org.dom4j.Element;
41 import org.dom4j.Namespace;
42 import org.dom4j.QName;
43
44
45
46
47
48
49
50 public class ModifyRequestDsml
51 extends AbstractResultResponseRequestDsml<ModifyRequest, ModifyResponse>
52 implements ModifyRequest
53 {
54
55
56 private Attribute currentAttribute;
57
58
59 private ModificationOperation currentOperation;
60
61
62
63
64
65
66
67 public ModifyRequestDsml( LdapApiService codec )
68 {
69 super( codec, new ModifyRequestImpl() );
70 }
71
72
73
74
75
76
77
78
79 public ModifyRequestDsml( LdapApiService codec, ModifyRequest ldapMessage )
80 {
81 super( codec, ldapMessage );
82 }
83
84
85
86
87
88 public String getCurrentAttributeType()
89 {
90 return currentAttribute.getId();
91 }
92
93
94
95
96
97
98
99 public void setCurrentOperation( int currentOperation )
100 {
101 this.currentOperation = ModificationOperation.getOperation( currentOperation );
102 }
103
104
105
106
107
108
109
110 public void addAttributeTypeAndValues( String type )
111 {
112 currentAttribute = new DefaultAttribute( type );
113
114 Modification modification = new DefaultModification( currentOperation, currentAttribute );
115 getDecorated().addModification( modification );
116 }
117
118
119
120
121
122
123
124
125 public void addAttributeValue( byte[] value ) throws LdapException
126 {
127 currentAttribute.add( value );
128 }
129
130
131
132
133
134
135
136
137 public void addAttributeValue( String value ) throws LdapException
138 {
139 currentAttribute.add( value );
140 }
141
142
143
144
145
146 public Element toDsml( Element root )
147 {
148 Element element = super.toDsml( root );
149
150 ModifyRequest request = getDecorated();
151
152
153 if ( request.getName() != null )
154 {
155 element.addAttribute( "dn", request.getName().getName() );
156 }
157
158
159 Collection<Modification> modifications = request.getModifications();
160
161 for ( Modification modification : modifications )
162 {
163 Element modElement = element.addElement( "modification" );
164
165 if ( modification.getAttribute() != null )
166 {
167 modElement.addAttribute( "name", modification.getAttribute().getId() );
168
169 for ( Value<?> value : modification.getAttribute() )
170 {
171 if ( value.getValue() != null )
172 {
173 if ( ParserUtils.needsBase64Encoding( value.getValue() ) )
174 {
175 Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
176 Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
177 element.getDocument().getRootElement().add( xsdNamespace );
178 element.getDocument().getRootElement().add( xsiNamespace );
179
180 Element valueElement = modElement.addElement( "value" ).addText(
181 ParserUtils.base64Encode( value.getValue() ) );
182 valueElement.addAttribute( new QName( "type", xsiNamespace ), "xsd:"
183 + ParserUtils.BASE64BINARY );
184 }
185 else
186 {
187 modElement.addElement( "value" ).setText( value.getString() );
188 }
189 }
190 }
191 }
192
193 ModificationOperation operation = modification.getOperation();
194
195 if ( operation == ModificationOperation.ADD_ATTRIBUTE )
196 {
197 modElement.addAttribute( "operation", "add" );
198 }
199 else if ( operation == ModificationOperation.REPLACE_ATTRIBUTE )
200 {
201 modElement.addAttribute( "operation", "replace" );
202 }
203 else if ( operation == ModificationOperation.REMOVE_ATTRIBUTE )
204 {
205 modElement.addAttribute( "operation", "delete" );
206 }
207 }
208
209 return element;
210 }
211
212
213
214
215
216
217
218
219
220 public MessageTypeEnum getResponseType()
221 {
222 return getDecorated().getResponseType();
223 }
224
225
226
227
228
229 public Dn getName()
230 {
231 return getDecorated().getName();
232 }
233
234
235
236
237
238 public ModifyRequest setName( Dn name )
239 {
240 getDecorated().setName( name );
241
242 return this;
243 }
244
245
246
247
248
249 public Collection<Modification> getModifications()
250 {
251 return getDecorated().getModifications();
252 }
253
254
255
256
257
258 public ModifyRequest addModification( Modification mod )
259 {
260 getDecorated().addModification( mod );
261
262 return this;
263 }
264
265
266
267
268
269 public ModifyRequest removeModification( Modification mod )
270 {
271 getDecorated().removeModification( mod );
272
273 return this;
274 }
275
276
277
278
279
280 public ModifyRequest remove( String attributeName, String... attributeValue )
281 {
282 getDecorated().remove( attributeName, attributeValue );
283
284 return this;
285 }
286
287
288
289
290
291 public ModifyRequest remove( String attributeName, byte[]... attributeValue )
292 {
293 getDecorated().remove( attributeName, attributeValue );
294
295 return this;
296 }
297
298
299
300
301
302 public ModifyRequest remove( Attribute attr )
303 {
304 getDecorated().remove( attr );
305
306 return this;
307 }
308
309
310
311
312
313 public ModifyRequest remove( String attributeName )
314 {
315 getDecorated().remove( attributeName );
316
317 return this;
318 }
319
320
321
322
323
324 public ModifyRequest addModification( Attribute attr, ModificationOperation modOp )
325 {
326 getDecorated().addModification( attr, modOp );
327
328 return this;
329 }
330
331
332
333
334
335 public ModifyRequest add( String attributeName, String... attributeValue )
336 {
337 getDecorated().add( attributeName, attributeValue );
338
339 return this;
340 }
341
342
343
344
345
346 public ModifyRequest add( String attributeName, byte[]... attributeValue )
347 {
348 getDecorated().add( attributeName, attributeValue );
349
350 return this;
351 }
352
353
354
355
356
357 public ModifyRequest add( Attribute attr )
358 {
359 getDecorated().add( attr );
360
361 return this;
362 }
363
364
365
366
367
368 public ModifyRequest replace( String attributeName )
369 {
370 getDecorated().replace( attributeName );
371
372 return this;
373 }
374
375
376
377
378
379 public ModifyRequest replace( String attributeName, String... attributeValue )
380 {
381 getDecorated().replace( attributeName, attributeValue );
382
383 return this;
384 }
385
386
387
388
389
390 public ModifyRequest replace( String attributeName, byte[]... attributeValue )
391 {
392 getDecorated().replace( attributeName, attributeValue );
393
394 return this;
395 }
396
397
398
399
400
401 public ModifyRequest replace( Attribute attr )
402 {
403 getDecorated().replace( attr );
404
405 return this;
406 }
407
408
409
410
411
412 public ModifyRequest setMessageId( int messageId )
413 {
414 super.setMessageId( messageId );
415
416 return this;
417 }
418
419
420
421
422
423 public ModifyRequest addControl( Control control )
424 {
425 return ( ModifyRequest ) super.addControl( control );
426 }
427
428
429
430
431
432 public ModifyRequest addAllControls( Control[] controls )
433 {
434 return ( ModifyRequest ) super.addAllControls( controls );
435 }
436
437
438
439
440
441 public ModifyRequest removeControl( Control control )
442 {
443 return ( ModifyRequest ) super.removeControl( control );
444 }
445 }