1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
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 public VirtualListViewRequestDecorator( LdapApiService codec )
55 {
56 this( codec, new VirtualListViewRequestImpl() );
57 }
58
59
60 public VirtualListViewRequestDecorator( LdapApiService codec, VirtualListViewRequest vlvRequest )
61 {
62 super( codec, vlvRequest );
63 }
64
65
66
67
68
69 public int computeLength()
70 {
71 vlvSeqLength = 1 + 1 + BerValue.getNbBytes( getBeforeCount() );
72 vlvSeqLength += 1 + 1 + BerValue.getNbBytes( getAfterCount() );
73
74 if ( hasOffset() )
75 {
76 targetSeqLength = 1 + 1 + BerValue.getNbBytes( getOffset() );
77 targetSeqLength += 1 + 1 + BerValue.getNbBytes( getContentCount() );
78
79 vlvSeqLength += 1 + 1 + targetSeqLength;
80 }
81 else
82 {
83 byte[] assertionValue = getAssertionValue();
84
85 if ( assertionValue != null )
86 {
87 targetSeqLength = 1 + TLV.getNbBytes( assertionValue.length ) + assertionValue.length;
88 }
89 else
90 {
91 targetSeqLength = 1 + 1;
92 }
93
94 vlvSeqLength += targetSeqLength;
95 }
96
97 if ( getContextId() != null )
98 {
99 vlvSeqLength += 1 + TLV.getNbBytes( getContextId().length ) + getContextId().length;
100 }
101
102 valueLength = 1 + TLV.getNbBytes( vlvSeqLength ) + vlvSeqLength;
103
104 return valueLength;
105 }
106
107
108
109
110
111 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
112 {
113 if ( buffer == null )
114 {
115 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
116 }
117
118 buffer.put( UniversalTag.SEQUENCE.getValue() );
119 buffer.put( TLV.getBytes( vlvSeqLength ) );
120
121 BerValue.encode( buffer, getBeforeCount() );
122 BerValue.encode( buffer, getAfterCount() );
123
124 if ( hasOffset() )
125 {
126
127 buffer.put( ( byte ) VirtualListViewerTags.BY_OFFSET_TAG.getValue() );
128 buffer.put( TLV.getBytes( targetSeqLength ) );
129
130
131 BerValue.encode( buffer, getOffset() );
132 BerValue.encode( buffer, getContentCount() );
133 }
134 else
135 {
136 buffer.put( ( byte ) VirtualListViewerTags.ASSERTION_VALUE_TAG.getValue() );
137 byte[] value = getAssertionValue();
138
139 if ( value != null )
140 {
141 buffer.put( TLV.getBytes( value.length ) );
142 }
143 else
144 {
145 buffer.put( TLV.getBytes( 0 ) );
146 }
147
148
149 buffer.put( value );
150 }
151
152 if ( getContextId() != null )
153 {
154 BerValue.encode( buffer, getContextId() );
155 }
156
157 return buffer;
158 }
159
160
161
162
163
164 public byte[] getValue()
165 {
166 if ( value == null )
167 {
168 try
169 {
170 computeLength();
171 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
172
173 value = encode( buffer ).array();
174 }
175 catch ( Exception e )
176 {
177 return null;
178 }
179 }
180
181 return value;
182 }
183
184
185
186
187
188 @Override
189 public Asn1Object decode( byte[] controlBytes ) throws DecoderException
190 {
191 ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
192 VirtualListViewRequestContainer container = new VirtualListViewRequestContainer( this, getCodecService() );
193 decoder.decode( buffer, container );
194 return this;
195 }
196
197
198
199
200
201 @Override
202 public int getBeforeCount()
203 {
204 return getDecorated().getBeforeCount();
205 }
206
207
208
209
210
211 @Override
212 public void setBeforeCount( int beforeCount )
213 {
214 getDecorated().setBeforeCount( beforeCount );
215 }
216
217
218
219
220
221 @Override
222 public int getAfterCount()
223 {
224 return getDecorated().getAfterCount();
225 }
226
227
228
229
230
231 @Override
232 public void setAfterCount( int afterCount )
233 {
234 getDecorated().setAfterCount( afterCount );
235 }
236
237
238
239
240
241 @Override
242 public int getOffset()
243 {
244 return getDecorated().getOffset();
245 }
246
247
248
249
250
251 @Override
252 public void setOffset( int offset )
253 {
254 getDecorated().setOffset( offset );
255 }
256
257
258
259
260
261 @Override
262 public int getContentCount()
263 {
264 return getDecorated().getContentCount();
265 }
266
267
268
269
270
271 @Override
272 public void setContentCount( int contentCount )
273 {
274 getDecorated().setContentCount( contentCount );
275 }
276
277
278
279
280
281 @Override
282 public byte[] getContextId()
283 {
284 return getDecorated().getContextId();
285 }
286
287
288
289
290
291 @Override
292 public void setContextId( byte[] contextId )
293 {
294 getDecorated().setContextId( contextId );
295 }
296
297
298
299
300
301 @Override
302 public byte[] getAssertionValue()
303 {
304 return getDecorated().getAssertionValue();
305 }
306
307
308
309
310
311 @Override
312 public void setAssertionValue( byte[] assertionValue )
313 {
314 getDecorated().setAssertionValue( assertionValue );
315 }
316
317
318
319
320
321 @Override
322 public boolean hasOffset()
323 {
324 return getDecorated().hasOffset();
325 }
326
327
328
329
330
331 @Override
332 public boolean hasAssertionValue()
333 {
334 return getDecorated().hasAssertionValue();
335 }
336
337 }