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.syncrepl_impl;
21
22
23 import java.nio.ByteBuffer;
24
25 import org.apache.directory.api.asn1.Asn1Object;
26 import org.apache.directory.api.asn1.DecoderException;
27 import org.apache.directory.api.asn1.EncoderException;
28 import org.apache.directory.api.asn1.ber.Asn1Decoder;
29 import org.apache.directory.api.asn1.ber.tlv.BerValue;
30 import org.apache.directory.api.asn1.ber.tlv.TLV;
31 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
32 import org.apache.directory.api.i18n.I18n;
33 import org.apache.directory.api.ldap.codec.api.ControlDecorator;
34 import org.apache.directory.api.ldap.codec.api.LdapApiService;
35 import org.apache.directory.api.ldap.extras.controls.syncrepl.syncDone.SyncDoneValue;
36 import org.apache.directory.api.ldap.extras.controls.syncrepl.syncDone.SyncDoneValueImpl;
37 import org.apache.directory.api.util.Strings;
38
39
40
41
42
43
44
45 public class SyncDoneValueDecorator extends ControlDecorator<SyncDoneValue> implements SyncDoneValue
46 {
47
48 private int syncDoneValueLength;
49
50
51 private static final Asn1Decoder DECODER = new Asn1Decoder();
52
53
54
55
56
57
58
59 public SyncDoneValueDecorator( LdapApiService codec )
60 {
61 super( codec, new SyncDoneValueImpl() );
62 }
63
64
65
66
67
68
69
70
71 public SyncDoneValueDecorator( LdapApiService codec, SyncDoneValue control )
72 {
73 super( codec, control );
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @Override
89 public int computeLength()
90 {
91
92 if ( getCookie() != null )
93 {
94 syncDoneValueLength = 1 + TLV.getNbBytes( getCookie().length ) + getCookie().length;
95 }
96
97
98 if ( isRefreshDeletes() )
99 {
100 syncDoneValueLength += 1 + 1 + 1;
101 }
102
103 valueLength = 1 + TLV.getNbBytes( syncDoneValueLength ) + syncDoneValueLength;
104
105
106 return valueLength;
107 }
108
109
110
111
112
113
114
115
116
117 @Override
118 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
119 {
120 if ( buffer == null )
121 {
122 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
123 }
124
125
126 buffer.put( UniversalTag.SEQUENCE.getValue() );
127 buffer.put( TLV.getBytes( syncDoneValueLength ) );
128
129 if ( getCookie() != null )
130 {
131 BerValue.encode( buffer, getCookie() );
132 }
133
134 if ( isRefreshDeletes() )
135 {
136 BerValue.encode( buffer, isRefreshDeletes() );
137 }
138
139 return buffer;
140 }
141
142
143
144
145
146 @Override
147 public byte[] getValue()
148 {
149 if ( value == null )
150 {
151 try
152 {
153 computeLength();
154 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
155
156
157 buffer.put( UniversalTag.SEQUENCE.getValue() );
158 buffer.put( TLV.getBytes( syncDoneValueLength ) );
159
160 if ( getCookie() != null )
161 {
162 BerValue.encode( buffer, getCookie() );
163 }
164
165 if ( isRefreshDeletes() )
166 {
167 BerValue.encode( buffer, isRefreshDeletes() );
168 }
169
170 value = buffer.array();
171 }
172 catch ( Exception e )
173 {
174 return null;
175 }
176 }
177
178 return value;
179 }
180
181
182
183
184
185 @Override
186 public byte[] getCookie()
187 {
188 return getDecorated().getCookie();
189 }
190
191
192
193
194
195 @Override
196 public void setCookie( byte[] cookie )
197 {
198
199 if ( !Strings.isEmpty( cookie ) )
200 {
201 byte[] copy = new byte[cookie.length];
202 System.arraycopy( cookie, 0, copy, 0, cookie.length );
203 getDecorated().setCookie( copy );
204 }
205 else
206 {
207 getDecorated().setCookie( null );
208 }
209 }
210
211
212
213
214
215 @Override
216 public boolean isRefreshDeletes()
217 {
218 return getDecorated().isRefreshDeletes();
219 }
220
221
222
223
224
225 @Override
226 public void setRefreshDeletes( boolean refreshDeletes )
227 {
228 getDecorated().setRefreshDeletes( refreshDeletes );
229 }
230
231
232
233
234
235 @Override
236 public Asn1Object decode( byte[] controlBytes ) throws DecoderException
237 {
238 ByteBuffer bb = ByteBuffer.wrap( controlBytes );
239 SyncDoneValueContainer container = new SyncDoneValueContainer( getCodecService(), this );
240 DECODER.decode( bb, container );
241 return this;
242 }
243 }