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