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 import java.util.LinkedList;
26 import java.util.List;
27
28 import org.apache.directory.api.asn1.EncoderException;
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.LdapApiService;
34 import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
35 import org.apache.directory.api.ldap.codec.api.MessageDecorator;
36 import org.apache.directory.api.ldap.model.entry.Attribute;
37 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
38 import org.apache.directory.api.ldap.model.entry.Entry;
39 import org.apache.directory.api.ldap.model.entry.Value;
40 import org.apache.directory.api.ldap.model.exception.LdapException;
41 import org.apache.directory.api.ldap.model.message.SearchResultEntry;
42 import org.apache.directory.api.ldap.model.name.Dn;
43 import org.apache.directory.api.util.Strings;
44
45
46
47
48
49
50
51 public class SearchResultEntryDecorator extends MessageDecorator<SearchResultEntry> implements SearchResultEntry
52 {
53
54 private byte[] objectNameBytes;
55
56
57 private int searchResultEntryLength;
58
59
60 private int attributesLength;
61
62
63 private List<Integer> attributeLength;
64
65
66 private List<byte[]> attributeIds;
67
68
69 private List<Integer> valuesLength;
70
71
72 private Attribute currentAttribute;
73
74
75
76
77
78
79
80
81 public SearchResultEntryDecorator( LdapApiService codec, SearchResultEntry decoratedMessage )
82 {
83 super( codec, decoratedMessage );
84 }
85
86
87
88
89
90 public Attribute getCurrentAttribute()
91 {
92 return currentAttribute;
93 }
94
95
96
97
98
99
100
101
102 public void addAttribute( String type ) throws LdapException
103 {
104 currentAttribute = new DefaultAttribute( type );
105
106 getDecorated().getEntry().put( currentAttribute );
107 }
108
109
110
111
112
113
114
115
116 public void addAttribute( byte[] type ) throws LdapException
117 {
118 currentAttribute = new DefaultAttribute( type );
119
120 getDecorated().getEntry().put( currentAttribute );
121 }
122
123
124
125
126
127
128
129
130 public void addAttributeValue( Object value ) throws LdapException
131 {
132 if ( value instanceof String )
133 {
134 currentAttribute.add( ( String ) value );
135 }
136 else
137 {
138 currentAttribute.add( ( byte[] ) value );
139 }
140 }
141
142
143
144
145
146
147
148
149
150 @Override
151 public Dn getObjectName()
152 {
153 return getDecorated().getObjectName();
154 }
155
156
157
158
159
160 @Override
161 public void setObjectName( Dn objectName )
162 {
163 getDecorated().setObjectName( objectName );
164 }
165
166
167
168
169
170 @Override
171 public Entry getEntry()
172 {
173 return getDecorated().getEntry();
174 }
175
176
177
178
179
180 @Override
181 public void setEntry( Entry entry )
182 {
183 getDecorated().setEntry( entry );
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 @Override
232 public int computeLength()
233 {
234 Dn dn = getObjectName();
235
236 objectNameBytes = Strings.getBytesUtf8Ascii( dn.getName() );
237
238
239 searchResultEntryLength = 1 + TLV.getNbBytes( objectNameBytes.length ) + objectNameBytes.length;
240
241
242 attributesLength = 0;
243
244 Entry entry = getEntry();
245
246 if ( ( entry != null ) && ( entry.size() != 0 ) )
247 {
248 attributeLength = new LinkedList<>();
249 attributeIds = new LinkedList<>();
250 valuesLength = new LinkedList<>();
251
252
253 valuesLength = new LinkedList<>();
254
255
256 for ( Attribute attribute : entry )
257 {
258 int localAttributeLength;
259 int localValuesLength = 0;
260
261
262 byte[] attributeIdBytes = Strings.getBytesUtf8Ascii( attribute.getUpId() );
263 attributeIds.add( attributeIdBytes );
264 int idLength = attributeIdBytes.length;
265 localAttributeLength = 1 + TLV.getNbBytes( idLength ) + idLength;
266
267 if ( attribute.size() != 0 )
268 {
269
270 if ( attribute.size() > 0 )
271 {
272 localValuesLength = 0;
273
274 for ( org.apache.directory.api.ldap.model.entry.Value<?> value : attribute )
275 {
276 byte[] binaryValue = value.getBytes();
277 localValuesLength += 1 + TLV.getNbBytes( binaryValue.length ) + binaryValue.length;
278 }
279
280 localAttributeLength += 1 + TLV.getNbBytes( localValuesLength ) + localValuesLength;
281 }
282 else
283 {
284
285
286
287
288 localAttributeLength += 1 + 1;
289 }
290 }
291 else
292 {
293
294
295 localAttributeLength += 1 + 1;
296 }
297
298
299 attributesLength += 1 + TLV.getNbBytes( localAttributeLength ) + localAttributeLength;
300
301
302 attributeLength.add( localAttributeLength );
303 valuesLength.add( localValuesLength );
304 }
305 }
306
307 searchResultEntryLength += 1 + TLV.getNbBytes( attributesLength ) + attributesLength;
308
309
310 return 1 + TLV.getNbBytes( searchResultEntryLength ) + searchResultEntryLength;
311 }
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340 @Override
341 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
342 {
343 try
344 {
345
346 buffer.put( LdapCodecConstants.SEARCH_RESULT_ENTRY_TAG );
347 buffer.put( TLV.getBytes( searchResultEntryLength ) );
348
349
350 BerValue.encode( buffer, objectNameBytes );
351
352
353 buffer.put( UniversalTag.SEQUENCE.getValue() );
354 buffer.put( TLV.getBytes( attributesLength ) );
355
356
357 Entry entry = getEntry();
358
359 if ( ( entry != null ) && ( entry.size() != 0 ) )
360 {
361 int attributeNumber = 0;
362
363
364 for ( Attribute attribute : entry )
365 {
366
367 buffer.put( UniversalTag.SEQUENCE.getValue() );
368 int localAttributeLength = attributeLength.get( attributeNumber );
369 buffer.put( TLV.getBytes( localAttributeLength ) );
370
371
372 BerValue.encode( buffer, attributeIds.get( attributeNumber ) );
373
374
375 buffer.put( UniversalTag.SET.getValue() );
376 int localValuesLength = valuesLength.get( attributeNumber );
377 buffer.put( TLV.getBytes( localValuesLength ) );
378
379 if ( attribute.size() > 0 )
380 {
381 for ( Value<?> value : attribute )
382 {
383 BerValue.encode( buffer, value.getBytes() );
384 }
385 }
386
387
388 attributeNumber++;
389 }
390 }
391 }
392 catch ( BufferOverflowException boe )
393 {
394 throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
395 }
396
397 return buffer;
398 }
399 }