1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.extras.controls.ad_impl;
21
22 import java.nio.ByteBuffer;
23
24 import org.apache.directory.api.asn1.Asn1Object;
25 import org.apache.directory.api.asn1.DecoderException;
26 import org.apache.directory.api.asn1.EncoderException;
27 import org.apache.directory.api.asn1.ber.Asn1Decoder;
28 import org.apache.directory.api.asn1.ber.tlv.BerValue;
29 import org.apache.directory.api.asn1.ber.tlv.TLV;
30 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
31 import org.apache.directory.api.i18n.I18n;
32 import org.apache.directory.api.ldap.codec.api.ControlDecorator;
33 import org.apache.directory.api.ldap.codec.api.LdapApiService;
34 import org.apache.directory.api.ldap.extras.controls.ad.AdDirSync;
35 import org.apache.directory.api.ldap.extras.controls.ad.AdDirSyncFlag;
36 import org.apache.directory.api.ldap.extras.controls.ad.AdDirSyncImpl;
37 import org.apache.directory.api.util.Strings;
38
39
40
41
42
43
44 public class AdDirSyncDecorator extends ControlDecorator<AdDirSync> implements AdDirSync
45 {
46
47 private int adDirSyncLength;
48
49
50 private static final Asn1Decoder decoder = new Asn1Decoder();
51
52
53
54
55
56 public AdDirSyncDecorator( LdapApiService codec )
57 {
58 super( codec, new AdDirSyncImpl() );
59 }
60
61
62
63
64
65
66
67
68 public AdDirSyncDecorator( LdapApiService codec, AdDirSync control )
69 {
70 super( codec, control );
71 }
72
73
74
75
76
77 public int getParentFirst()
78 {
79 return getDecorated().getParentFirst();
80 }
81
82
83
84
85
86 public void setParentFirst( int parentFirst )
87 {
88 getDecorated().setParentFirst( parentFirst );
89 }
90
91
92
93
94
95 public AdDirSyncFlag getFlag()
96 {
97 return getDecorated().getFlag();
98 }
99
100
101
102
103
104 public void setFlag( AdDirSyncFlag flag )
105 {
106 getDecorated().setFlag( flag );
107 }
108
109
110
111
112
113 public int getMaxReturnLength()
114 {
115 return getDecorated().getMaxReturnLength();
116 }
117
118
119
120
121
122 public void setMaxReturnLength( int maxReturnLength )
123 {
124 getDecorated().setMaxReturnLength( maxReturnLength );
125 }
126
127
128
129
130
131 public byte[] getCookie()
132 {
133 return getDecorated().getCookie();
134 }
135
136
137
138
139
140 public void setCookie( byte[] cookie )
141 {
142
143 if ( !Strings.isEmpty( cookie ) )
144 {
145 byte[] copy = new byte[cookie.length];
146 System.arraycopy( cookie, 0, copy, 0, cookie.length );
147 getDecorated().setCookie( copy );
148 }
149 else
150 {
151 getDecorated().setCookie( null );
152 }
153 }
154
155
156
157
158
159
160
161
162
163
164 @Override
165 public int computeLength()
166 {
167
168 adDirSyncLength = 1 + TLV.getNbBytes( getParentFirst() ) + BerValue.getNbBytes( getParentFirst() );
169
170
171 adDirSyncLength += 1 + TLV.getNbBytes( getMaxReturnLength() ) + BerValue.getNbBytes( getMaxReturnLength() );
172
173
174 byte[] cookie = getCookie();
175
176 if ( cookie == null )
177 {
178 adDirSyncLength += 1 + 1;
179 }
180 else
181 {
182 adDirSyncLength += 1 + TLV.getNbBytes( cookie.length ) + cookie.length;
183 }
184
185 valueLength = 1 + TLV.getNbBytes( adDirSyncLength ) + adDirSyncLength;
186
187
188 return valueLength;
189 }
190
191
192
193
194
195
196
197
198
199 @Override
200 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
201 {
202 if ( buffer == null )
203 {
204 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
205 }
206
207
208 buffer.put( UniversalTag.SEQUENCE.getValue() );
209 buffer.put( TLV.getBytes( adDirSyncLength ) );
210
211
212 BerValue.encode( buffer, getParentFirst() );
213
214
215 BerValue.encode( buffer, getMaxReturnLength() );
216
217
218 BerValue.encode( buffer, getCookie() );
219
220 return buffer;
221 }
222
223
224
225
226
227 @Override
228 public byte[] getValue()
229 {
230 if ( value == null )
231 {
232 try
233 {
234 computeLength();
235 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
236
237
238 buffer.put( UniversalTag.SEQUENCE.getValue() );
239 buffer.put( TLV.getBytes( adDirSyncLength ) );
240
241
242 BerValue.encode( buffer, getParentFirst() );
243
244
245 BerValue.encode( buffer, getMaxReturnLength() );
246
247
248 BerValue.encode( buffer, getCookie() );
249
250 value = buffer.array();
251 }
252 catch ( Exception e )
253 {
254 return null;
255 }
256 }
257
258 return value;
259 }
260
261
262
263
264
265 public Asn1Object decode( byte[] controlBytes ) throws DecoderException
266 {
267 ByteBuffer bb = ByteBuffer.wrap( controlBytes );
268 AdDirSyncContainer container = new AdDirSyncContainer( getCodecService(), this );
269 decoder.decode( bb, container );
270 return this;
271 }
272 }