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.search;
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.i18n.I18n;
30 import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
31 import org.apache.directory.api.util.Strings;
32
33
34
35
36
37
38
39 public class ExtensibleMatchFilter extends Filter
40 {
41
42 private int expectedMatchingRuleLength;
43
44
45 private String matchingRule;
46
47
48 private byte[] matchingRuleBytes;
49
50
51 private String type;
52
53 private byte[] typeBytes;
54
55
56 private org.apache.directory.api.ldap.model.entry.Value<?> matchValue;
57
58
59 private boolean dnAttributes = false;
60
61
62 private int extensibleMatchLength;
63
64
65
66
67
68
69
70
71 public ExtensibleMatchFilter( int tlvId )
72 {
73 super( tlvId );
74 }
75
76
77
78
79
80
81 public ExtensibleMatchFilter()
82 {
83 super();
84 }
85
86
87
88
89
90
91
92 public boolean isDnAttributes()
93 {
94 return dnAttributes;
95 }
96
97
98
99
100
101
102
103 public void setDnAttributes( boolean dnAttributes )
104 {
105 this.dnAttributes = dnAttributes;
106 }
107
108
109
110
111
112
113
114 public String getMatchingRule()
115 {
116 return matchingRule;
117 }
118
119
120
121
122
123
124
125 public void setMatchingRule( String matchingRule )
126 {
127 this.matchingRule = matchingRule;
128 }
129
130
131
132
133
134
135
136 public org.apache.directory.api.ldap.model.entry.Value<?> getMatchValue()
137 {
138 return matchValue;
139 }
140
141
142
143
144
145
146
147 public void setMatchValue( org.apache.directory.api.ldap.model.entry.Value<?> matchValue )
148 {
149 this.matchValue = matchValue;
150 }
151
152
153
154
155
156
157
158 public String getType()
159 {
160 return type;
161 }
162
163
164
165
166
167
168
169 public void setType( String type )
170 {
171 this.type = type;
172 }
173
174
175
176
177
178
179
180 public int getExpectedMatchingRuleLength()
181 {
182 return expectedMatchingRuleLength;
183 }
184
185
186
187
188
189
190
191 public void setExpectedMatchingRuleLength( int expectedMatchingRuleLength )
192 {
193 this.expectedMatchingRuleLength = expectedMatchingRuleLength;
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 @Override
213 public int computeLength()
214 {
215 if ( matchingRule != null )
216 {
217 matchingRuleBytes = Strings.getBytesUtf8( matchingRule );
218 extensibleMatchLength = 1 + TLV.getNbBytes( matchingRuleBytes.length ) + matchingRuleBytes.length;
219 }
220
221 if ( type != null )
222 {
223 typeBytes = Strings.getBytesUtf8( type );
224 extensibleMatchLength += 1 + TLV.getNbBytes( typeBytes.length ) + typeBytes.length;
225 }
226
227 if ( matchValue != null )
228 {
229 int bytesLength = matchValue.getBytes().length;
230 extensibleMatchLength += 1 + TLV.getNbBytes( bytesLength ) + bytesLength;
231 }
232
233 if ( dnAttributes )
234 {
235 extensibleMatchLength += 1 + 1 + 1;
236 }
237
238 return 1 + TLV.getNbBytes( extensibleMatchLength ) + extensibleMatchLength;
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 @Override
263 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
264 {
265 if ( buffer == null )
266 {
267 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
268 }
269
270 try
271 {
272
273 buffer.put( ( byte ) LdapCodecConstants.EXTENSIBLE_MATCH_FILTER_TAG );
274 buffer.put( TLV.getBytes( extensibleMatchLength ) );
275
276 if ( ( matchingRule == null ) && ( type == null ) )
277 {
278 throw new EncoderException( I18n.err( I18n.ERR_04056 ) );
279 }
280
281
282 if ( matchingRule != null )
283 {
284 buffer.put( ( byte ) LdapCodecConstants.MATCHING_RULE_ID_TAG );
285 buffer.put( TLV.getBytes( matchingRuleBytes.length ) );
286 buffer.put( matchingRuleBytes );
287 }
288
289
290 if ( type != null )
291 {
292 buffer.put( ( byte ) LdapCodecConstants.MATCHING_RULE_TYPE_TAG );
293 buffer.put( TLV.getBytes( typeBytes.length ) );
294 buffer.put( typeBytes );
295 }
296
297
298 if ( matchValue != null )
299 {
300 buffer.put( ( byte ) LdapCodecConstants.MATCH_VALUE_TAG );
301
302 byte[] bytes = matchValue.getBytes();
303 int bytesLength = bytes.length;
304 buffer.put( TLV.getBytes( bytesLength ) );
305
306 if ( bytesLength != 0 )
307 {
308 buffer.put( bytes );
309 }
310
311 }
312
313
314 if ( dnAttributes )
315 {
316 buffer.put( ( byte ) LdapCodecConstants.DN_ATTRIBUTES_FILTER_TAG );
317 buffer.put( ( byte ) 1 );
318 buffer.put( BerValue.TRUE_VALUE );
319 }
320 }
321 catch ( BufferOverflowException boe )
322 {
323 throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
324 }
325
326 return buffer;
327 }
328
329
330
331
332
333
334
335 @Override
336 public String toString()
337 {
338
339 StringBuilder sb = new StringBuilder();
340
341 if ( type != null )
342 {
343 sb.append( type );
344 }
345
346 if ( dnAttributes )
347 {
348 sb.append( ":dn" );
349 }
350
351 if ( matchingRule == null )
352 {
353
354 if ( type == null )
355 {
356 return "Extended Filter wrong syntax";
357 }
358 }
359 else
360 {
361 sb.append( ':' ).append( matchingRule );
362 }
363
364 sb.append( ":=" ).append( matchValue );
365
366 return sb.toString();
367 }
368 }