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