View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.dsmlv2.request;
21  
22  
23  import java.util.Collection;
24  
25  import org.apache.commons.text.StringEscapeUtils;
26  import org.apache.directory.api.dsmlv2.DsmlLiterals;
27  import org.apache.directory.api.dsmlv2.ParserUtils;
28  import org.apache.directory.api.ldap.codec.api.LdapApiService;
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.entry.Value;
35  import org.apache.directory.api.ldap.model.exception.LdapException;
36  import org.apache.directory.api.ldap.model.message.Control;
37  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
38  import org.apache.directory.api.ldap.model.message.ModifyRequest;
39  import org.apache.directory.api.ldap.model.message.ModifyRequestImpl;
40  import org.apache.directory.api.ldap.model.message.ModifyResponse;
41  import org.apache.directory.api.ldap.model.name.Dn;
42  import org.dom4j.Element;
43  import org.dom4j.Namespace;
44  import org.dom4j.QName;
45  
46  
47  /**
48   * DSML Decorator for ModifyRequest
49   *
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   */
52  public class ModifyRequestDsml
53      extends AbstractResultResponseRequestDsml<ModifyRequest, ModifyResponse>
54      implements ModifyRequest
55  {
56  
57      /** The current attribute being decoded */
58      private Attribute currentAttribute;
59  
60      /** A local storage for the operation */
61      private ModificationOperation currentOperation;
62  
63  
64      /**
65       * Creates a new getDecoratedMessage() of ModifyRequestDsml.
66       * 
67       * @param codec The LDAP Service to use
68       */
69      public ModifyRequestDsml( LdapApiService codec )
70      {
71          super( codec, new ModifyRequestImpl() );
72      }
73  
74  
75      /**
76       * Creates a new getDecoratedMessage() of ModifyRequestDsml.
77       *
78       * @param codec The LDAP Service to use
79       * @param ldapMessage the message to decorate
80       */
81      public ModifyRequestDsml( LdapApiService codec, ModifyRequest ldapMessage )
82      {
83          super( codec, ldapMessage );
84      }
85  
86  
87      /**
88       * @return the current attribute's type
89       */
90      public String getCurrentAttributeType()
91      {
92          return currentAttribute.getId();
93      }
94  
95  
96      /**
97       * Store the current operation
98       * 
99       * @param currentOperation The currentOperation to set.
100      */
101     public void setCurrentOperation( int currentOperation )
102     {
103         this.currentOperation = ModificationOperation.getOperation( currentOperation );
104     }
105 
106 
107     /**
108      * Add a new attributeTypeAndValue
109      * 
110      * @param type The attribute's name
111      */
112     public void addAttributeTypeAndValues( String type )
113     {
114         currentAttribute = new DefaultAttribute( type );
115 
116         Modification modification = new DefaultModification( currentOperation, currentAttribute );
117         getDecorated().addModification( modification );
118     }
119 
120 
121     /**
122      * Add a new value to the current attribute
123      * 
124      * @param value The value to add
125      * @throws LdapException If we can't add a value
126      */
127     public void addAttributeValue( byte[] value ) throws LdapException
128     {
129         currentAttribute.add( value );
130     }
131 
132 
133     /**
134      * Add a new value to the current attribute
135      * 
136      * @param value The value to add
137      * @throws LdapException If we can't add a value
138      */
139     public void addAttributeValue( String value ) throws LdapException
140     {
141         currentAttribute.add( value );
142     }
143 
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public Element toDsml( Element root )
150     {
151         Element element = super.toDsml( root );
152 
153         ModifyRequest request = getDecorated();
154 
155         // Dn
156         if ( request.getName() != null )
157         {
158             element.addAttribute( DsmlLiterals.DN, request.getName().getName() );
159         }
160 
161         // Modifications
162         Collection<Modification> modifications = request.getModifications();
163 
164         for ( Modification modification : modifications )
165         {
166             Element modElement = element.addElement( DsmlLiterals.MODIFICATION );
167 
168             if ( modification.getAttribute() != null )
169             {
170                 modElement.addAttribute( DsmlLiterals.NAME, modification.getAttribute().getId() );
171 
172                 for ( Value value : modification.getAttribute() )
173                 {
174                     if ( value.isHumanReadable() )
175                     {
176                         modElement.addElement( DsmlLiterals.VALUE ).setText( StringEscapeUtils.escapeXml11( value.getString() ) );
177                     }
178                     else
179                     {
180                         Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
181                         Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
182                         element.getDocument().getRootElement().add( xsdNamespace );
183                         element.getDocument().getRootElement().add( xsiNamespace );
184 
185                         Element valueElement = modElement.addElement( DsmlLiterals.VALUE ).addText(
186                             ParserUtils.base64Encode( value.getString() ) );
187                         valueElement.addAttribute( new QName( DsmlLiterals.TYPE, xsiNamespace ), ParserUtils.XSD_COLON
188                             + ParserUtils.BASE64BINARY );
189                     }
190                 }
191             }
192 
193             ModificationOperation operation = modification.getOperation();
194 
195             if ( operation == ModificationOperation.ADD_ATTRIBUTE )
196             {
197                 modElement.addAttribute( DsmlLiterals.OPERATION, DsmlLiterals.ADD );
198             }
199             else if ( operation == ModificationOperation.REPLACE_ATTRIBUTE )
200             {
201                 modElement.addAttribute( DsmlLiterals.OPERATION, DsmlLiterals.REPLACE );
202             }
203             else if ( operation == ModificationOperation.REMOVE_ATTRIBUTE )
204             {
205                 modElement.addAttribute( DsmlLiterals.OPERATION, DsmlLiterals.DELETE );
206             }
207             else if ( operation == ModificationOperation.INCREMENT_ATTRIBUTE )
208             {
209                 modElement.addAttribute( DsmlLiterals.OPERATION, DsmlLiterals.INCREMENT );
210             }
211         }
212 
213         return element;
214     }
215 
216 
217     //-------------------------------------------------------------------------
218     // The ModifyRequest methods
219     //-------------------------------------------------------------------------
220 
221     /**
222      * {@inheritDoc}
223      */
224     @Override
225     public MessageTypeEnum getResponseType()
226     {
227         return getDecorated().getResponseType();
228     }
229 
230 
231     /**
232      * {@inheritDoc}
233      */
234     @Override
235     public Dn getName()
236     {
237         return getDecorated().getName();
238     }
239 
240 
241     /**
242      * {@inheritDoc}
243      */
244     @Override
245     public ModifyRequest setName( Dn name )
246     {
247         getDecorated().setName( name );
248 
249         return this;
250     }
251 
252 
253     /**
254      * {@inheritDoc}
255      */
256     @Override
257     public Collection<Modification> getModifications()
258     {
259         return getDecorated().getModifications();
260     }
261 
262 
263     /**
264      * {@inheritDoc}
265      */
266     @Override
267     public ModifyRequest addModification( Modification mod )
268     {
269         getDecorated().addModification( mod );
270 
271         return this;
272     }
273 
274 
275     /**
276      * {@inheritDoc}
277      */
278     @Override
279     public ModifyRequest removeModification( Modification mod )
280     {
281         getDecorated().removeModification( mod );
282 
283         return this;
284     }
285 
286 
287     /**
288      * {@inheritDoc}
289      */
290     @Override
291     public ModifyRequest remove( String attributeName, String... attributeValue )
292     {
293         getDecorated().remove( attributeName, attributeValue );
294 
295         return this;
296     }
297 
298 
299     /**
300      * {@inheritDoc}
301      */
302     public ModifyRequest remove( String attributeName, byte[]... attributeValue )
303     {
304         getDecorated().remove( attributeName, attributeValue );
305 
306         return this;
307     }
308 
309 
310     /**
311      * {@inheritDoc}
312      */
313     @Override
314     public ModifyRequest remove( Attribute attr )
315     {
316         getDecorated().remove( attr );
317 
318         return this;
319     }
320 
321 
322     /**
323      * {@inheritDoc}
324      */
325     @Override
326     public ModifyRequest remove( String attributeName )
327     {
328         getDecorated().remove( attributeName );
329 
330         return this;
331     }
332 
333 
334     /**
335      * {@inheritDoc}
336      */
337     @Override
338     public ModifyRequest addModification( Attribute attr, ModificationOperation modOp )
339     {
340         getDecorated().addModification( attr, modOp );
341 
342         return this;
343     }
344 
345 
346     /**
347      * {@inheritDoc}
348      */
349     @Override
350     public ModifyRequest add( String attributeName, String... attributeValue )
351     {
352         getDecorated().add( attributeName, attributeValue );
353 
354         return this;
355     }
356 
357 
358     /**
359      * {@inheritDoc}
360      */
361     public ModifyRequest add( String attributeName, byte[]... attributeValue )
362     {
363         getDecorated().add( attributeName, attributeValue );
364 
365         return this;
366     }
367 
368 
369     /**
370      * {@inheritDoc}
371      */
372     @Override
373     public ModifyRequest add( Attribute attr )
374     {
375         getDecorated().add( attr );
376 
377         return this;
378     }
379 
380 
381     /**
382      * {@inheritDoc}
383      */
384     @Override
385     public ModifyRequest replace( String attributeName )
386     {
387         getDecorated().replace( attributeName );
388 
389         return this;
390     }
391 
392 
393     /**
394      * {@inheritDoc}
395      */
396     @Override
397     public ModifyRequest replace( String attributeName, String... attributeValue )
398     {
399         getDecorated().replace( attributeName, attributeValue );
400 
401         return this;
402     }
403 
404 
405     /**
406      * {@inheritDoc}
407      */
408     public ModifyRequest replace( String attributeName, byte[]... attributeValue )
409     {
410         getDecorated().replace( attributeName, attributeValue );
411 
412         return this;
413     }
414 
415 
416     /**
417      * {@inheritDoc}
418      */
419     @Override
420     public ModifyRequest replace( Attribute attr )
421     {
422         getDecorated().replace( attr );
423 
424         return this;
425     }
426 
427 
428     /**
429      * {@inheritDoc}
430      */
431     @Override
432     public ModifyRequest increment( Attribute attributeName )
433     {
434         getDecorated().increment( attributeName );
435 
436         return this;
437     }
438 
439 
440     /**
441      * {@inheritDoc}
442      */
443     @Override
444     public ModifyRequest increment( Attribute attributeName, int increment )
445     {
446         getDecorated().increment( attributeName, increment );
447 
448         return this;
449     }
450 
451 
452     /**
453      * {@inheritDoc}
454      */
455     @Override
456     public ModifyRequest increment( String attr )
457     {
458         getDecorated().increment( attr );
459 
460         return this;
461     }
462 
463 
464     /**
465      * {@inheritDoc}
466      */
467     @Override
468     public ModifyRequest increment( String attr, int increment )
469     {
470         getDecorated().increment( attr, increment );
471 
472         return this;
473     }
474 
475 
476     /**
477      * {@inheritDoc}
478      */
479     @Override
480     public ModifyRequest setMessageId( int messageId )
481     {
482         super.setMessageId( messageId );
483 
484         return this;
485     }
486 
487 
488     /**
489      * {@inheritDoc}
490      */
491     @Override
492     public ModifyRequest addControl( Control control )
493     {
494         return ( ModifyRequest ) super.addControl( control );
495     }
496 
497 
498     /**
499      * {@inheritDoc}
500      */
501     @Override
502     public ModifyRequest addAllControls( Control[] controls )
503     {
504         return ( ModifyRequest ) super.addAllControls( controls );
505     }
506 
507 
508     /**
509      * {@inheritDoc}
510      */
511     @Override
512     public ModifyRequest removeControl( Control control )
513     {
514         return ( ModifyRequest ) super.removeControl( control );
515     }
516 }