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.search;
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.LdapResponseCodec;
030    
031    
032    /**
033     * A SearchResultDone Message. Its syntax is : 
034     * 
035     * SearchResultDone ::= [APPLICATION 5] 
036     * 
037     * LDAPResult It's a Response, so it inherites from LdapResponse.
038     * 
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sam, 11 avr 2009) $, 
041     */
042    public class SearchResultDoneCodec extends LdapResponseCodec
043    {
044        // ~ Constructors
045        // -------------------------------------------------------------------------------
046    
047        /**
048         * Creates a new SearchResultDone object.
049         */
050        public SearchResultDoneCodec()
051        {
052            super();
053        }
054    
055    
056        /**
057         * Get the message type
058         * 
059         * @return Returns the type.
060         */
061        public int getMessageType()
062        {
063            return LdapConstants.SEARCH_RESULT_DONE;
064        }
065    
066    
067        /**
068         * Compute the SearchResultDone length 
069         * 
070         * SearchResultDone : 
071         * 0x65 L1 
072         *   | 
073         *   +--> LdapResult 
074         *   
075         * L1 = Length(LdapResult) 
076         * Length(SearchResultDone) = Length(0x65) + Length(L1) + L1
077         */
078        public int computeLength()
079        {
080            int ldapResponseLength = super.computeLength();
081    
082            return 1 + TLV.getNbBytes( ldapResponseLength ) + ldapResponseLength;
083        }
084    
085    
086        /**
087         * Encode the SearchResultDone message to a PDU.
088         * 
089         * @param buffer The buffer where to put the PDU
090         * @return The PDU.
091         */
092        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
093        {
094            if ( buffer == null )
095            {
096                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
097            }
098    
099            try
100            {
101                // The tag
102                buffer.put( LdapConstants.SEARCH_RESULT_DONE_TAG );
103                buffer.put( TLV.getBytes( getLdapResponseLength() ) );
104            }
105            catch ( BufferOverflowException boe )
106            {
107                throw new EncoderException( "The PDU buffer size is too small !" );
108            }
109    
110            // The ldapResult
111            return super.encode( buffer );
112        }
113    
114    
115        /**
116         * Get a String representation of a SearchResultDone
117         * 
118         * @return A SearchResultDone String
119         */
120        public String toString()
121        {
122    
123            StringBuffer sb = new StringBuffer();
124    
125            sb.append( "    Search Result Done\n" );
126            sb.append( super.toString() );
127    
128            return sb.toString();
129        }
130    }