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.core.api.event;
21
22
23 import java.util.regex.Pattern;
24 import java.util.regex.PatternSyntaxException;
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.exception.LdapInvalidSearchFilterException;
31 import org.apache.directory.api.ldap.model.filter.ExprNode;
32 import org.apache.directory.api.ldap.model.filter.SubstringNode;
33 import org.apache.directory.api.ldap.model.name.Dn;
34 import org.apache.directory.api.ldap.model.schema.AttributeType;
35 import org.apache.directory.api.ldap.model.schema.MatchingRule;
36 import org.apache.directory.api.ldap.model.schema.Normalizer;
37 import org.apache.directory.server.i18n.I18n;
38
39
40
41
42
43
44
45 public class SubstringEvaluator implements Evaluator
46 {
47
48
49
50 public SubstringEvaluator()
51 {
52 }
53
54
55
56
57
58 public boolean evaluate( ExprNode node, Dn dn, Entry entry ) throws LdapException
59 {
60 Pattern regex = null;
61 SubstringNode snode = ( SubstringNode ) node;
62 AttributeType attributeType = snode.getAttributeType();
63 MatchingRule matchingRule = attributeType.getSubstring();
64
65 if ( matchingRule == null )
66 {
67 matchingRule = attributeType.getEquality();
68 }
69
70 Normalizer normalizer = matchingRule.getNormalizer();
71
72
73 Attribute attr = entry.get( snode.getAttribute() );
74
75
76 if ( null == attr )
77 {
78 return false;
79 }
80
81
82 try
83 {
84 regex = snode.getRegex( normalizer );
85 }
86 catch ( PatternSyntaxException pse )
87 {
88 LdapInvalidSearchFilterException ne = new LdapInvalidSearchFilterException( I18n.err( I18n.ERR_248, node ) );
89 ne.initCause( pse );
90 throw ne;
91 }
92
93
94
95
96
97
98
99
100 for ( Value value : attr )
101 {
102 String normValue = normalizer.normalize( value.getString() );
103
104
105
106 if ( regex.matcher( normValue ).matches() )
107 {
108 return true;
109 }
110 }
111
112
113 return false;
114 }
115 }