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.codec.decorators;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25
26 import org.apache.directory.api.asn1.EncoderException;
27 import org.apache.directory.api.asn1.ber.tlv.BerValue;
28 import org.apache.directory.api.asn1.ber.tlv.TLV;
29 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30 import org.apache.directory.api.i18n.I18n;
31 import org.apache.directory.api.ldap.codec.api.LdapApiService;
32 import org.apache.directory.api.ldap.codec.api.LdapConstants;
33 import org.apache.directory.api.ldap.model.entry.BinaryValue;
34 import org.apache.directory.api.ldap.model.entry.Value;
35 import org.apache.directory.api.ldap.model.message.CompareRequest;
36 import org.apache.directory.api.ldap.model.message.Control;
37 import org.apache.directory.api.ldap.model.name.Dn;
38 import org.apache.directory.api.util.Strings;
39
40
41
42
43
44
45
46 public class CompareRequestDecorator extends SingleReplyRequestDecorator<CompareRequest>
47 implements CompareRequest
48 {
49
50 private byte[] attrIdBytes;
51
52
53 private byte[] attrValBytes;
54
55
56 private int compareRequestLength;
57
58
59 private int avaLength;
60
61
62
63
64
65
66
67 public CompareRequestDecorator( LdapApiService codec, CompareRequest decoratedMessage )
68 {
69 super( codec, decoratedMessage );
70 }
71
72
73
74
75
76
77 public void setCompareRequestLength( int compareRequestLength )
78 {
79 this.compareRequestLength = compareRequestLength;
80 }
81
82
83
84
85
86 public int getCompareRequestLength()
87 {
88 return compareRequestLength;
89 }
90
91
92
93
94
95
96 public void setAvaLength( int avaLength )
97 {
98 this.avaLength = avaLength;
99 }
100
101
102
103
104
105 public int getAvaLength()
106 {
107 return avaLength;
108 }
109
110
111
112
113
114
115
116 public byte[] getAttrIdBytes()
117 {
118 return attrIdBytes;
119 }
120
121
122
123
124
125
126
127 public void setAttrIdBytes( byte[] attrIdBytes )
128 {
129 this.attrIdBytes = attrIdBytes;
130 }
131
132
133
134
135
136
137
138 public byte[] getAttrValBytes()
139 {
140 return attrValBytes;
141 }
142
143
144
145
146
147
148
149 public void setAttrValBytes( byte[] attrValBytes )
150 {
151 this.attrValBytes = attrValBytes;
152 }
153
154
155
156
157
158
159
160
161
162 public Dn getName()
163 {
164 return getDecorated().getName();
165 }
166
167
168
169
170
171 public CompareRequest setName( Dn name )
172 {
173 getDecorated().setName( name );
174
175 return this;
176 }
177
178
179
180
181
182 public Value<?> getAssertionValue()
183 {
184 return getDecorated().getAssertionValue();
185 }
186
187
188
189
190
191 public CompareRequest setAssertionValue( String value )
192 {
193 getDecorated().setAssertionValue( value );
194
195 return this;
196 }
197
198
199
200
201
202 public CompareRequest setAssertionValue( byte[] value )
203 {
204 getDecorated().setAssertionValue( value );
205
206 return this;
207 }
208
209
210
211
212
213 public String getAttributeId()
214 {
215 return getDecorated().getAttributeId();
216 }
217
218
219
220
221
222 public CompareRequest setAttributeId( String attrId )
223 {
224 getDecorated().setAttributeId( attrId );
225
226 return this;
227 }
228
229
230
231
232
233 public CompareRequest setMessageId( int messageId )
234 {
235 super.setMessageId( messageId );
236
237 return this;
238 }
239
240
241
242
243
244 public CompareRequest addControl( Control control )
245 {
246 return ( CompareRequest ) super.addControl( control );
247 }
248
249
250
251
252
253 public CompareRequest addAllControls( Control[] controls )
254 {
255 return ( CompareRequest ) super.addAllControls( controls );
256 }
257
258
259
260
261
262 public CompareRequest removeControl( Control control )
263 {
264 return ( CompareRequest ) super.removeControl( control );
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290 public int computeLength()
291 {
292
293 Dn entry = getName();
294 int compareRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( entry ) ) + Dn.getNbBytes( entry );
295
296
297 byte[] attributeIdBytes = Strings.getBytesUtf8( getAttributeId() );
298 int avaLength = 1 + TLV.getNbBytes( attributeIdBytes.length ) + attributeIdBytes.length;
299 setAttrIdBytes( attributeIdBytes );
300
301 org.apache.directory.api.ldap.model.entry.Value<?> assertionValue = getAssertionValue();
302
303 if ( assertionValue instanceof BinaryValue )
304 {
305 byte[] value = getAssertionValue().getBytes();
306 avaLength += 1 + TLV.getNbBytes( value.length ) + value.length;
307 setAttrValBytes( value );
308 }
309 else
310 {
311 byte[] value = Strings.getBytesUtf8( getAssertionValue().getString() );
312 avaLength += 1 + TLV.getNbBytes( value.length ) + value.length;
313 setAttrValBytes( value );
314 }
315
316 setAvaLength( avaLength );
317 compareRequestLength += 1 + TLV.getNbBytes( avaLength ) + avaLength;
318 setCompareRequestLength( compareRequestLength );
319
320 return 1 + TLV.getNbBytes( compareRequestLength ) + compareRequestLength;
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
337 {
338 try
339 {
340
341 buffer.put( LdapConstants.COMPARE_REQUEST_TAG );
342 buffer.put( TLV.getBytes( getCompareRequestLength() ) );
343
344
345 BerValue.encode( buffer, Dn.getBytes( getName() ) );
346
347
348 buffer.put( UniversalTag.SEQUENCE.getValue() );
349 buffer.put( TLV.getBytes( getAvaLength() ) );
350 }
351 catch ( BufferOverflowException boe )
352 {
353 throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
354 }
355
356
357 BerValue.encode( buffer, getAttrIdBytes() );
358
359
360 BerValue.encode( buffer, getAttrValBytes() );
361
362 return buffer;
363 }
364 }