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;
021    
022    
023    import java.nio.ByteBuffer;
024    
025    import org.apache.directory.shared.asn1.codec.EncoderException;
026    
027    
028    /**
029     * A generic LdapResponse Object. It will contain the LdapResult.
030     * 
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sam, 11 avr 2009) $, 
033     */
034    public class LdapResponseCodec extends LdapMessageCodec
035    {
036        // ~ Instance fields
037        // ----------------------------------------------------------------------------
038    
039        /** The LdapResult element */
040        private LdapResultCodec ldapResult;
041    
042        /** The response length */
043        private int ldapResponseLength;
044    
045    
046        // ~ Constructors
047        // -------------------------------------------------------------------------------
048    
049        /**
050         * Creates a new LdapResponse object.
051         */
052        public LdapResponseCodec()
053        {
054            super();
055        }
056    
057    
058        // ~ Methods
059        // ------------------------------------------------------------------------------------
060    
061        /**
062         * Get the LdapResult
063         * 
064         * @return Returns the ldapResult.
065         */
066        public LdapResultCodec getLdapResult()
067        {
068            return ldapResult;
069        }
070    
071    
072        /**
073         * Set the ldap result
074         * 
075         * @param ldapResult The ldapResult to set.
076         */
077        public void setLdapResult( LdapResultCodec ldapResult )
078        {
079            this.ldapResult = ldapResult;
080        }
081    
082    
083        /**
084         * @return Returns the ldapResponseLength.
085         */
086        public int getLdapResponseLength()
087        {
088            return ldapResponseLength;
089        }
090    
091    
092        /**
093         * Compute the LdapResponse length LdapResponse : LdapResult
094         */
095        public int computeLength()
096        {
097            ldapResponseLength = ldapResult.computeLength();
098    
099            return ldapResponseLength;
100        }
101    
102    
103        /**
104         * Encode the AddResponse message to a PDU.
105         * 
106         * @param buffer The buffer where to put the PDU
107         * @return The PDU.
108         */
109        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
110        {
111            if ( buffer == null )
112            {
113                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
114            }
115    
116            // The ldapResult
117            ldapResult.encode( buffer );
118    
119            // The ldapResult
120            return buffer;
121        }
122    
123    
124        /**
125         * Get a String representation of an Response
126         * 
127         * @return An Response String
128         */
129        public String toString()
130        {
131            return ( ldapResult != null ? ldapResult.toString() : "" );
132        }
133    }