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 codec The LDAP service instance
066     * @param decoratedMessage the decorated CompareRequest
067     */
068    public CompareRequestDecorator( LdapApiService codec, CompareRequest decoratedMessage )
069    {
070        super( codec, decoratedMessage );
071    }
072
073
074    //-------------------------------------------------------------------------
075    // The CompareRequest methods
076    //-------------------------------------------------------------------------
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public Dn getName()
083    {
084        return getDecorated().getName();
085    }
086
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public CompareRequest setName( Dn name )
093    {
094        getDecorated().setName( name );
095
096        return this;
097    }
098
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public Value<?> getAssertionValue()
105    {
106        return getDecorated().getAssertionValue();
107    }
108
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public CompareRequest setAssertionValue( String value )
115    {
116        getDecorated().setAssertionValue( value );
117
118        return this;
119    }
120
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public CompareRequest setAssertionValue( byte[] value )
127    {
128        getDecorated().setAssertionValue( value );
129
130        return this;
131    }
132
133
134    /**
135     * {@inheritDoc}
136     */
137    @Override
138    public String getAttributeId()
139    {
140        return getDecorated().getAttributeId();
141    }
142
143
144    /**
145     * {@inheritDoc}
146     */
147    @Override
148    public CompareRequest setAttributeId( String attrId )
149    {
150        getDecorated().setAttributeId( attrId );
151
152        return this;
153    }
154
155
156    /**
157     * {@inheritDoc}
158     */
159    @Override
160    public CompareRequest setMessageId( int messageId )
161    {
162        super.setMessageId( messageId );
163
164        return this;
165    }
166
167
168    /**
169     * {@inheritDoc}
170     */
171    @Override
172    public CompareRequest addControl( Control control )
173    {
174        return ( CompareRequest ) super.addControl( control );
175    }
176
177
178    /**
179     * {@inheritDoc}
180     */
181    @Override
182    public CompareRequest addAllControls( Control[] controls )
183    {
184        return ( CompareRequest ) super.addAllControls( controls );
185    }
186
187
188    /**
189     * {@inheritDoc}
190     */
191    @Override
192    public CompareRequest removeControl( Control control )
193    {
194        return ( CompareRequest ) super.removeControl( control );
195    }
196
197
198    //-------------------------------------------------------------------------
199    // The Decorator methods
200    //-------------------------------------------------------------------------
201    /**
202     * Compute the CompareRequest length
203     * <br>
204     * CompareRequest :
205     * <pre>
206     * 0x6E L1
207     *   |
208     *   +--&gt; 0x04 L2 entry
209     *   +--&gt; 0x30 L3 (ava)
210     *         |
211     *         +--&gt; 0x04 L4 attributeDesc
212     *         +--&gt; 0x04 L5 assertionValue
213     * 
214     * L3 = Length(0x04) + Length(L4) + L4 + Length(0x04) +
215     *      Length(L5) + L5
216     * Length(CompareRequest) = Length(0x6E) + Length(L1) + L1 +
217     *      Length(0x04) + Length(L2) + L2 + Length(0x30) + Length(L3) + L3
218     * </pre>
219     * 
220     * @return The CompareRequest PDU's length
221     */
222    @Override
223    public int computeLength()
224    {
225        // The entry Dn
226        Dn entry = getName();
227        compareRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( entry ) ) + Dn.getNbBytes( entry );
228
229        // The attribute value assertion
230        attrIdBytes = Strings.getBytesUtf8( getAttributeId() );
231        avaLength = 1 + TLV.getNbBytes( attrIdBytes.length ) + attrIdBytes.length;
232
233        org.apache.directory.api.ldap.model.entry.Value<?> assertionValue = getAssertionValue();
234
235        if ( assertionValue instanceof BinaryValue )
236        {
237            attrValBytes = getAssertionValue().getBytes();
238            avaLength += 1 + TLV.getNbBytes( attrValBytes.length ) + attrValBytes.length;
239        }
240        else
241        {
242            attrValBytes = Strings.getBytesUtf8( getAssertionValue().getString() );
243            avaLength += 1 + TLV.getNbBytes( attrValBytes.length ) + attrValBytes.length;
244        }
245
246        compareRequestLength += 1 + TLV.getNbBytes( avaLength ) + avaLength;
247
248        return 1 + TLV.getNbBytes( compareRequestLength ) + compareRequestLength;
249    }
250
251
252    /**
253     * Encode the CompareRequest message to a PDU.
254     * <pre>
255     * CompareRequest :
256     *   0x6E LL
257     *     0x04 LL entry
258     *     0x30 LL attributeValueAssertion
259     *       0x04 LL attributeDesc
260     *       0x04 LL assertionValue
261     * </pre>
262     * 
263     * @param buffer The buffer where to put the PDU
264     */
265    @Override
266    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
267    {
268        try
269        {
270            // The CompareRequest Tag
271            buffer.put( LdapCodecConstants.COMPARE_REQUEST_TAG );
272            buffer.put( TLV.getBytes( compareRequestLength ) );
273
274            // The entry
275            BerValue.encode( buffer, Dn.getBytes( getName() ) );
276
277            // The attributeValueAssertion sequence Tag
278            buffer.put( UniversalTag.SEQUENCE.getValue() );
279            buffer.put( TLV.getBytes( avaLength ) );
280        }
281        catch ( BufferOverflowException boe )
282        {
283            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
284        }
285
286        // The attributeDesc
287        BerValue.encode( buffer, attrIdBytes );
288
289        // The assertionValue
290        BerValue.encode( buffer, attrValBytes );
291
292        return buffer;
293    }
294}