View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.api.ldap.codec.decorators;
21  
22  
23  import java.nio.BufferOverflowException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.EncoderException;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.asn1.ber.tlv.TLV;
29  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30  import org.apache.directory.api.i18n.I18n;
31  import org.apache.directory.api.ldap.codec.api.LdapApiService;
32  import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
33  import org.apache.directory.api.ldap.model.entry.BinaryValue;
34  import org.apache.directory.api.ldap.model.entry.Value;
35  import org.apache.directory.api.ldap.model.message.CompareRequest;
36  import org.apache.directory.api.ldap.model.message.Control;
37  import org.apache.directory.api.ldap.model.name.Dn;
38  import org.apache.directory.api.util.Strings;
39  
40  
41  /**
42   * A decorator for the CompareRequest message
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class CompareRequestDecorator extends SingleReplyRequestDecorator<CompareRequest>
47      implements CompareRequest
48  {
49      /** The bytes of the attribute id used in the comparison */
50      private byte[] attrIdBytes;
51  
52      /** The bytes of the attribute value used in the comparison */
53      private byte[] attrValBytes;
54  
55      /** The compare request length */
56      private int compareRequestLength;
57  
58      /** The attribute value assertion length */
59      private int avaLength;
60  
61  
62      /**
63       * Makes a CompareRequest a MessageDecorator.
64       *
65       * @param codec The LDAP service instance
66       * @param decoratedMessage the decorated CompareRequest
67       */
68      public CompareRequestDecorator( LdapApiService codec, CompareRequest decoratedMessage )
69      {
70          super( codec, decoratedMessage );
71      }
72  
73  
74      //-------------------------------------------------------------------------
75      // The CompareRequest methods
76      //-------------------------------------------------------------------------
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public Dn getName()
83      {
84          return getDecorated().getName();
85      }
86  
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public CompareRequest setName( Dn name )
93      {
94          getDecorated().setName( name );
95  
96          return this;
97      }
98  
99  
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 }