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 private byte[] encodedValueBytes;
52
53
54
55
56
57
58
59
60 public IntermediateResponseDecorator( LdapApiService codec, IntermediateResponse decoratedMessage )
61 {
62 super( codec, decoratedMessage );
63 }
64
65
66
67
68
69
70
71
72
73 @Override
74 public String getResponseName()
75 {
76 return getDecorated().getResponseName();
77 }
78
79
80
81
82
83 @Override
84 public void setResponseName( String oid )
85 {
86 getDecorated().setResponseName( oid );
87 }
88
89
90
91
92
93 @Override
94 public byte[] getResponseValue()
95 {
96 return getDecorated().getResponseValue();
97 }
98
99
100
101
102
103 @Override
104 public void setResponseValue( byte[] value )
105 {
106 getDecorated().setResponseValue( value );
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 @Override
132 public int computeLength()
133 {
134 intermediateResponseLength = 0;
135
136 if ( !Strings.isEmpty( getResponseName() ) )
137 {
138 responseNameBytes = Strings.getBytesUtf8( getResponseName() );
139
140 int responseNameLength = responseNameBytes.length;
141 intermediateResponseLength += 1 + TLV.getNbBytes( responseNameLength ) + responseNameLength;
142 }
143
144 encodedValueBytes = getResponseValue();
145
146 if ( encodedValueBytes != null )
147 {
148 intermediateResponseLength += 1 + TLV.getNbBytes( encodedValueBytes.length ) + encodedValueBytes.length;
149 }
150
151 return 1 + TLV.getNbBytes( intermediateResponseLength ) + intermediateResponseLength;
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 @Override
168 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
169 {
170 try
171 {
172
173 buffer.put( LdapCodecConstants.INTERMEDIATE_RESPONSE_TAG );
174 buffer.put( TLV.getBytes( intermediateResponseLength ) );
175
176
177 if ( ( responseNameBytes != null ) && ( responseNameBytes.length != 0 ) )
178 {
179 buffer.put( ( byte ) LdapCodecConstants.INTERMEDIATE_RESPONSE_NAME_TAG );
180 buffer.put( TLV.getBytes( responseNameBytes.length ) );
181 buffer.put( responseNameBytes );
182 }
183
184
185 if ( encodedValueBytes != null )
186 {
187 buffer.put( ( byte ) LdapCodecConstants.INTERMEDIATE_RESPONSE_VALUE_TAG );
188
189 buffer.put( TLV.getBytes( encodedValueBytes.length ) );
190
191 if ( encodedValueBytes.length != 0 )
192 {
193 buffer.put( encodedValueBytes );
194 }
195 }
196 }
197 catch ( BufferOverflowException boe )
198 {
199 throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
200 }
201
202 return buffer;
203 }
204 }