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