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 */
020package org.apache.directory.api.ldap.codec.decorators;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.api.asn1.EncoderException;
027import org.apache.directory.api.asn1.ber.tlv.TLV;
028import org.apache.directory.api.i18n.I18n;
029import org.apache.directory.api.ldap.codec.api.LdapApiService;
030import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
031import org.apache.directory.api.ldap.model.message.CompareResponse;
032
033
034/**
035 * A decorator for the CompareResponse message
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class CompareResponseDecorator extends ResponseDecorator<CompareResponse>
040    implements CompareResponse
041{
042    /** The encoded compareResponse length */
043    private int compareResponseLength;
044
045
046    /**
047     * Makes a CompareResponse a MessageDecorator.
048     *
049     * @param codec The LDAP service instance
050     * @param decoratedMessage the decorated CompareResponse
051     */
052    public CompareResponseDecorator( LdapApiService codec, CompareResponse decoratedMessage )
053    {
054        super( codec, decoratedMessage );
055    }
056
057
058    //-------------------------------------------------------------------------
059    // The CompareResponse methods
060    //-------------------------------------------------------------------------
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public boolean isTrue()
067    {
068        return getDecorated().isTrue();
069    }
070
071
072    //-------------------------------------------------------------------------
073    // The Decorator methods
074    //-------------------------------------------------------------------------
075
076    /**
077     * Compute the CompareResponse length 
078     * <br>
079     * CompareResponse :
080     * <pre>
081     * 0x6F L1
082     *  |
083     *  +--&gt; LdapResult
084     * 
085     * L1 = Length(LdapResult)
086     * 
087     * Length(CompareResponse) = Length(0x6F) + Length(L1) + L1
088     * </pre>
089     */
090    @Override
091    public int computeLength()
092    {
093        compareResponseLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
094
095        return 1 + TLV.getNbBytes( compareResponseLength ) + compareResponseLength;
096    }
097
098
099    /**
100     * Encode the CompareResponse message to a PDU.
101     * 
102     * @param buffer The buffer where to put the PDU
103     */
104    @Override
105    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
106    {
107        try
108        {
109            // The CompareResponse Tag
110            buffer.put( LdapCodecConstants.COMPARE_RESPONSE_TAG );
111            buffer.put( TLV.getBytes( compareResponseLength ) );
112
113            // The LdapResult
114            ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
115        }
116        catch ( BufferOverflowException boe )
117        {
118            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
119        }
120
121        return buffer;
122    }
123}