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.TLV;
028import org.apache.directory.api.i18n.I18n;
029import org.apache.directory.api.ldap.codec.api.LdapApiService;
030import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
031import org.apache.directory.api.ldap.model.message.Control;
032import org.apache.directory.api.ldap.model.message.DeleteRequest;
033import org.apache.directory.api.ldap.model.name.Dn;
034import org.apache.directory.api.util.Strings;
035
036
037/**
038 * A decorator for the DeleteRequest message
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public class DeleteRequestDecorator extends SingleReplyRequestDecorator<DeleteRequest>
043    implements DeleteRequest
044{
045    /** The bytes containing the Dn */
046    private byte[] dnBytes;
047
048
049    /**
050     * Makes a DeleteRequest a MessageDecorator.
051     *
052     * @param codec The LDAP service instance
053     * @param decoratedMessage the decorated DeleteRequest
054     */
055    public DeleteRequestDecorator( LdapApiService codec, DeleteRequest decoratedMessage )
056    {
057        super( codec, decoratedMessage );
058    }
059
060
061    //-------------------------------------------------------------------------
062    // The DeleteRequest methods
063    //-------------------------------------------------------------------------
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public Dn getName()
070    {
071        return getDecorated().getName();
072    }
073
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public DeleteRequest setName( Dn name )
080    {
081        getDecorated().setName( name );
082
083        return this;
084    }
085
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public DeleteRequest setMessageId( int messageId )
092    {
093        super.setMessageId( messageId );
094
095        return this;
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public DeleteRequest addControl( Control control )
104    {
105        return ( DeleteRequest ) super.addControl( control );
106    }
107
108
109    /**
110     * {@inheritDoc}
111     */
112    @Override
113    public DeleteRequest addAllControls( Control[] controls )
114    {
115        return ( DeleteRequest ) super.addAllControls( controls );
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    @Override
123    public DeleteRequest removeControl( Control control )
124    {
125        return ( DeleteRequest ) super.removeControl( control );
126    }
127
128
129    //-------------------------------------------------------------------------
130    // The Decorator methods
131    //-------------------------------------------------------------------------
132    /**
133     * Compute the DelRequest length
134     * <br>
135     * DelRequest :
136     * <pre>
137     * 0x4A L1 entry
138     * 
139     * L1 = Length(entry)
140     * Length(DelRequest) = Length(0x4A) + Length(L1) + L1
141     * </pre>
142     */
143    @Override
144    public int computeLength()
145    {
146        dnBytes = Strings.getBytesUtf8( getName().getName() );
147        int dnLength = dnBytes.length;
148
149        // The entry
150        return 1 + TLV.getNbBytes( dnLength ) + dnLength;
151    }
152
153
154    /**
155     * Encode the DelRequest message to a PDU.
156     * <br>
157     * DelRequest :
158     * <pre>
159     * 0x4A LL entry
160     * </pre>
161     * 
162     * @param buffer The buffer where to put the PDU
163     */
164    @Override
165    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
166    {
167        try
168        {
169            // The DelRequest Tag
170            buffer.put( LdapCodecConstants.DEL_REQUEST_TAG );
171
172            // The entry
173            buffer.put( TLV.getBytes( dnBytes.length ) );
174            buffer.put( dnBytes );
175        }
176        catch ( BufferOverflowException boe )
177        {
178            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
179        }
180
181        return buffer;
182    }
183}