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
69 public LdapResultDecorator( LdapApiService codec, LdapResult decoratedLdapResult )
70 {
71 this.decoratedLdapResult = decoratedLdapResult;
72 this.codec = codec;
73 }
74
75
76
77
78
79
80
81
82
83 @Override
84 public ResultCodeEnum getResultCode()
85 {
86 return decoratedLdapResult.getResultCode();
87 }
88
89
90
91
92
93 @Override
94 public void setResultCode( ResultCodeEnum resultCode )
95 {
96 decoratedLdapResult.setResultCode( resultCode );
97 }
98
99
100
101
102
103 @Override
104 public Dn getMatchedDn()
105 {
106 return decoratedLdapResult.getMatchedDn();
107 }
108
109
110
111
112
113 @Override
114 public void setMatchedDn( Dn dn )
115 {
116 decoratedLdapResult.setMatchedDn( dn );
117 }
118
119
120
121
122
123 @Override
124 public String getDiagnosticMessage()
125 {
126 return decoratedLdapResult.getDiagnosticMessage();
127 }
128
129
130
131
132
133 @Override
134 public void setDiagnosticMessage( String diagnosticMessage )
135 {
136 decoratedLdapResult.setDiagnosticMessage( diagnosticMessage );
137 }
138
139
140
141
142
143 @Override
144 public boolean isReferral()
145 {
146 return decoratedLdapResult.isReferral();
147 }
148
149
150
151
152
153 @Override
154 public Referral getReferral()
155 {
156 return decoratedLdapResult.getReferral();
157 }
158
159
160
161
162
163 @Override
164 public void setReferral( Referral referral )
165 {
166 decoratedLdapResult.setReferral( referral );
167 }
168
169
170
171
172
173 @Override
174 public String toString()
175 {
176 return decoratedLdapResult.toString();
177 }
178
179
180
181
182
183
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 @Override
210 public int computeLength()
211 {
212 if ( decoratedLdapResult.isDefaultSuccess() )
213 {
214
215 return DEFAULT_SUCCESS.length;
216 }
217
218 int ldapResultLength;
219
220
221 ldapResultLength = 1 + 1 + BerValue.getNbBytes( getResultCode().getValue() );
222
223
224 if ( getMatchedDn() == null )
225 {
226 ldapResultLength += 1 + 1;
227 }
228 else
229 {
230 matchedDnBytes = Strings.getBytesUtf8Ascii( Strings.trimLeft( getMatchedDn().getName() ) );
231 ldapResultLength += 1 + TLV.getNbBytes( matchedDnBytes.length ) + matchedDnBytes.length;
232 }
233
234
235 errorMessageBytes = Strings.getBytesUtf8Ascii( getDiagnosticMessage() );
236 ldapResultLength += 1 + TLV.getNbBytes( errorMessageBytes.length ) + errorMessageBytes.length;
237
238 int referralLength = LdapEncoder.computeReferralLength( getReferral() );
239
240 if ( referralLength != 0 )
241 {
242
243 ldapResultLength += 1 + TLV.getNbBytes( referralLength ) + referralLength;
244 }
245
246 return ldapResultLength;
247 }
248
249
250
251
252
253
254
255
256 @Override
257 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
258 {
259 if ( buffer == null )
260 {
261 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
262 }
263
264 if ( decoratedLdapResult.isDefaultSuccess() )
265 {
266
267 buffer.put( DEFAULT_SUCCESS );
268
269 return buffer;
270 }
271
272 try
273 {
274
275 BerValue.encodeEnumerated( buffer, getResultCode().getValue() );
276 }
277 catch ( BufferOverflowException boe )
278 {
279 throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
280 }
281
282
283 BerValue.encode( buffer, matchedDnBytes );
284
285
286 BerValue.encode( buffer, errorMessageBytes );
287
288
289 Referral referral = getReferral();
290
291 if ( referral != null )
292 {
293 LdapEncoder.encodeReferral( buffer, referral );
294 }
295
296 return buffer;
297 }
298
299
300
301
302
303 @Override
304 public LdapResult getDecorated()
305 {
306 return decoratedLdapResult;
307 }
308
309
310
311
312
313 @Override
314 public LdapApiService getCodecService()
315 {
316 return codec;
317 }
318
319
320
321
322
323 @Override
324 public boolean isDefaultSuccess()
325 {
326 return decoratedLdapResult.isDefaultSuccess();
327 }
328 }