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