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