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