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.api;
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.TLV;
28 import org.apache.directory.api.i18n.I18n;
29 import org.apache.directory.api.ldap.codec.decorators.LdapResultDecorator;
30 import org.apache.directory.api.ldap.codec.decorators.ResponseDecorator;
31 import org.apache.directory.api.ldap.model.message.ExtendedResponse;
32 import org.apache.directory.api.util.Strings;
33
34
35
36
37
38
39
40 public class ExtendedResponseDecorator<R extends ExtendedResponse> extends ResponseDecorator<R>
41 implements ExtendedResponse
42 {
43
44 private byte[] responseNameBytes;
45
46
47 private int extendedResponseLength;
48
49 protected byte[] responseValue;
50
51
52
53
54
55
56
57 public ExtendedResponseDecorator( LdapApiService codec, R decoratedMessage )
58 {
59 super( codec, decoratedMessage );
60 }
61
62
63
64
65
66
67
68 public byte[] getResponseNameBytes()
69 {
70 return responseNameBytes;
71 }
72
73
74
75
76
77
78
79 public void setResponseNameBytes( byte[] responseNameBytes )
80 {
81 this.responseNameBytes = responseNameBytes;
82 }
83
84
85
86
87
88
89
90 public void setExtendedResponseLength( int extendedResponseLength )
91 {
92 this.extendedResponseLength = extendedResponseLength;
93 }
94
95
96
97
98
99 public int getExtendedResponseLength()
100 {
101 return extendedResponseLength;
102 }
103
104
105
106
107
108
109
110
111
112 public String getResponseName()
113 {
114 return getDecorated().getResponseName();
115 }
116
117
118
119
120
121 public void setResponseName( String oid )
122 {
123 getDecorated().setResponseName( oid );
124 }
125
126
127
128
129
130 public byte[] getResponseValue()
131 {
132 return responseValue;
133 }
134
135
136
137
138
139 public void setResponseValue( byte[] responseValue )
140 {
141 this.responseValue = responseValue;
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 public int computeLength()
168 {
169 int ldapResultLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
170
171 int extendedResponseLength = ldapResultLength;
172
173 String id = getResponseName();
174
175 if ( !Strings.isEmpty( id ) )
176 {
177 byte[] idBytes = Strings.getBytesUtf8( id );
178 setResponseNameBytes( idBytes );
179 int idLength = idBytes.length;
180 extendedResponseLength += 1 + TLV.getNbBytes( idLength ) + idLength;
181 }
182
183 byte[] encodedValue = getResponseValue();
184
185 if ( encodedValue != null )
186 {
187 extendedResponseLength += 1 + TLV.getNbBytes( encodedValue.length ) + encodedValue.length;
188 }
189
190 setExtendedResponseLength( extendedResponseLength );
191
192 return 1 + TLV.getNbBytes( extendedResponseLength ) + extendedResponseLength;
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
207 {
208 try
209 {
210
211 buffer.put( LdapCodecConstants.EXTENDED_RESPONSE_TAG );
212 buffer.put( TLV.getBytes( getExtendedResponseLength() ) );
213
214
215 ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
216
217
218 byte[] idBytes = getResponseNameBytes();
219
220 if ( idBytes != null )
221 {
222 buffer.put( ( byte ) LdapCodecConstants.EXTENDED_RESPONSE_RESPONSE_NAME_TAG );
223 buffer.put( TLV.getBytes( idBytes.length ) );
224
225 if ( idBytes.length != 0 )
226 {
227 buffer.put( idBytes );
228 }
229 }
230
231
232 byte[] encodedValue = getResponseValue();
233
234 if ( encodedValue != null )
235 {
236 buffer.put( ( byte ) LdapCodecConstants.EXTENDED_RESPONSE_RESPONSE_TAG );
237
238 buffer.put( TLV.getBytes( encodedValue.length ) );
239
240 if ( encodedValue.length != 0 )
241 {
242 buffer.put( encodedValue );
243 }
244 }
245 }
246 catch ( BufferOverflowException boe )
247 {
248 throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
249 }
250
251 return buffer;
252 }
253 }