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