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 decoratedMessage the decorated ModifyDnRequest
055     */
056    public ModifyDnRequestDecorator( LdapApiService codec, ModifyDnRequest decoratedMessage )
057    {
058        super( codec, decoratedMessage );
059    }
060
061
062    //-------------------------------------------------------------------------
063    // The ModifyDnResponse methods
064    //-------------------------------------------------------------------------
065
066    /**
067     * {@inheritDoc}
068     */
069    public Dn getName()
070    {
071        return getDecorated().getName();
072    }
073
074
075    /**
076     * {@inheritDoc}
077     */
078    public ModifyDnRequest setName( Dn name )
079    {
080        getDecorated().setName( name );
081
082        return this;
083    }
084
085
086    /**
087     * {@inheritDoc}
088     */
089    public Rdn getNewRdn()
090    {
091        return getDecorated().getNewRdn();
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    public ModifyDnRequest setNewRdn( Rdn newRdn )
099    {
100        getDecorated().setNewRdn( newRdn );
101
102        return this;
103    }
104
105
106    /**
107     * {@inheritDoc}
108     */
109    public boolean getDeleteOldRdn()
110    {
111        return getDecorated().getDeleteOldRdn();
112    }
113
114
115    /**
116     * {@inheritDoc}
117     */
118    public ModifyDnRequest setDeleteOldRdn( boolean deleteOldRdn )
119    {
120        getDecorated().setDeleteOldRdn( deleteOldRdn );
121
122        return this;
123    }
124
125
126    /**
127     * {@inheritDoc}
128     */
129    public Dn getNewSuperior()
130    {
131        return getDecorated().getNewSuperior();
132    }
133
134
135    /**
136     * {@inheritDoc}
137     */
138    public ModifyDnRequest setNewSuperior( Dn newSuperior )
139    {
140        getDecorated().setNewSuperior( newSuperior );
141
142        return this;
143    }
144
145
146    /**
147     * {@inheritDoc}
148     */
149    public boolean isMove()
150    {
151        return getDecorated().isMove();
152    }
153
154
155    /**
156     * {@inheritDoc}
157     */
158    public ModifyDnRequest setMessageId( int messageId )
159    {
160        super.setMessageId( messageId );
161
162        return this;
163    }
164
165
166    /**
167     * {@inheritDoc}
168     */
169    public ModifyDnRequest addControl( Control control )
170    {
171        return ( ModifyDnRequest ) super.addControl( control );
172    }
173
174
175    /**
176     * {@inheritDoc}
177     */
178    public ModifyDnRequest addAllControls( Control[] controls )
179    {
180        return ( ModifyDnRequest ) super.addAllControls( controls );
181    }
182
183
184    /**
185     * {@inheritDoc}
186     */
187    public ModifyDnRequest removeControl( Control control )
188    {
189        return ( ModifyDnRequest ) super.removeControl( control );
190    }
191
192
193    //-------------------------------------------------------------------------
194    // The Decorator methods
195    //-------------------------------------------------------------------------
196
197    /**
198     * Compute the ModifyDNRequest length
199     * 
200     * ModifyDNRequest :
201     * <pre>
202     * 0x6C L1
203     *  |
204     *  +--> 0x04 L2 entry
205     *  +--> 0x04 L3 newRDN
206     *  +--> 0x01 0x01 (true/false) deleteOldRDN (3 bytes)
207     * [+--> 0x80 L4 newSuperior ] 
208     * 
209     * L2 = Length(0x04) + Length(Length(entry)) + Length(entry) 
210     * L3 = Length(0x04) + Length(Length(newRDN)) + Length(newRDN) 
211     * L4 = Length(0x80) + Length(Length(newSuperior)) + Length(newSuperior)
212     * L1 = L2 + L3 + 3 [+ L4] 
213     * 
214     * Length(ModifyDNRequest) = Length(0x6C) + Length(L1) + L1
215     * </pre>
216     * 
217     * @return The PDU's length of a ModifyDN Request
218     */
219    public int computeLength()
220    {
221        int newRdnlength = Strings.getBytesUtf8( getNewRdn().getName() ).length;
222
223        // deleteOldRDN
224        modifyDnRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( getName() ) )
225            + Dn.getNbBytes( getName() ) + 1 + TLV.getNbBytes( newRdnlength ) + newRdnlength + 1 + 1
226            + 1;
227
228        if ( getNewSuperior() != null )
229        {
230            modifyDnRequestLength += 1 + TLV.getNbBytes( Dn.getNbBytes( getNewSuperior() ) )
231                + Dn.getNbBytes( getNewSuperior() );
232        }
233
234        return 1 + TLV.getNbBytes( modifyDnRequestLength ) + modifyDnRequestLength;
235    }
236
237
238    /**
239     * Encode the ModifyDNRequest message to a PDU. 
240     * 
241     * ModifyDNRequest :
242     * <pre>
243     * 0x6C LL
244     *   0x04 LL entry
245     *   0x04 LL newRDN
246     *   0x01 0x01 deleteOldRDN
247     *   [0x80 LL newSuperior]
248     * </pre>
249     * @param buffer The buffer where to put the PDU
250     * @return The PDU.
251     */
252    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
253    {
254        try
255        {
256            // The ModifyDNRequest Tag
257            buffer.put( LdapCodecConstants.MODIFY_DN_REQUEST_TAG );
258            buffer.put( TLV.getBytes( modifyDnRequestLength ) );
259
260            // The entry
261
262            BerValue.encode( buffer, Dn.getBytes( getName() ) );
263
264            // The newRDN
265            BerValue.encode( buffer, getNewRdn().getName() );
266
267            // The flag deleteOldRdn
268            BerValue.encode( buffer, getDeleteOldRdn() );
269
270            // The new superior, if any
271            if ( getNewSuperior() != null )
272            {
273                // Encode the reference
274                buffer.put( ( byte ) LdapCodecConstants.MODIFY_DN_REQUEST_NEW_SUPERIOR_TAG );
275
276                int newSuperiorLength = Dn.getNbBytes( getNewSuperior() );
277
278                buffer.put( TLV.getBytes( newSuperiorLength ) );
279
280                if ( newSuperiorLength != 0 )
281                {
282                    buffer.put( Dn.getBytes( getNewSuperior() ) );
283                }
284            }
285        }
286        catch ( BufferOverflowException boe )
287        {
288            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
289        }
290
291        return buffer;
292    }
293}