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