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