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.compare;
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    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
031    
032    
033    /**
034     * An CompareResponse Message. Its syntax is : 
035     * 
036     * CompareResponse ::= [APPLICATION 15] LDAPResult
037     * 
038     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039     * @version $Rev: 910150 $, $Date: 2010-02-15 02:37:34 +0100 (Mon, 15 Feb 2010) $, 
040     */
041    public class CompareResponseCodec extends LdapResponseCodec
042    {
043        // ~ Constructors
044        // -------------------------------------------------------------------------------
045    
046        /**
047         * Creates a new CompareResponse object.
048         */
049        public CompareResponseCodec()
050        {
051            super();
052        }
053    
054    
055        // ~ Methods
056        // ------------------------------------------------------------------------------------
057        /**
058         * Compute the CompareResponse length 
059         * 
060         * CompareResponse :
061         * 
062         * 0x6F L1
063         *  |
064         *  +--> LdapResult
065         * 
066         * L1 = Length(LdapResult)
067         * 
068         * Length(CompareResponse) = Length(0x6F) + Length(L1) + L1
069         */
070        protected int computeLengthProtocolOp()
071        {
072            int ldapResultLength = super.computeLdapResultLength();
073    
074            return 1 + TLV.getNbBytes( ldapResultLength ) + ldapResultLength;
075        }
076    
077    
078        /**
079         * Encode the CompareResponse message to a PDU.
080         * 
081         * @param buffer The buffer where to put the PDU
082         * @return The PDU.
083         */
084        protected void encodeProtocolOp( ByteBuffer buffer ) throws EncoderException
085        {
086            try
087            {
088                // The tag
089                buffer.put( LdapConstants.COMPARE_RESPONSE_TAG );
090                buffer.put( TLV.getBytes( getLdapResponseLength() ) );
091            }
092            catch ( BufferOverflowException boe )
093            {
094                throw new EncoderException( "The PDU buffer size is too small !" );
095            }
096    
097            // The LdapResult
098            super.encode( buffer );
099        }
100    
101        
102        /**
103         * Get the message type
104         * 
105         * @return Returns the type.
106         */
107        public MessageTypeEnum getMessageType()
108        {
109            return MessageTypeEnum.COMPARE_RESPONSE;
110        }
111    
112        
113        /**
114         * {@inheritDoc}
115         */
116        public String getMessageTypeName()
117        {
118            return "COMPARE_RESPONSE";
119        }
120    
121    
122        /**
123         * Get a String representation of an CompareResponse
124         * 
125         * @return An CompareResponse String
126         */
127        public String toString()
128        {
129    
130            StringBuffer sb = new StringBuffer();
131    
132            sb.append( "    Compare Response\n" );
133            sb.append( super.toString() );
134    
135            return sb.toString();
136        }
137    }