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.BerValue;
028import org.apache.directory.api.asn1.ber.tlv.TLV;
029import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
030import org.apache.directory.api.i18n.I18n;
031import org.apache.directory.api.ldap.codec.api.LdapApiService;
032import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
033import org.apache.directory.api.ldap.model.entry.BinaryValue;
034import org.apache.directory.api.ldap.model.entry.Value;
035import org.apache.directory.api.ldap.model.message.CompareRequest;
036import org.apache.directory.api.ldap.model.message.Control;
037import org.apache.directory.api.ldap.model.name.Dn;
038import org.apache.directory.api.util.Strings;
039
040
041/**
042 * A decorator for the CompareRequest message
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class CompareRequestDecorator extends SingleReplyRequestDecorator<CompareRequest>
047    implements CompareRequest
048{
049    /** The bytes of the attribute id used in the comparison */
050    private byte[] attrIdBytes;
051
052    /** The bytes of the attribute value used in the comparison */
053    private byte[] attrValBytes;
054
055    /** The compare request length */
056    private int compareRequestLength;
057
058    /** The attribute value assertion length */
059    private int avaLength;
060
061
062    /**
063     * Makes a CompareRequest a MessageDecorator.
064     *
065     * @param decoratedMessage the decorated CompareRequest
066     */
067    public CompareRequestDecorator( LdapApiService codec, CompareRequest decoratedMessage )
068    {
069        super( codec, decoratedMessage );
070    }
071
072
073    //-------------------------------------------------------------------------
074    // The CompareRequest methods
075    //-------------------------------------------------------------------------
076
077    /**
078     * {@inheritDoc}
079     */
080    public Dn getName()
081    {
082        return getDecorated().getName();
083    }
084
085
086    /**
087     * {@inheritDoc}
088     */
089    public CompareRequest setName( Dn name )
090    {
091        getDecorated().setName( name );
092
093        return this;
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
100    public Value<?> getAssertionValue()
101    {
102        return getDecorated().getAssertionValue();
103    }
104
105
106    /**
107     * {@inheritDoc}
108     */
109    public CompareRequest setAssertionValue( String value )
110    {
111        getDecorated().setAssertionValue( value );
112
113        return this;
114    }
115
116
117    /**
118     * {@inheritDoc}
119     */
120    public CompareRequest setAssertionValue( byte[] value )
121    {
122        getDecorated().setAssertionValue( value );
123
124        return this;
125    }
126
127
128    /**
129     * {@inheritDoc}
130     */
131    public String getAttributeId()
132    {
133        return getDecorated().getAttributeId();
134    }
135
136
137    /**
138     * {@inheritDoc}
139     */
140    public CompareRequest setAttributeId( String attrId )
141    {
142        getDecorated().setAttributeId( attrId );
143
144        return this;
145    }
146
147
148    /**
149     * {@inheritDoc}
150     */
151    public CompareRequest setMessageId( int messageId )
152    {
153        super.setMessageId( messageId );
154
155        return this;
156    }
157
158
159    /**
160     * {@inheritDoc}
161     */
162    public CompareRequest addControl( Control control )
163    {
164        return ( CompareRequest ) super.addControl( control );
165    }
166
167
168    /**
169     * {@inheritDoc}
170     */
171    public CompareRequest addAllControls( Control[] controls )
172    {
173        return ( CompareRequest ) super.addAllControls( controls );
174    }
175
176
177    /**
178     * {@inheritDoc}
179     */
180    public CompareRequest removeControl( Control control )
181    {
182        return ( CompareRequest ) super.removeControl( control );
183    }
184
185
186    //-------------------------------------------------------------------------
187    // The Decorator methods
188    //-------------------------------------------------------------------------
189    /**
190     * Compute the CompareRequest length
191     * 
192     * CompareRequest :
193     * 0x6E L1
194     *   |
195     *   +--> 0x04 L2 entry
196     *   +--> 0x30 L3 (ava)
197     *         |
198     *         +--> 0x04 L4 attributeDesc
199     *         +--> 0x04 L5 assertionValue
200     * 
201     * L3 = Length(0x04) + Length(L4) + L4 + Length(0x04) +
202     *      Length(L5) + L5
203     * Length(CompareRequest) = Length(0x6E) + Length(L1) + L1 +
204     *      Length(0x04) + Length(L2) + L2 + Length(0x30) + Length(L3) + L3
205     * 
206     * @return The CompareRequest PDU's length
207     */
208    public int computeLength()
209    {
210        // The entry Dn
211        Dn entry = getName();
212        compareRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( entry ) ) + Dn.getNbBytes( entry );
213
214        // The attribute value assertion
215        attrIdBytes = Strings.getBytesUtf8( getAttributeId() );
216        avaLength = 1 + TLV.getNbBytes( attrIdBytes.length ) + attrIdBytes.length;
217
218        org.apache.directory.api.ldap.model.entry.Value<?> assertionValue = getAssertionValue();
219
220        if ( assertionValue instanceof BinaryValue )
221        {
222            attrValBytes = getAssertionValue().getBytes();
223            avaLength += 1 + TLV.getNbBytes( attrValBytes.length ) + attrValBytes.length;
224        }
225        else
226        {
227            attrValBytes = Strings.getBytesUtf8( getAssertionValue().getString() );
228            avaLength += 1 + TLV.getNbBytes( attrValBytes.length ) + attrValBytes.length;
229        }
230
231        compareRequestLength += 1 + TLV.getNbBytes( avaLength ) + avaLength;
232
233        return 1 + TLV.getNbBytes( compareRequestLength ) + compareRequestLength;
234    }
235
236
237    /**
238     * Encode the CompareRequest message to a PDU.
239     * 
240     * CompareRequest :
241     *   0x6E LL
242     *     0x04 LL entry
243     *     0x30 LL attributeValueAssertion
244     *       0x04 LL attributeDesc
245     *       0x04 LL assertionValue
246     * 
247     * @param buffer The buffer where to put the PDU
248     */
249    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
250    {
251        try
252        {
253            // The CompareRequest Tag
254            buffer.put( LdapCodecConstants.COMPARE_REQUEST_TAG );
255            buffer.put( TLV.getBytes( compareRequestLength ) );
256
257            // The entry
258            BerValue.encode( buffer, Dn.getBytes( getName() ) );
259
260            // The attributeValueAssertion sequence Tag
261            buffer.put( UniversalTag.SEQUENCE.getValue() );
262            buffer.put( TLV.getBytes( avaLength ) );
263        }
264        catch ( BufferOverflowException boe )
265        {
266            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
267        }
268
269        // The attributeDesc
270        BerValue.encode( buffer, attrIdBytes );
271
272        // The assertionValue
273        BerValue.encode( buffer, attrValBytes );
274
275        return buffer;
276    }
277}