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.VirtualListViewResponse;
37  import org.apache.directory.api.ldap.extras.controls.vlv.VirtualListViewResponseImpl;
38  import org.apache.directory.api.ldap.extras.controls.vlv.VirtualListViewResultCode;
39  
40  
41  /**
42   * The VirtualListView response decorator
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class VirtualListViewResponseDecorator extends ControlDecorator<VirtualListViewResponse> implements
47      VirtualListViewResponse
48  {
49      private int vlvSeqLength;
50  
51      private static final Asn1Decoder decoder = new Asn1Decoder();
52  
53  
54      public VirtualListViewResponseDecorator( LdapApiService codec )
55      {
56          this( codec, new VirtualListViewResponseImpl() );
57      }
58  
59  
60      public VirtualListViewResponseDecorator( LdapApiService codec, VirtualListViewResponse vlvRequest )
61      {
62          super( codec, vlvRequest );
63      }
64  
65  
66      /**
67       * {@inheritDoc}
68       */
69      public int computeLength()
70      {
71          vlvSeqLength = 1 + 1 + BerValue.getNbBytes( getTargetPosition() );
72          vlvSeqLength += 1 + 1 + BerValue.getNbBytes( getContentCount() );
73  
74          // result code : always one byte long
75          vlvSeqLength += 1 + 1 + 1;
76  
77          if ( getContextId() != null )
78          {
79              vlvSeqLength += 1 + TLV.getNbBytes( getContextId().length ) + getContextId().length;
80          }
81  
82          valueLength = 1 + TLV.getNbBytes( vlvSeqLength ) + vlvSeqLength;
83  
84          return valueLength;
85      }
86  
87  
88      /**
89       * {@inheritDoc}
90       */
91      public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
92      {
93          if ( buffer == null )
94          {
95              throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
96          }
97  
98          buffer.put( UniversalTag.SEQUENCE.getValue() );
99          buffer.put( TLV.getBytes( vlvSeqLength ) );
100 
101         BerValue.encode( buffer, getTargetPosition() );
102         BerValue.encode( buffer, getContentCount() );
103 
104         BerValue.encodeEnumerated( buffer, getVirtualListViewResult().getVal() );
105 
106         if ( getContextId() != null )
107         {
108             BerValue.encode( buffer, getContextId() );
109         }
110 
111         return buffer;
112     }
113 
114 
115     /**
116      * {@inheritDoc}
117      */
118     public byte[] getValue()
119     {
120         if ( value == null )
121         {
122             try
123             {
124                 computeLength();
125                 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
126 
127                 value = encode( buffer ).array();
128             }
129             catch ( Exception e )
130             {
131                 return null;
132             }
133         }
134 
135         return value;
136     }
137 
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public Asn1Object decode( byte[] controlBytes ) throws DecoderException
144     {
145         ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
146         VirtualListViewResponseContainer container = new VirtualListViewResponseContainer( this, getCodecService() );
147         decoder.decode( buffer, container );
148 
149         return this;
150     }
151 
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public int getTargetPosition()
158     {
159         return getDecorated().getTargetPosition();
160     }
161 
162 
163     /**
164      * {@inheritDoc}
165      */
166     @Override
167     public void setTargetPosition( int targetPosition )
168     {
169         getDecorated().setTargetPosition( targetPosition );
170     }
171 
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public int getContentCount()
178     {
179         return getDecorated().getContentCount();
180     }
181 
182 
183     /**
184      * {@inheritDoc}
185      */
186     @Override
187     public void setContentCount( int contentCount )
188     {
189         getDecorated().setContentCount( contentCount );
190     }
191 
192 
193     /**
194      * {@inheritDoc}
195      */
196     @Override
197     public VirtualListViewResultCode getVirtualListViewResult()
198     {
199         return getDecorated().getVirtualListViewResult();
200     }
201 
202 
203     /**
204      * {@inheritDoc}
205      */
206     @Override
207     public void setVirtualListViewResult( VirtualListViewResultCode virtualListViewResult )
208     {
209         getDecorated().setVirtualListViewResult( virtualListViewResult );
210     }
211 
212 
213     /**
214      * {@inheritDoc}
215      */
216     @Override
217     public byte[] getContextId()
218     {
219         return getDecorated().getContextId();
220     }
221 
222 
223     /**
224      * {@inheritDoc}
225      */
226     @Override
227     public void setContextId( byte[] contextId )
228     {
229         getDecorated().setContextId( contextId );
230     }
231 
232 }