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     */
020    package org.apache.directory.shared.ldap.codec.del;
021    
022    
023    import java.nio.BufferOverflowException;
024    import java.nio.ByteBuffer;
025    
026    import org.apache.directory.shared.asn1.ber.tlv.TLV;
027    import org.apache.directory.shared.asn1.codec.EncoderException;
028    import org.apache.directory.shared.ldap.codec.LdapConstants;
029    import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
030    import org.apache.directory.shared.ldap.name.LdapDN;
031    
032    
033    /**
034     * A DelRequest Message. 
035     * 
036     * Its syntax is : 
037     * 
038     * DelRequest ::= [APPLICATION 10] LDAPDN
039     * 
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sam, 11 avr 2009) $, 
042     */
043    public class DelRequestCodec extends LdapMessageCodec
044    {
045        // ~ Instance fields
046        // ----------------------------------------------------------------------------
047    
048        /** The entry to be deleted */
049        private LdapDN entry;
050    
051    
052        // ~ Constructors
053        // -------------------------------------------------------------------------------
054    
055        /**
056         * Creates a new DelRequest object.
057         */
058        public DelRequestCodec()
059        {
060            super();
061        }
062    
063    
064        // ~ Methods
065        // ------------------------------------------------------------------------------------
066    
067        /**
068         * Get the message type
069         * 
070         * @return Returns the type.
071         */
072        public int getMessageType()
073        {
074            return LdapConstants.DEL_REQUEST;
075        }
076    
077    
078        /**
079         * Get the entry to be deleted
080         * 
081         * @return Returns the entry.
082         */
083        public LdapDN getEntry()
084        {
085            return entry;
086        }
087    
088    
089        /**
090         * Set the entry to be deleted
091         * 
092         * @param entry The entry to set.
093         */
094        public void setEntry( LdapDN entry )
095        {
096            this.entry = entry;
097        }
098    
099    
100        /**
101         * Compute the DelRequest length 
102         * 
103         * DelRequest : 
104         * 0x4A L1 entry 
105         * 
106         * L1 = Length(entry) 
107         * Length(DelRequest) = Length(0x4A) + Length(L1) + L1
108         */
109        public int computeLength()
110        {
111            // The entry
112            return 1 + TLV.getNbBytes( LdapDN.getNbBytes( entry ) ) + LdapDN.getNbBytes( entry );
113        }
114    
115    
116        /**
117         * Encode the DelRequest message to a PDU. 
118         * 
119         * DelRequest : 
120         * 0x4A LL entry
121         * 
122         * @param buffer The buffer where to put the PDU
123         * @return The PDU.
124         */
125        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
126        {
127            if ( buffer == null )
128            {
129                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
130            }
131    
132            try
133            {
134                // The DelRequest Tag
135                buffer.put( LdapConstants.DEL_REQUEST_TAG );
136    
137                // The entry
138                buffer.put( TLV.getBytes( LdapDN.getNbBytes( entry ) ) );
139                buffer.put( LdapDN.getBytes( entry ) );
140            }
141            catch ( BufferOverflowException boe )
142            {
143                throw new EncoderException( "The PDU buffer size is too small !" );
144            }
145    
146            return buffer;
147        }
148    
149    
150        /**
151         * Return a String representing a DelRequest
152         * 
153         * @return A DelRequest String
154         */
155        public String toString()
156        {
157    
158            StringBuffer sb = new StringBuffer();
159    
160            sb.append( "    Del request\n" );
161            sb.append( "        Entry : '" ).append( entry ).append( "'\n" );
162    
163            return sb.toString();
164        }
165    }