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 public SyncDoneValueDecorator( LdapApiService codec )
58 {
59 super( codec, new SyncDoneValueImpl() );
60 }
61
62
63
64
65
66
67
68
69 public SyncDoneValueDecorator( LdapApiService codec, SyncDoneValue control )
70 {
71 super( codec, control );
72 }
73
74
75
76
77
78
79
80
81
82 @Override
83 public int computeLength()
84 {
85
86 if ( getCookie() != null )
87 {
88 syncDoneValueLength = 1 + TLV.getNbBytes( getCookie().length ) + getCookie().length;
89 }
90
91
92 if ( isRefreshDeletes() )
93 {
94 syncDoneValueLength += 1 + 1 + 1;
95 }
96
97 valueLength = 1 + TLV.getNbBytes( syncDoneValueLength ) + syncDoneValueLength;
98
99
100 return valueLength;
101 }
102
103
104
105
106
107
108
109
110
111 @Override
112 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
113 {
114 if ( buffer == null )
115 {
116 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
117 }
118
119
120 buffer.put( UniversalTag.SEQUENCE.getValue() );
121 buffer.put( TLV.getBytes( syncDoneValueLength ) );
122
123 if ( getCookie() != null )
124 {
125 BerValue.encode( buffer, getCookie() );
126 }
127
128 if ( isRefreshDeletes() )
129 {
130 BerValue.encode( buffer, isRefreshDeletes() );
131 }
132
133 return buffer;
134 }
135
136
137
138
139
140 @Override
141 public byte[] getValue()
142 {
143 if ( value == null )
144 {
145 try
146 {
147 computeLength();
148 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
149
150
151 buffer.put( UniversalTag.SEQUENCE.getValue() );
152 buffer.put( TLV.getBytes( syncDoneValueLength ) );
153
154 if ( getCookie() != null )
155 {
156 BerValue.encode( buffer, getCookie() );
157 }
158
159 if ( isRefreshDeletes() )
160 {
161 BerValue.encode( buffer, isRefreshDeletes() );
162 }
163
164 value = buffer.array();
165 }
166 catch ( Exception e )
167 {
168 return null;
169 }
170 }
171
172 return value;
173 }
174
175
176
177
178
179 public byte[] getCookie()
180 {
181 return getDecorated().getCookie();
182 }
183
184
185
186
187
188 public void setCookie( byte[] cookie )
189 {
190
191 if ( !Strings.isEmpty( cookie ) )
192 {
193 byte[] copy = new byte[cookie.length];
194 System.arraycopy( cookie, 0, copy, 0, cookie.length );
195 getDecorated().setCookie( copy );
196 }
197 else
198 {
199 getDecorated().setCookie( null );
200 }
201 }
202
203
204
205
206
207 public boolean isRefreshDeletes()
208 {
209 return getDecorated().isRefreshDeletes();
210 }
211
212
213
214
215
216 public void setRefreshDeletes( boolean refreshDeletes )
217 {
218 getDecorated().setRefreshDeletes( refreshDeletes );
219 }
220
221
222
223
224
225 public Asn1Object decode( byte[] controlBytes ) throws DecoderException
226 {
227 ByteBuffer bb = ByteBuffer.wrap( controlBytes );
228 SyncDoneValueContainer container = new SyncDoneValueContainer( getCodecService(), this );
229 decoder.decode( bb, container );
230 return this;
231 }
232 }