1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.xdbm.search.evaluator;
21
22
23 import java.util.Iterator;
24 import java.util.regex.Pattern;
25
26 import org.apache.directory.api.ldap.model.entry.Attribute;
27 import org.apache.directory.api.ldap.model.entry.Entry;
28 import org.apache.directory.api.ldap.model.entry.Value;
29 import org.apache.directory.api.ldap.model.exception.LdapException;
30 import org.apache.directory.api.ldap.model.filter.SubstringNode;
31 import org.apache.directory.api.ldap.model.schema.AttributeType;
32 import org.apache.directory.api.ldap.model.schema.MatchingRule;
33 import org.apache.directory.api.ldap.model.schema.Normalizer;
34 import org.apache.directory.api.ldap.model.schema.SchemaManager;
35 import org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer;
36 import org.apache.directory.server.core.api.partition.PartitionTxn;
37 import org.apache.directory.server.xdbm.IndexEntry;
38 import org.apache.directory.server.xdbm.Store;
39 import org.apache.directory.server.xdbm.search.Evaluator;
40
41
42
43
44
45
46
47 public class SubstringEvaluator implements Evaluator<SubstringNode>
48 {
49
50 private final Store db;
51
52
53 private final SchemaManager schemaManager;
54
55
56 private final SubstringNode node;
57
58
59 private final Pattern regex;
60
61
62 private final AttributeType attributeType;
63
64
65 private final Normalizer normalizer;
66
67
68
69
70
71
72
73
74
75
76 public SubstringEvaluator( SubstringNode node, Store db, SchemaManager schemaManager ) throws LdapException
77 {
78 this.db = db;
79 this.node = node;
80 this.schemaManager = schemaManager;
81 this.attributeType = node.getAttributeType();
82
83 MatchingRule rule = attributeType.getSubstring();
84
85 if ( rule == null )
86 {
87 rule = attributeType.getEquality();
88 }
89
90 if ( rule != null )
91 {
92 normalizer = rule.getNormalizer();
93 }
94 else
95 {
96 normalizer = new NoOpNormalizer( attributeType.getSyntaxOid() );
97 }
98
99
100
101 if ( attributeType.getSyntax().isHumanReadable() )
102 {
103 regex = node.getRegex( normalizer );
104 }
105 else
106 {
107 regex = null;
108 }
109 }
110
111
112
113
114
115 @SuppressWarnings("unchecked")
116 @Override
117 public boolean evaluate( PartitionTxn partitionTxn, IndexEntry<?, String> indexEntryQM ) throws LdapException
118 {
119 IndexEntry<String, String> indexEntry = ( IndexEntry<String, String> ) indexEntryQM;
120
121 Entry entry = indexEntry.getEntry();
122
123
124 if ( null == entry )
125 {
126 entry = db.fetch( partitionTxn, indexEntry.getId() );
127
128 if ( null == entry )
129 {
130
131 return false;
132 }
133
134 indexEntry.setEntry( entry );
135 }
136
137
138
139
140
141
142
143
144 Attribute attr = entry.get( attributeType );
145
146
147 if ( attr != null )
148 {
149
150
151
152
153
154
155 if ( attr.isHumanReadable() )
156 {
157 for ( Value value : attr )
158 {
159 String strValue = value.getString();
160 String normalizedValue = attr.getAttributeType().getEquality().getNormalizer().normalize( strValue );
161
162
163 if ( regex.matcher( normalizedValue ).matches() )
164 {
165
166 indexEntry.setKey( strValue );
167 return true;
168 }
169 }
170 }
171 else
172 {
173
174
175 for ( Value value : attr )
176 {
177 byte[] byteValue = value.getBytes();
178
179
180
181
182
183
184
185
186
187
188
189 }
190 }
191
192
193
194 }
195
196
197
198 if ( schemaManager.getAttributeTypeRegistry().hasDescendants( attributeType ) )
199 {
200
201
202
203 Iterator<AttributeType> descendants = schemaManager.getAttributeTypeRegistry().descendants( attributeType );
204
205 while ( descendants.hasNext() )
206 {
207 AttributeType descendant = descendants.next();
208
209 attr = entry.get( descendant );
210
211 if ( null != attr )
212 {
213
214
215
216
217
218
219
220 for ( Value value : attr )
221 {
222 String strValue = value.getString();
223 String normalizedValue = attr.getAttributeType().getEquality().getNormalizer().normalize( strValue );
224
225
226 if ( regex.matcher( normalizedValue ).matches() )
227 {
228
229 indexEntry.setKey( strValue );
230 return true;
231 }
232 }
233 }
234 }
235 }
236
237
238 return false;
239 }
240
241
242
243
244
245 @Override
246 public boolean evaluate( Entry entry ) throws LdapException
247 {
248
249 Attribute attr = entry.get( attributeType );
250
251
252 if ( attr != null )
253 {
254
255
256
257
258
259
260 for ( Value value : attr )
261 {
262 String strValue = value.getString();
263
264
265 if ( regex.matcher( strValue ).matches() )
266 {
267 return true;
268 }
269 }
270
271
272
273 }
274
275
276
277 if ( schemaManager.getAttributeTypeRegistry().hasDescendants( attributeType ) )
278 {
279
280
281
282 Iterator<AttributeType> descendants = schemaManager.getAttributeTypeRegistry().descendants( attributeType );
283
284 while ( descendants.hasNext() )
285 {
286 AttributeType descendant = descendants.next();
287
288 attr = entry.get( descendant );
289
290 if ( null != attr )
291 {
292
293
294
295
296
297
298
299 for ( Value value : attr )
300 {
301 String strValue = value.getString();
302
303
304 if ( regex.matcher( strValue ).matches() )
305 {
306 return true;
307 }
308 }
309 }
310 }
311 }
312
313
314 return false;
315 }
316
317
318 public Pattern getPattern()
319 {
320 return regex;
321 }
322
323
324
325
326
327 @Override
328 public SubstringNode getExpression()
329 {
330 return node;
331 }
332
333
334
335
336
337 @Override
338 public String toString( String tabs )
339 {
340 StringBuilder sb = new StringBuilder();
341
342 sb.append( tabs ).append( "SubstringEvaluator : " ).append( node ).append( "\n" );
343
344 return sb.toString();
345 }
346
347
348
349
350
351 @Override
352 public String toString()
353 {
354 return toString( "" );
355 }
356 }