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  
21  package org.apache.directory.api.ldap.extras.controls.vlv_impl;
22  
23  
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.Asn1Object;
27  import org.apache.directory.api.asn1.DecoderException;
28  import org.apache.directory.api.asn1.EncoderException;
29  import org.apache.directory.api.asn1.ber.Asn1Decoder;
30  import org.apache.directory.api.asn1.ber.tlv.BerValue;
31  import org.apache.directory.api.asn1.ber.tlv.TLV;
32  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
33  import org.apache.directory.api.i18n.I18n;
34  import org.apache.directory.api.ldap.codec.api.ControlDecorator;
35  import org.apache.directory.api.ldap.codec.api.LdapApiService;
36  import org.apache.directory.api.ldap.extras.controls.vlv.VirtualListViewRequest;
37  import org.apache.directory.api.ldap.extras.controls.vlv.VirtualListViewRequestImpl;
38  
39  
40  /**
41   * The VirtualListView decorator
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class VirtualListViewRequestDecorator extends ControlDecorator<VirtualListViewRequest> implements
46      VirtualListViewRequest
47  {
48      private int vlvSeqLength;
49      private int targetSeqLength;
50  
51      private static final Asn1Decoder DECODER = new Asn1Decoder();
52  
53  
54      /**
55       * Creates a new instance of VirtualListViewRequestDecorator.
56       * 
57       * @param codec The LDAP Service to use
58       */
59      public VirtualListViewRequestDecorator( LdapApiService codec )
60      {
61          this( codec, new VirtualListViewRequestImpl() );
62      }
63  
64  
65      /**
66       * Creates a new instance of VirtualListViewRequestDecorator.
67       * 
68       * @param codec The LDAP Service to use
69       * @param vlvRequest The VLV request to use
70       */
71      public VirtualListViewRequestDecorator( LdapApiService codec, VirtualListViewRequest vlvRequest )
72      {
73          super( codec, vlvRequest );
74      }
75  
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public int computeLength()
82      {
83          vlvSeqLength = 1 + 1 + BerValue.getNbBytes( getBeforeCount() );
84          vlvSeqLength += 1 + 1 + BerValue.getNbBytes( getAfterCount() );
85  
86          if ( hasOffset() )
87          {
88              targetSeqLength = 1 + 1 + BerValue.getNbBytes( getOffset() );
89              targetSeqLength += 1 + 1 + BerValue.getNbBytes( getContentCount() );
90  
91              vlvSeqLength += 1 + 1 + targetSeqLength;
92          }
93          else
94          {
95              byte[] assertionValue = getAssertionValue();
96  
97              if ( assertionValue != null )
98              {
99                  targetSeqLength = 1 + TLV.getNbBytes( assertionValue.length ) + assertionValue.length;
100             }
101             else
102             {
103                 targetSeqLength = 1 + 1;
104             }
105 
106             vlvSeqLength += targetSeqLength;
107         }
108 
109         if ( getContextId() != null )
110         {
111             vlvSeqLength += 1 + TLV.getNbBytes( getContextId().length ) + getContextId().length;
112         }
113 
114         valueLength = 1 + TLV.getNbBytes( vlvSeqLength ) + vlvSeqLength;
115 
116         return valueLength;
117     }
118 
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
125     {
126         if ( buffer == null )
127         {
128             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
129         }
130 
131         buffer.put( UniversalTag.SEQUENCE.getValue() );
132         buffer.put( TLV.getBytes( vlvSeqLength ) );
133 
134         BerValue.encode( buffer, getBeforeCount() );
135         BerValue.encode( buffer, getAfterCount() );
136 
137         if ( hasOffset() )
138         {
139             // The byOffset tag
140             buffer.put( ( byte ) VirtualListViewerTags.BY_OFFSET_TAG.getValue() );
141             buffer.put( TLV.getBytes( targetSeqLength ) );
142 
143             // The by offset values
144             BerValue.encode( buffer, getOffset() );
145             BerValue.encode( buffer, getContentCount() );
146         }
147         else
148         {
149             buffer.put( ( byte ) VirtualListViewerTags.ASSERTION_VALUE_TAG.getValue() );
150             byte[] value = getAssertionValue();
151 
152             if ( value != null )
153             {
154                 buffer.put( TLV.getBytes( value.length ) );
155 
156                 // The by assertionValue value
157                 buffer.put( value );
158             }
159             else
160             {
161                 buffer.put( TLV.getBytes( 0 ) );
162             }
163         }
164 
165         if ( getContextId() != null )
166         {
167             BerValue.encode( buffer, getContextId() );
168         }
169 
170         return buffer;
171     }
172 
173 
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public byte[] getValue()
179     {
180         if ( value == null )
181         {
182             try
183             {
184                 computeLength();
185                 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
186 
187                 value = encode( buffer ).array();
188             }
189             catch ( Exception e )
190             {
191                 return null;
192             }
193         }
194 
195         return value;
196     }
197 
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     public Asn1Object decode( byte[] controlBytes ) throws DecoderException
204     {
205         ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
206         VirtualListViewRequestContainer container = new VirtualListViewRequestContainer( this, getCodecService() );
207         DECODER.decode( buffer, container );
208         return this;
209     }
210 
211 
212     /**
213      * {@inheritDoc}
214      */
215     @Override
216     public int getBeforeCount()
217     {
218         return getDecorated().getBeforeCount();
219     }
220 
221 
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public void setBeforeCount( int beforeCount )
227     {
228         getDecorated().setBeforeCount( beforeCount );
229     }
230 
231 
232     /**
233      * {@inheritDoc}
234      */
235     @Override
236     public int getAfterCount()
237     {
238         return getDecorated().getAfterCount();
239     }
240 
241 
242     /**
243      * {@inheritDoc}
244      */
245     @Override
246     public void setAfterCount( int afterCount )
247     {
248         getDecorated().setAfterCount( afterCount );
249     }
250 
251 
252     /**
253      * {@inheritDoc}
254      */
255     @Override
256     public int getOffset()
257     {
258         return getDecorated().getOffset();
259     }
260 
261 
262     /**
263      * {@inheritDoc}
264      */
265     @Override
266     public void setOffset( int offset )
267     {
268         getDecorated().setOffset( offset );
269     }
270 
271 
272     /**
273      * {@inheritDoc}
274      */
275     @Override
276     public int getContentCount()
277     {
278         return getDecorated().getContentCount();
279     }
280 
281 
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public void setContentCount( int contentCount )
287     {
288         getDecorated().setContentCount( contentCount );
289     }
290 
291 
292     /**
293      * {@inheritDoc}
294      */
295     @Override
296     public byte[] getContextId()
297     {
298         return getDecorated().getContextId();
299     }
300 
301 
302     /**
303      * {@inheritDoc}
304      */
305     @Override
306     public void setContextId( byte[] contextId )
307     {
308         getDecorated().setContextId( contextId );
309     }
310 
311 
312     /**
313      * {@inheritDoc}
314      */
315     @Override
316     public byte[] getAssertionValue()
317     {
318         return getDecorated().getAssertionValue();
319     }
320 
321 
322     /**
323      * {@inheritDoc}
324      */
325     @Override
326     public void setAssertionValue( byte[] assertionValue )
327     {
328         getDecorated().setAssertionValue( assertionValue );
329     }
330 
331 
332     /**
333      * {@inheritDoc}
334      */
335     @Override
336     public boolean hasOffset()
337     {
338         return getDecorated().hasOffset();
339     }
340 
341 
342     /**
343      * {@inheritDoc}
344      */
345     @Override
346     public boolean hasAssertionValue()
347     {
348         return getDecorated().hasAssertionValue();
349     }
350 
351 }