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.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
43
44
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
56
57
58
59 public VirtualListViewResponseDecorator( LdapApiService codec )
60 {
61 this( codec, new VirtualListViewResponseImpl() );
62 }
63
64
65
66
67
68
69
70
71 public VirtualListViewResponseDecorator( LdapApiService codec, VirtualListViewResponse vlvRequest )
72 {
73 super( codec, vlvRequest );
74 }
75
76
77
78
79
80 @Override
81 public int computeLength()
82 {
83 vlvSeqLength = 1 + 1 + BerValue.getNbBytes( getTargetPosition() );
84 vlvSeqLength += 1 + 1 + BerValue.getNbBytes( getContentCount() );
85
86
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
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
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
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
169
170 @Override
171 public int getTargetPosition()
172 {
173 return getDecorated().getTargetPosition();
174 }
175
176
177
178
179
180 @Override
181 public void setTargetPosition( int targetPosition )
182 {
183 getDecorated().setTargetPosition( targetPosition );
184 }
185
186
187
188
189
190 @Override
191 public int getContentCount()
192 {
193 return getDecorated().getContentCount();
194 }
195
196
197
198
199
200 @Override
201 public void setContentCount( int contentCount )
202 {
203 getDecorated().setContentCount( contentCount );
204 }
205
206
207
208
209
210 @Override
211 public VirtualListViewResultCode getVirtualListViewResult()
212 {
213 return getDecorated().getVirtualListViewResult();
214 }
215
216
217
218
219
220 @Override
221 public void setVirtualListViewResult( VirtualListViewResultCode virtualListViewResult )
222 {
223 getDecorated().setVirtualListViewResult( virtualListViewResult );
224 }
225
226
227
228
229
230 @Override
231 public byte[] getContextId()
232 {
233 return getDecorated().getContextId();
234 }
235
236
237
238
239
240 @Override
241 public void setContextId( byte[] contextId )
242 {
243 getDecorated().setContextId( contextId );
244 }
245
246 }