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.LdapConstants;
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
046    /** The bytes containing the Dn */
047    private byte[] dnBytes;
048
049
050    /**
051     * Makes a DeleteRequest a MessageDecorator.
052     *
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    public Dn getName()
069    {
070        return getDecorated().getName();
071    }
072
073
074    /**
075     * {@inheritDoc}
076     */
077    public DeleteRequest setName( Dn name )
078    {
079        getDecorated().setName( name );
080
081        return this;
082    }
083
084
085    /**
086     * {@inheritDoc}
087     */
088    public DeleteRequest setMessageId( int messageId )
089    {
090        super.setMessageId( messageId );
091
092        return this;
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    public DeleteRequest addControl( Control control )
100    {
101        return ( DeleteRequest ) super.addControl( control );
102    }
103
104
105    /**
106     * {@inheritDoc}
107     */
108    public DeleteRequest addAllControls( Control[] controls )
109    {
110        return ( DeleteRequest ) super.addAllControls( controls );
111    }
112
113
114    /**
115     * {@inheritDoc}
116     */
117    public DeleteRequest removeControl( Control control )
118    {
119        return ( DeleteRequest ) super.removeControl( control );
120    }
121
122
123    //-------------------------------------------------------------------------
124    // The Decorator methods
125    //-------------------------------------------------------------------------
126    /**
127     * Compute the DelRequest length
128     * 
129     * DelRequest :
130     * 0x4A L1 entry
131     * 
132     * L1 = Length(entry)
133     * Length(DelRequest) = Length(0x4A) + Length(L1) + L1
134     */
135    public int computeLength()
136    {
137        dnBytes = Strings.getBytesUtf8( getName().getName() );
138        int dnLength = dnBytes.length;
139
140        // The entry
141        return 1 + TLV.getNbBytes( dnLength ) + dnLength;
142    }
143
144
145    /**
146     * Encode the DelRequest message to a PDU.
147     * 
148     * DelRequest :
149     * 0x4A LL entry
150     * 
151     * @param buffer The buffer where to put the PDU
152     */
153    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
154    {
155        try
156        {
157            // The DelRequest Tag
158            buffer.put( LdapConstants.DEL_REQUEST_TAG );
159
160            // The entry
161            buffer.put( TLV.getBytes( dnBytes.length ) );
162            buffer.put( dnBytes );
163        }
164        catch ( BufferOverflowException boe )
165        {
166            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
167        }
168
169        return buffer;
170    }
171}