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