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.codec.decorators;
21  
22  
23  import java.nio.BufferOverflowException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.EncoderException;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.asn1.ber.tlv.TLV;
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.ldap.codec.api.LdapApiService;
31  import org.apache.directory.api.ldap.codec.api.LdapConstants;
32  import org.apache.directory.api.ldap.model.message.Control;
33  import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
34  import org.apache.directory.api.ldap.model.name.Dn;
35  import org.apache.directory.api.ldap.model.name.Rdn;
36  import org.apache.directory.api.util.Strings;
37  
38  
39  /**
40   * A decorator for the ModifyDnRequest message
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public class ModifyDnRequestDecorator extends SingleReplyRequestDecorator<ModifyDnRequest>
45      implements ModifyDnRequest
46  {
47      /** The modify Dn request length */
48      private int modifyDnRequestLength;
49  
50  
51      /**
52       * Makes a ModifyDnRequest encodable.
53       *
54       * @param decoratedMessage the decorated ModifyDnRequest
55       */
56      public ModifyDnRequestDecorator( LdapApiService codec, ModifyDnRequest decoratedMessage )
57      {
58          super( codec, decoratedMessage );
59      }
60  
61  
62      /**
63       * @param modifyDnRequestLength The encoded ModifyDnRequest's length
64       */
65      public void setModifyDnRequestLength( int modifyDnRequestLength )
66      {
67          this.modifyDnRequestLength = modifyDnRequestLength;
68      }
69  
70  
71      /**
72       * Stores the encoded length for the ModifyDnRequest
73       * @return the encoded length
74       */
75      public int getModifyDnResponseLength()
76      {
77          return modifyDnRequestLength;
78      }
79  
80  
81      //-------------------------------------------------------------------------
82      // The ModifyDnResponse methods
83      //-------------------------------------------------------------------------
84  
85      /**
86       * {@inheritDoc}
87       */
88      public Dn getName()
89      {
90          return getDecorated().getName();
91      }
92  
93  
94      /**
95       * {@inheritDoc}
96       */
97      public ModifyDnRequest setName( Dn name )
98      {
99          getDecorated().setName( name );
100 
101         return this;
102     }
103 
104 
105     /**
106      * {@inheritDoc}
107      */
108     public Rdn getNewRdn()
109     {
110         return getDecorated().getNewRdn();
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     public ModifyDnRequest setNewRdn( Rdn newRdn )
118     {
119         getDecorated().setNewRdn( newRdn );
120 
121         return this;
122     }
123 
124 
125     /**
126      * {@inheritDoc}
127      */
128     public boolean getDeleteOldRdn()
129     {
130         return getDecorated().getDeleteOldRdn();
131     }
132 
133 
134     /**
135      * {@inheritDoc}
136      */
137     public ModifyDnRequest setDeleteOldRdn( boolean deleteOldRdn )
138     {
139         getDecorated().setDeleteOldRdn( deleteOldRdn );
140 
141         return this;
142     }
143 
144 
145     /**
146      * {@inheritDoc}
147      */
148     public Dn getNewSuperior()
149     {
150         return getDecorated().getNewSuperior();
151     }
152 
153 
154     /**
155      * {@inheritDoc}
156      */
157     public ModifyDnRequest setNewSuperior( Dn newSuperior )
158     {
159         getDecorated().setNewSuperior( newSuperior );
160 
161         return this;
162     }
163 
164 
165     /**
166      * {@inheritDoc}
167      */
168     public boolean isMove()
169     {
170         return getDecorated().isMove();
171     }
172 
173 
174     /**
175      * {@inheritDoc}
176      */
177     public ModifyDnRequest setMessageId( int messageId )
178     {
179         super.setMessageId( messageId );
180 
181         return this;
182     }
183 
184 
185     /**
186      * {@inheritDoc}
187      */
188     public ModifyDnRequest addControl( Control control )
189     {
190         return ( ModifyDnRequest ) super.addControl( control );
191     }
192 
193 
194     /**
195      * {@inheritDoc}
196      */
197     public ModifyDnRequest addAllControls( Control[] controls )
198     {
199         return ( ModifyDnRequest ) super.addAllControls( controls );
200     }
201 
202 
203     /**
204      * {@inheritDoc}
205      */
206     public ModifyDnRequest removeControl( Control control )
207     {
208         return ( ModifyDnRequest ) super.removeControl( control );
209     }
210 
211 
212     //-------------------------------------------------------------------------
213     // The Decorator methods
214     //-------------------------------------------------------------------------
215 
216     /**
217      * Compute the ModifyDNRequest length
218      * 
219      * ModifyDNRequest :
220      * <pre>
221      * 0x6C L1
222      *  |
223      *  +--> 0x04 L2 entry
224      *  +--> 0x04 L3 newRDN
225      *  +--> 0x01 0x01 (true/false) deleteOldRDN (3 bytes)
226      * [+--> 0x80 L4 newSuperior ] 
227      * 
228      * L2 = Length(0x04) + Length(Length(entry)) + Length(entry) 
229      * L3 = Length(0x04) + Length(Length(newRDN)) + Length(newRDN) 
230      * L4 = Length(0x80) + Length(Length(newSuperior)) + Length(newSuperior)
231      * L1 = L2 + L3 + 3 [+ L4] 
232      * 
233      * Length(ModifyDNRequest) = Length(0x6C) + Length(L1) + L1
234      * </pre>
235      * 
236      * @return The PDU's length of a ModifyDN Request
237      */
238     public int computeLength()
239     {
240         int newRdnlength = Strings.getBytesUtf8( getNewRdn().getName() ).length;
241 
242         int modifyDNRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( getName() ) )
243             + Dn.getNbBytes( getName() ) + 1 + TLV.getNbBytes( newRdnlength ) + newRdnlength + 1 + 1
244             + 1; // deleteOldRDN
245 
246         if ( getNewSuperior() != null )
247         {
248             modifyDNRequestLength += 1 + TLV.getNbBytes( Dn.getNbBytes( getNewSuperior() ) )
249                 + Dn.getNbBytes( getNewSuperior() );
250         }
251 
252         setModifyDnRequestLength( modifyDNRequestLength );
253 
254         return 1 + TLV.getNbBytes( modifyDNRequestLength ) + modifyDNRequestLength;
255     }
256 
257 
258     /**
259      * Encode the ModifyDNRequest message to a PDU. 
260      * 
261      * ModifyDNRequest :
262      * <pre>
263      * 0x6C LL
264      *   0x04 LL entry
265      *   0x04 LL newRDN
266      *   0x01 0x01 deleteOldRDN
267      *   [0x80 LL newSuperior]
268      * </pre>
269      * @param buffer The buffer where to put the PDU
270      * @return The PDU.
271      */
272     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
273     {
274         try
275         {
276             // The ModifyDNRequest Tag
277             buffer.put( LdapConstants.MODIFY_DN_REQUEST_TAG );
278             buffer.put( TLV.getBytes( getModifyDnResponseLength() ) );
279 
280             // The entry
281 
282             BerValue.encode( buffer, Dn.getBytes( getName() ) );
283 
284             // The newRDN
285             BerValue.encode( buffer, getNewRdn().getName() );
286 
287             // The flag deleteOldRdn
288             BerValue.encode( buffer, getDeleteOldRdn() );
289 
290             // The new superior, if any
291             if ( getNewSuperior() != null )
292             {
293                 // Encode the reference
294                 buffer.put( ( byte ) LdapConstants.MODIFY_DN_REQUEST_NEW_SUPERIOR_TAG );
295 
296                 int newSuperiorLength = Dn.getNbBytes( getNewSuperior() );
297 
298                 buffer.put( TLV.getBytes( newSuperiorLength ) );
299 
300                 if ( newSuperiorLength != 0 )
301                 {
302                     buffer.put( Dn.getBytes( getNewSuperior() ) );
303                 }
304             }
305         }
306         catch ( BufferOverflowException boe )
307         {
308             throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
309         }
310 
311         return buffer;
312     }
313 }