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.intermediate;
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.asn1.primitives.OID;
029    import org.apache.directory.shared.ldap.codec.LdapConstants;
030    import org.apache.directory.shared.ldap.codec.LdapResponseCodec;
031    import org.apache.directory.shared.ldap.util.StringTools;
032    
033    
034    /**
035     * A IntermediateResponse Message. Its syntax is :
036     *   IntermediateResponse ::= [APPLICATION 25] SEQUENCE {
037     *              responseName     [0] LDAPOID OPTIONAL,
038     *              responseValue    [1] OCTET STRING OPTIONAL }
039     * 
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sat, 07 Jun 2008) $, 
042     */
043    public class IntermediateResponseCodec extends LdapResponseCodec
044    {
045        // ~ Instance fields
046        // ----------------------------------------------------------------------------
047    
048        /** The name */
049        private OID responseName;
050    
051        /** The response */
052        private byte[] responseValue;
053    
054        /** The extended response length */
055        private int intermediateResponseLength;
056    
057        /** The OID length */
058        private int responseNameLength;
059    
060    
061        // ~ Constructors
062        // -------------------------------------------------------------------------------
063    
064        /**
065         * Creates a new IntermediateResponse object.
066         */
067        public IntermediateResponseCodec()
068        {
069            super();
070        }
071    
072    
073        // ~ Methods
074        // ------------------------------------------------------------------------------------
075    
076        /**
077         * Get the message type
078         * 
079         * @return Returns the type.
080         */
081        public int getMessageType()
082        {
083            return LdapConstants.INTERMEDIATE_RESPONSE;
084        }
085    
086    
087        /**
088         * Get the intermediate response name
089         * 
090         * @return Returns the name.
091         */
092        public String getResponseName()
093        {
094            return ( ( responseName == null ) ? "" : responseName.toString() );
095        }
096    
097    
098        /**
099         * Set the intermediate response name
100         * 
101         * @param responseName The name to set.
102         */
103        public void setResponseName( OID responseName )
104        {
105            this.responseName = responseName;
106        }
107    
108    
109        /**
110         * Get the intermediate response value
111         * 
112         * @return Returns the intermediate response value.
113         */
114        public byte[] getResponseValue()
115        {
116            return responseValue;
117        }
118    
119    
120        /**
121         * Set the intermediate response value
122         * 
123         * @param responseValue The intermediate response value to set.
124         */
125        public void setResponseValue( byte[] responseValue )
126        {
127            this.responseValue = responseValue;
128        }
129    
130    
131        /**
132         * Compute the intermediateResponse length
133         * 
134         * intermediateResponse :
135         * 
136         * 0x79 L1
137         *  |
138         * [+--> 0x80 L2 name
139         * [+--> 0x81 L3 response]]
140         * 
141         * L1 = [ + Length(0x80) + Length(L2) + L2
142         *      [ + Length(0x81) + Length(L3) + L3]]
143         * 
144         * Length(IntermediateResponse) = Length(0x79) + Length(L1) + L1
145         * 
146         * @return The IntermediateResponse length
147         */
148        public int computeLength()
149        {
150            intermediateResponseLength = 0;
151    
152            if ( responseName != null )
153            {
154                responseNameLength = responseName.toString().length();
155                intermediateResponseLength += 1 + TLV.getNbBytes( responseNameLength ) + responseNameLength;
156            }
157    
158            if ( responseValue != null )
159            {
160                intermediateResponseLength += 1 + TLV.getNbBytes( responseValue.length )
161                        + responseValue.length;
162            }
163    
164            return 1 + TLV.getNbBytes( intermediateResponseLength ) + intermediateResponseLength;
165        }
166    
167    
168        /**
169         * Encode the IntermediateResponse message to a PDU. 
170         * IntermediateResponse :
171         *   0x79 LL
172         *     [0x80 LL response name]
173         *     [0x81 LL responseValue]
174         * 
175         * @param buffer The buffer where to put the PDU
176         * @return The PDU.
177         */
178        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
179        {
180            if ( buffer == null )
181            {
182                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
183            }
184    
185            try
186            {
187                // The IntermediateResponse Tag
188                buffer.put( LdapConstants.INTERMEDIATE_RESPONSE_TAG );
189                buffer.put( TLV.getBytes( intermediateResponseLength ) );
190    
191                // The responseName, if any
192                if ( responseName != null )
193                {
194                    buffer.put( ( byte ) LdapConstants.INTERMEDIATE_RESPONSE_NAME_TAG );
195                    buffer.put( TLV.getBytes( responseNameLength ) );
196    
197                    if ( responseName.getOIDLength() != 0 )
198                    {
199                        buffer.put( StringTools.getBytesUtf8( responseName.toString() ) );
200                    }
201                }
202    
203                // The response, if any
204                if ( responseValue != null )
205                {
206                    buffer.put( ( byte ) LdapConstants.INTERMEDIATE_RESPONSE_VALUE_TAG );
207    
208                    buffer.put( TLV.getBytes( responseValue.length ) );
209    
210                    if ( responseValue.length != 0 )
211                    {
212                        buffer.put( responseValue );
213                    }
214                }
215            }
216            catch ( BufferOverflowException boe )
217            {
218                throw new EncoderException( "The PDU buffer size is too small !" );
219            }
220    
221            return buffer;
222        }
223    
224    
225        /**
226         * Get a String representation of an IntermediateResponse
227         * 
228         * @return An IntermediateResponse String
229         */
230        public String toString()
231        {
232    
233            StringBuffer sb = new StringBuffer();
234    
235            sb.append( "    Intermediate Response\n" );
236            sb.append( super.toString() );
237    
238            if ( responseName != null )
239            {
240                sb.append( "        Response name :'" ).append( responseName ).append( "'\n" );
241            }
242    
243            if ( responseValue != null )
244            {
245                sb.append( "        ResponseValue :'" );
246                sb.append( StringTools.dumpBytes( responseValue ) );
247                sb.append( "'\n" );
248            }
249    
250            return sb.toString();
251        }
252    }