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   *    http://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.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   * DSML Decorator for ModifyRequest
47   *
48   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49   */
50  public class ModifyRequestDsml
51      extends AbstractResultResponseRequestDsml<ModifyRequest, ModifyResponse>
52      implements ModifyRequest
53  {
54  
55      /** The current attribute being decoded */
56      private Attribute currentAttribute;
57  
58      /** A local storage for the operation */
59      private ModificationOperation currentOperation;
60  
61  
62      /**
63       * Creates a new getDecoratedMessage() of ModifyRequestDsml.
64       */
65      public ModifyRequestDsml( LdapApiService codec )
66      {
67          super( codec, new ModifyRequestImpl() );
68      }
69  
70  
71      /**
72       * Creates a new getDecoratedMessage() of ModifyRequestDsml.
73       *
74       * @param ldapMessage
75       *      the message to decorate
76       */
77      public ModifyRequestDsml( LdapApiService codec, ModifyRequest ldapMessage )
78      {
79          super( codec, ldapMessage );
80      }
81  
82  
83      /**
84       * Return the current attribute's type
85       */
86      public String getCurrentAttributeType()
87      {
88          return currentAttribute.getId();
89      }
90  
91  
92      /**
93       * Store the current operation
94       * 
95       * @param currentOperation The currentOperation to set.
96       */
97      public void setCurrentOperation( int currentOperation )
98      {
99          this.currentOperation = ModificationOperation.getOperation( currentOperation );
100     }
101 
102 
103     /**
104      * Add a new attributeTypeAndValue
105      * 
106      * @param type The attribute's name
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      * Add a new value to the current attribute
119      * 
120      * @param value The value to add
121      */
122     public void addAttributeValue( byte[] value ) throws LdapException
123     {
124         currentAttribute.add( value );
125     }
126 
127 
128     /**
129      * Add a new value to the current attribute
130      * 
131      * @param value The value to add
132      */
133     public void addAttributeValue( String value ) throws LdapException
134     {
135         currentAttribute.add( value );
136     }
137 
138 
139     /**
140      * {@inheritDoc}
141      */
142     public Element toDsml( Element root )
143     {
144         Element element = super.toDsml( root );
145 
146         ModifyRequest request = getDecorated();
147 
148         // Dn
149         if ( request.getName() != null )
150         {
151             element.addAttribute( "dn", request.getName().getName() );
152         }
153 
154         // Modifications
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     // The ModifyRequest methods
211     //-------------------------------------------------------------------------
212 
213     /**
214      * {@inheritDoc}
215      */
216     public MessageTypeEnum getResponseType()
217     {
218         return getDecorated().getResponseType();
219     }
220 
221 
222     /**
223      * {@inheritDoc}
224      */
225     public Dn getName()
226     {
227         return getDecorated().getName();
228     }
229 
230 
231     /**
232      * {@inheritDoc}
233      */
234     public ModifyRequest setName( Dn name )
235     {
236         getDecorated().setName( name );
237 
238         return this;
239     }
240 
241 
242     /**
243      * {@inheritDoc}
244      */
245     public Collection<Modification> getModifications()
246     {
247         return getDecorated().getModifications();
248     }
249 
250 
251     /**
252      * {@inheritDoc}
253      */
254     public ModifyRequest addModification( Modification mod )
255     {
256         getDecorated().addModification( mod );
257 
258         return this;
259     }
260 
261 
262     /**
263      * {@inheritDoc}
264      */
265     public ModifyRequest removeModification( Modification mod )
266     {
267         getDecorated().removeModification( mod );
268 
269         return this;
270     }
271 
272 
273     /**
274      * {@inheritDoc}
275      */
276     public ModifyRequest remove( String attributeName, String... attributeValue )
277     {
278         getDecorated().remove( attributeName, attributeValue );
279 
280         return this;
281     }
282 
283 
284     /**
285      * {@inheritDoc}
286      */
287     public ModifyRequest remove( String attributeName, byte[]... attributeValue )
288     {
289         getDecorated().remove( attributeName, attributeValue );
290 
291         return this;
292     }
293 
294 
295     /**
296      * {@inheritDoc}
297      */
298     public ModifyRequest remove( Attribute attr )
299     {
300         getDecorated().remove( attr );
301 
302         return this;
303     }
304 
305 
306     /**
307      * {@inheritDoc}
308      */
309     public ModifyRequest addModification( Attribute attr, ModificationOperation modOp )
310     {
311         getDecorated().addModification( attr, modOp );
312 
313         return this;
314     }
315 
316 
317     /**
318      * {@inheritDoc}
319      */
320     public ModifyRequest add( String attributeName, String... attributeValue )
321     {
322         getDecorated().add( attributeName, attributeValue );
323 
324         return this;
325     }
326 
327 
328     /**
329      * {@inheritDoc}
330      */
331     public ModifyRequest add( String attributeName, byte[]... attributeValue )
332     {
333         getDecorated().add( attributeName, attributeValue );
334 
335         return this;
336     }
337 
338 
339     /**
340      * {@inheritDoc}
341      */
342     public ModifyRequest add( Attribute attr )
343     {
344         getDecorated().add( attr );
345 
346         return this;
347     }
348 
349 
350     /**
351      * {@inheritDoc}
352      */
353     public ModifyRequest replace( String attributeName )
354     {
355         getDecorated().replace( attributeName );
356 
357         return this;
358     }
359 
360 
361     /**
362      * {@inheritDoc}
363      */
364     public ModifyRequest replace( String attributeName, String... attributeValue )
365     {
366         getDecorated().replace( attributeName, attributeValue );
367 
368         return this;
369     }
370 
371 
372     /**
373      * {@inheritDoc}
374      */
375     public ModifyRequest replace( String attributeName, byte[]... attributeValue )
376     {
377         getDecorated().replace( attributeName, attributeValue );
378 
379         return this;
380     }
381 
382 
383     /**
384      * {@inheritDoc}
385      */
386     public ModifyRequest replace( Attribute attr )
387     {
388         getDecorated().replace( attr );
389 
390         return this;
391     }
392 
393 
394     /**
395      * {@inheritDoc}
396      */
397     public ModifyRequest setMessageId( int messageId )
398     {
399         super.setMessageId( messageId );
400 
401         return this;
402     }
403 
404 
405     /**
406      * {@inheritDoc}
407      */
408     public ModifyRequest addControl( Control control )
409     {
410         return ( ModifyRequest ) super.addControl( control );
411     }
412 
413 
414     /**
415      * {@inheritDoc}
416      */
417     public ModifyRequest addAllControls( Control[] controls )
418     {
419         return ( ModifyRequest ) super.addAllControls( controls );
420     }
421 
422 
423     /**
424      * {@inheritDoc}
425      */
426     public ModifyRequest removeControl( Control control )
427     {
428         return ( ModifyRequest ) super.removeControl( control );
429     }
430 }