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.asn1.ber.tlv.UniversalTag;
30 import org.apache.directory.api.i18n.I18n;
31 import org.apache.directory.api.ldap.codec.api.LdapApiService;
32 import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
33 import org.apache.directory.api.ldap.model.entry.BinaryValue;
34 import org.apache.directory.api.ldap.model.entry.Value;
35 import org.apache.directory.api.ldap.model.message.CompareRequest;
36 import org.apache.directory.api.ldap.model.message.Control;
37 import org.apache.directory.api.ldap.model.name.Dn;
38 import org.apache.directory.api.util.Strings;
39
40
41
42
43
44
45
46 public class CompareRequestDecorator extends SingleReplyRequestDecorator<CompareRequest>
47 implements CompareRequest
48 {
49
50 private byte[] attrIdBytes;
51
52
53 private byte[] attrValBytes;
54
55
56 private int compareRequestLength;
57
58
59 private int avaLength;
60
61
62
63
64
65
66
67
68 public CompareRequestDecorator( LdapApiService codec, CompareRequest decoratedMessage )
69 {
70 super( codec, decoratedMessage );
71 }
72
73
74
75
76
77
78
79
80
81 @Override
82 public Dn getName()
83 {
84 return getDecorated().getName();
85 }
86
87
88
89
90
91 @Override
92 public CompareRequest setName( Dn name )
93 {
94 getDecorated().setName( name );
95
96 return this;
97 }
98
99
100
101
102
103 @Override
104 public Value<?> getAssertionValue()
105 {
106 return getDecorated().getAssertionValue();
107 }
108
109
110
111
112
113 @Override
114 public CompareRequest setAssertionValue( String value )
115 {
116 getDecorated().setAssertionValue( value );
117
118 return this;
119 }
120
121
122
123
124
125 @Override
126 public CompareRequest setAssertionValue( byte[] value )
127 {
128 getDecorated().setAssertionValue( value );
129
130 return this;
131 }
132
133
134
135
136
137 @Override
138 public String getAttributeId()
139 {
140 return getDecorated().getAttributeId();
141 }
142
143
144
145
146
147 @Override
148 public CompareRequest setAttributeId( String attrId )
149 {
150 getDecorated().setAttributeId( attrId );
151
152 return this;
153 }
154
155
156
157
158
159 @Override
160 public CompareRequest setMessageId( int messageId )
161 {
162 super.setMessageId( messageId );
163
164 return this;
165 }
166
167
168
169
170
171 @Override
172 public CompareRequest addControl( Control control )
173 {
174 return ( CompareRequest ) super.addControl( control );
175 }
176
177
178
179
180
181 @Override
182 public CompareRequest addAllControls( Control[] controls )
183 {
184 return ( CompareRequest ) super.addAllControls( controls );
185 }
186
187
188
189
190
191 @Override
192 public CompareRequest removeControl( Control control )
193 {
194 return ( CompareRequest ) super.removeControl( control );
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 @Override
223 public int computeLength()
224 {
225
226 Dn entry = getName();
227 compareRequestLength = 1 + TLV.getNbBytes( Dn.getNbBytes( entry ) ) + Dn.getNbBytes( entry );
228
229
230 attrIdBytes = Strings.getBytesUtf8( getAttributeId() );
231 avaLength = 1 + TLV.getNbBytes( attrIdBytes.length ) + attrIdBytes.length;
232
233 org.apache.directory.api.ldap.model.entry.Value<?> assertionValue = getAssertionValue();
234
235 if ( assertionValue instanceof BinaryValue )
236 {
237 attrValBytes = getAssertionValue().getBytes();
238 avaLength += 1 + TLV.getNbBytes( attrValBytes.length ) + attrValBytes.length;
239 }
240 else
241 {
242 attrValBytes = Strings.getBytesUtf8( getAssertionValue().getString() );
243 avaLength += 1 + TLV.getNbBytes( attrValBytes.length ) + attrValBytes.length;
244 }
245
246 compareRequestLength += 1 + TLV.getNbBytes( avaLength ) + avaLength;
247
248 return 1 + TLV.getNbBytes( compareRequestLength ) + compareRequestLength;
249 }
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 @Override
266 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
267 {
268 try
269 {
270
271 buffer.put( LdapCodecConstants.COMPARE_REQUEST_TAG );
272 buffer.put( TLV.getBytes( compareRequestLength ) );
273
274
275 BerValue.encode( buffer, Dn.getBytes( getName() ) );
276
277
278 buffer.put( UniversalTag.SEQUENCE.getValue() );
279 buffer.put( TLV.getBytes( avaLength ) );
280 }
281 catch ( BufferOverflowException boe )
282 {
283 throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
284 }
285
286
287 BerValue.encode( buffer, attrIdBytes );
288
289
290 BerValue.encode( buffer, attrValBytes );
291
292 return buffer;
293 }
294 }