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