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      /**
55       * Create a new SyncRequestValueDecorator instance 
56       * 
57       * @param codec The LDAP API service to use
58       */
59      public VirtualListViewResponseDecorator( LdapApiService codec )
60      {
61          this( codec, new VirtualListViewResponseImpl() );
62      }
63  
64  
65      /**
66       * Create a new SyncRequestValueDecorator instance 
67       * 
68       * @param codec The LDAP API service to use
69       * @param vlvRequest The decorated VLV request
70       */
71      public VirtualListViewResponseDecorator( LdapApiService codec, VirtualListViewResponse 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( getTargetPosition() );
84          vlvSeqLength += 1 + 1 + BerValue.getNbBytes( getContentCount() );
85  
86          // result code : always one byte long
87          vlvSeqLength += 1 + 1 + 1;
88  
89          if ( getContextId() != null )
90          {
91              vlvSeqLength += 1 + TLV.getNbBytes( getContextId().length ) + getContextId().length;
92          }
93  
94          valueLength = 1 + TLV.getNbBytes( vlvSeqLength ) + vlvSeqLength;
95  
96          return valueLength;
97      }
98  
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
105     {
106         if ( buffer == null )
107         {
108             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
109         }
110 
111         buffer.put( UniversalTag.SEQUENCE.getValue() );
112         buffer.put( TLV.getBytes( vlvSeqLength ) );
113 
114         BerValue.encode( buffer, getTargetPosition() );
115         BerValue.encode( buffer, getContentCount() );
116 
117         BerValue.encodeEnumerated( buffer, getVirtualListViewResult().getValue() );
118 
119         if ( getContextId() != null )
120         {
121             BerValue.encode( buffer, getContextId() );
122         }
123 
124         return buffer;
125     }
126 
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public byte[] getValue()
133     {
134         if ( value == null )
135         {
136             try
137             {
138                 computeLength();
139                 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
140 
141                 value = encode( buffer ).array();
142             }
143             catch ( Exception e )
144             {
145                 return null;
146             }
147         }
148 
149         return value;
150     }
151 
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public Asn1Object decode( byte[] controlBytes ) throws DecoderException
158     {
159         ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
160         VirtualListViewResponseContainer container = new VirtualListViewResponseContainer( this, getCodecService() );
161         DECODER.decode( buffer, container );
162 
163         return this;
164     }
165 
166 
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     public int getTargetPosition()
172     {
173         return getDecorated().getTargetPosition();
174     }
175 
176 
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public void setTargetPosition( int targetPosition )
182     {
183         getDecorated().setTargetPosition( targetPosition );
184     }
185 
186 
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     public int getContentCount()
192     {
193         return getDecorated().getContentCount();
194     }
195 
196 
197     /**
198      * {@inheritDoc}
199      */
200     @Override
201     public void setContentCount( int contentCount )
202     {
203         getDecorated().setContentCount( contentCount );
204     }
205 
206 
207     /**
208      * {@inheritDoc}
209      */
210     @Override
211     public VirtualListViewResultCode getVirtualListViewResult()
212     {
213         return getDecorated().getVirtualListViewResult();
214     }
215 
216 
217     /**
218      * {@inheritDoc}
219      */
220     @Override
221     public void setVirtualListViewResult( VirtualListViewResultCode virtualListViewResult )
222     {
223         getDecorated().setVirtualListViewResult( virtualListViewResult );
224     }
225 
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public byte[] getContextId()
232     {
233         return getDecorated().getContextId();
234     }
235 
236 
237     /**
238      * {@inheritDoc}
239      */
240     @Override
241     public void setContextId( byte[] contextId )
242     {
243         getDecorated().setContextId( contextId );
244     }
245 
246 }