View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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   * Evaluates substring filter assertions on an entry.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class SubstringEvaluator implements Evaluator
46  {
47      /**
48       * Creates a new SubstringEvaluator for substring expressions.
49       */
50      public SubstringEvaluator()
51      {
52      }
53  
54  
55      /**
56       * {@inheritDoc}
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          // get the attribute
73          Attribute attr = entry.get( snode.getAttribute() );
74  
75          // if the attribute does not exist just return false
76          if ( null == attr )
77          {
78              return false;
79          }
80  
81          // compile the regular expression to search for a matching attribute
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           * Cycle through the attribute values testing normalized version 
95           * obtained from using the substring matching rule's normalizer.
96           * The test uses the comparator obtained from the appropriate 
97           * substring matching rule.
98           */
99  
100         for ( Value value : attr )
101         {
102             String normValue = normalizer.normalize( value.getString() );
103 
104             // Once match is found cleanup and return true
105 
106             if ( regex.matcher( normValue ).matches() )
107             {
108                 return true;
109             }
110         }
111 
112         // we fell through so a match was not found - assertion was false.
113         return false;
114     }
115 }