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