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.i18n.I18n;
30 import org.apache.directory.api.ldap.codec.api.Decorator;
31 import org.apache.directory.api.ldap.codec.api.LdapApiService;
32 import org.apache.directory.api.ldap.codec.api.LdapEncoder;
33 import org.apache.directory.api.ldap.model.message.LdapResult;
34 import org.apache.directory.api.ldap.model.message.Referral;
35 import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
36 import org.apache.directory.api.ldap.model.name.Dn;
37 import org.apache.directory.api.util.Strings;
38
39
40
41
42
43
44
45 public class LdapResultDecorator implements LdapResult, Decorator<LdapResult>
46 {
47
48 private final LdapResult decoratedLdapResult;
49
50
51 private byte[] errorMessageBytes;
52
53
54 private byte[] matchedDnBytes;
55
56
57 private LdapApiService codec;
58
59 private static final byte[] DEFAULT_SUCCESS = new byte[]
60 { 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 };
61
62
63
64
65
66
67
68 public LdapResultDecorator( LdapResult decoratedLdapResult )
69 {
70 this.decoratedLdapResult = decoratedLdapResult;
71 }
72
73
74
75
76
77 public byte[] getErrorMessageBytes()
78 {
79 return errorMessageBytes;
80 }
81
82
83
84
85
86
87 public void setErrorMessageBytes( byte[] errorMessageBytes )
88 {
89 this.errorMessageBytes = errorMessageBytes;
90 }
91
92
93
94
95
96
97
98 public void setMatchedDnBytes( byte[] matchedDnBytes )
99 {
100 this.matchedDnBytes = matchedDnBytes;
101 }
102
103
104
105
106
107 public byte[] getMatchedDnBytes()
108 {
109 return matchedDnBytes;
110 }
111
112
113
114
115
116
117
118
119
120 public ResultCodeEnum getResultCode()
121 {
122 return decoratedLdapResult.getResultCode();
123 }
124
125
126
127
128
129 public void setResultCode( ResultCodeEnum resultCode )
130 {
131 decoratedLdapResult.setResultCode( resultCode );
132 }
133
134
135
136
137
138 public Dn getMatchedDn()
139 {
140 return decoratedLdapResult.getMatchedDn();
141 }
142
143
144
145
146
147 public void setMatchedDn( Dn dn )
148 {
149 decoratedLdapResult.setMatchedDn( dn );
150 }
151
152
153
154
155
156 public String getDiagnosticMessage()
157 {
158 return decoratedLdapResult.getDiagnosticMessage();
159 }
160
161
162
163
164
165 public void setDiagnosticMessage( String diagnosticMessage )
166 {
167 decoratedLdapResult.setDiagnosticMessage( diagnosticMessage );
168 }
169
170
171
172
173
174 public boolean isReferral()
175 {
176 return decoratedLdapResult.isReferral();
177 }
178
179
180
181
182
183 public Referral getReferral()
184 {
185 return decoratedLdapResult.getReferral();
186 }
187
188
189
190
191
192 public void setReferral( Referral referral )
193 {
194 decoratedLdapResult.setReferral( referral );
195 }
196
197
198
199
200
201 public String toString()
202 {
203 return decoratedLdapResult.toString();
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
232
233
234 public int computeLength()
235 {
236 if ( decoratedLdapResult.isDefaultSuccess() )
237 {
238
239 return DEFAULT_SUCCESS.length;
240 }
241
242 int ldapResultLength = 0;
243
244
245 ldapResultLength = 1 + 1 + BerValue.getNbBytes( getResultCode().getValue() );
246
247
248 if ( getMatchedDn() == null )
249 {
250 ldapResultLength += 1 + 1;
251 }
252 else
253 {
254 byte[] matchedDNBytes = Strings.getBytesUtf8Ascii( Strings.trimLeft( getMatchedDn().getName() ) );
255 ldapResultLength += 1 + TLV.getNbBytes( matchedDNBytes.length ) + matchedDNBytes.length;
256 setMatchedDnBytes( matchedDNBytes );
257 }
258
259
260 byte[] errorMessageBytes = Strings.getBytesUtf8Ascii( getDiagnosticMessage() );
261 ldapResultLength += 1 + TLV.getNbBytes( errorMessageBytes.length ) + errorMessageBytes.length;
262 setErrorMessageBytes( errorMessageBytes );
263
264 int referralLength = LdapEncoder.computeReferralLength( getReferral() );
265
266 if ( referralLength != 0 )
267 {
268
269 ldapResultLength += 1 + TLV.getNbBytes( referralLength ) + referralLength;
270 }
271
272 return ldapResultLength;
273 }
274
275
276
277
278
279
280
281
282 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
283 {
284 if ( buffer == null )
285 {
286 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
287 }
288
289 if ( decoratedLdapResult.isDefaultSuccess() )
290 {
291
292 buffer.put( DEFAULT_SUCCESS );
293
294 return buffer;
295 }
296
297 try
298 {
299
300 BerValue.encodeEnumerated( buffer, getResultCode().getValue() );
301 }
302 catch ( BufferOverflowException boe )
303 {
304 throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
305 }
306
307
308 BerValue.encode( buffer, getMatchedDnBytes() );
309
310
311 BerValue.encode( buffer, getErrorMessageBytes() );
312
313
314 Referral referral = getReferral();
315
316 if ( referral != null )
317 {
318 LdapEncoder.encodeReferral( buffer, referral );
319 }
320
321 return buffer;
322 }
323
324
325
326
327
328 public LdapResult getDecorated()
329 {
330 return decoratedLdapResult;
331 }
332
333
334
335
336
337 public LdapApiService getCodecService()
338 {
339 return codec;
340 }
341
342
343
344
345
346 public boolean isDefaultSuccess()
347 {
348 return decoratedLdapResult.isDefaultSuccess();
349 }
350 }