001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.codec.decorators;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.api.asn1.EncoderException;
027import org.apache.directory.api.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.asn1.ber.tlv.TLV;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.codec.api.LdapApiService;
031import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
032import org.apache.directory.api.ldap.model.message.Control;
033import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
034import org.apache.directory.api.ldap.model.name.Dn;
035import org.apache.directory.api.ldap.model.name.Rdn;
036import org.apache.directory.api.util.Strings;
037
038
039/**
040 * A decorator for the ModifyDnRequest message
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class ModifyDnRequestDecorator extends SingleReplyRequestDecorator<ModifyDnRequest>
045    implements ModifyDnRequest
046{
047    /** The modify Dn request length */
048    private int modifyDnRequestLength;
049
050
051    /**
052     * Makes a ModifyDnRequest encodable.
053     *
054     * @param codec The LDAP service instance
055     * @param decoratedMessage the decorated ModifyDnRequest
056     */
057    public ModifyDnRequestDecorator( LdapApiService codec, ModifyDnRequest decoratedMessage )
058    {
059        super( codec, decoratedMessage );
060    }
061
062
063    //-------------------------------------------------------------------------
064    // The ModifyDnResponse methods
065    //-------------------------------------------------------------------------
066
067    /**
068     * {@inheritDoc}
069     */
070    public Dn getName()
071    {
072        return getDecorated().getName();
073    }
074
075
076    /**
077     * {@inheritDoc}
078     */
079    public ModifyDnRequest setName( Dn name )
080    {
081        getDecorated().setName( name );
082
083        return this;
084    }
085
086
087    /**
088     * {@inheritDoc}
089     */
090    public Rdn getNewRdn()
091    {
092        return getDecorated().getNewRdn();
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    public ModifyDnRequest setNewRdn( Rdn newRdn )
100    {
101        getDecorated().setNewRdn( newRdn );
102
103        return this;
104    }
105
106
107    /**
108     * {@inheritDoc}
109     */
110    public boolean getDeleteOldRdn()
111    {
112        return getDecorated().getDeleteOldRdn();
113    }
114
115
116    /**
117     * {@inheritDoc}
118     */
119    public ModifyDnRequest setDeleteOldRdn( boolean deleteOldRdn )
120    {
121        getDecorated().setDeleteOldRdn( deleteOldRdn );
122
123        return this;
124    }
125
126
127    /**
128     * {@inheritDoc}
129     */
130    public Dn getNewSuperior()
131    {
132        return getDecorated().getNewSuperior();
133    }
134
135
136    /**
137     * {@inheritDoc}
138     */
139    public ModifyDnRequest setNewSuperior( Dn newSuperior )
140    {
141        getDecorated().setNewSuperior( newSuperior );
142
143        return this;
144    }
145
146
147    /**
148     * {@inheritDoc}
149     */
150    public boolean isMove()
151    {
152        return getDecorated().isMove();
153    }
154
155
156    /**
157     * {@inheritDoc}
158     */
159    public ModifyDnRequest setMessageId( int messageId )
160    {
161        super.setMessageId( messageId );
162
163        return this;
164    }
165
166
167    /**
168     * {@inheritDoc}
169     */
170    public ModifyDnRequest addControl( Control control )
171    {
172        return ( ModifyDnRequest ) super.addControl( control );
173    }
174
175
176    /**
177     * {@inheritDoc}
178     */
179    public ModifyDnRequest addAllControls( Control[] controls )
180    {
181        return ( ModifyDnRequest ) super.addAllControls( controls );
182    }
183
184
185    /**
186     * {@inheritDoc}
187     */
188    public ModifyDnRequest removeControl( Control control )
189    {
190        return ( ModifyDnRequest ) super.removeControl( control );
191    }
192
193
194    //-------------------------------------------------------------------------
195    // The Decorator methods
196    //-------------------------------------------------------------------------
197
198    /**
199     * Compute the ModifyDNRequest length
200     * <br>
201     * ModifyDNRequest :
202     * <pre>
203     * 0x6C L1
204     *  |
205     *  +--&gt; 0x04 L2 entry
206     *  +--&gt; 0x04 L3 newRDN
207     *  +--&gt; 0x01 0x01 (true/false) deleteOldRDN (3 bytes)
208     * [+--&gt; 0x80 L4 newSuperior ] 
209     * 
210     * L2 = Length(0x04) + Length(Length(entry)) + Length(entry) 
211     * L3 = Length(0x04) + Length(Length(newRDN)) + Length(newRDN) 
212     * L4 = Length(0x80) + Length(Length(newSuperior)) + Length(newSuperior)
213     * L1 = L2 + L3 + 3 [+ L4] 
214     * 
215     * Length(ModifyDNRequest) = Length(0x6C) + Length(L1) + L1
216     * </pre>
217     * 
218     * @return The PDU's length of a ModifyDN Request
219     */
220    public int computeLength()
221    {
222        int newRdnlength = Strings.getBytesUtf8( getNewRdn().getName() ).length;
223
224        // deleteOldRDN
225        modifyDnRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( getName() ) )
226            + Dn.getNbBytes( getName() ) + 1 + TLV.getNbBytes( newRdnlength ) + newRdnlength + 1 + 1
227            + 1;
228
229        if ( getNewSuperior() != null )
230        {
231            modifyDnRequestLength += 1 + TLV.getNbBytes( Dn.getNbBytes( getNewSuperior() ) )
232                + Dn.getNbBytes( getNewSuperior() );
233        }
234
235        return 1 + TLV.getNbBytes( modifyDnRequestLength ) + modifyDnRequestLength;
236    }
237
238
239    /**
240     * Encode the ModifyDNRequest message to a PDU. 
241     * <br>
242     * ModifyDNRequest :
243     * <pre>
244     * 0x6C LL
245     *   0x04 LL entry
246     *   0x04 LL newRDN
247     *   0x01 0x01 deleteOldRDN
248     *   [0x80 LL newSuperior]
249     * </pre>
250     * 
251     * @param buffer The buffer where to put the PDU
252     * @return The PDU.
253     */
254    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
255    {
256        try
257        {
258            // The ModifyDNRequest Tag
259            buffer.put( LdapCodecConstants.MODIFY_DN_REQUEST_TAG );
260            buffer.put( TLV.getBytes( modifyDnRequestLength ) );
261
262            // The entry
263
264            BerValue.encode( buffer, Dn.getBytes( getName() ) );
265
266            // The newRDN
267            BerValue.encode( buffer, getNewRdn().getName() );
268
269            // The flag deleteOldRdn
270            BerValue.encode( buffer, getDeleteOldRdn() );
271
272            // The new superior, if any
273            if ( getNewSuperior() != null )
274            {
275                // Encode the reference
276                buffer.put( ( byte ) LdapCodecConstants.MODIFY_DN_REQUEST_NEW_SUPERIOR_TAG );
277
278                int newSuperiorLength = Dn.getNbBytes( getNewSuperior() );
279
280                buffer.put( TLV.getBytes( newSuperiorLength ) );
281
282                if ( newSuperiorLength != 0 )
283                {
284                    buffer.put( Dn.getBytes( getNewSuperior() ) );
285                }
286            }
287        }
288        catch ( BufferOverflowException boe )
289        {
290            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
291        }
292
293        return buffer;
294    }
295}