View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.event;
18  
19  
20  import javax.naming.NamingEnumeration;
21  import javax.naming.NamingException;
22  import javax.naming.directory.Attribute;
23  import javax.naming.directory.Attributes;
24  
25  import org.apache.ldap.common.filter.ExprNode;
26  import org.apache.ldap.common.filter.SubstringNode;
27  import org.apache.ldap.common.schema.AttributeType;
28  import org.apache.ldap.common.schema.Normalizer;
29  import org.apache.ldap.server.schema.AttributeTypeRegistry;
30  import org.apache.ldap.server.schema.OidRegistry;
31  import org.apache.regexp.RE;
32  import org.apache.regexp.RESyntaxException;
33  
34  
35  /***
36   * Evaluates substring filter assertions on an entry.
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   * @version $Rev$
40   */
41  public class SubstringEvaluator implements Evaluator
42  {
43      /*** Oid Registry used to translate attributeIds to OIDs */
44      private OidRegistry oidRegistry;
45      /*** AttributeType registry needed for normalizing and comparing values */
46      private AttributeTypeRegistry attributeTypeRegistry;
47  
48  
49      /***
50       * Creates a new SubstringEvaluator for substring expressions.
51       *
52       * @param oidRegistry the OID registry for name to OID mapping
53       * @param attributeTypeRegistry the attributeType registry
54       */
55      public SubstringEvaluator( OidRegistry oidRegistry,
56                                 AttributeTypeRegistry attributeTypeRegistry )
57      {
58          this.oidRegistry = oidRegistry;
59          this.attributeTypeRegistry = attributeTypeRegistry;
60      }
61  
62  
63      /***
64       * @see Evaluator#evaluate(ExprNode, String, Attributes)
65       */
66      public boolean evaluate( ExprNode node, String dn, Attributes entry )
67          throws NamingException
68      {
69          RE regex = null; 
70          SubstringNode snode = ( SubstringNode ) node;
71          String oid = oidRegistry.getOid( snode.getAttribute() );
72          AttributeType type = attributeTypeRegistry.lookup( oid );
73          Normalizer normalizer = type.getSubstr().getNormalizer();
74  
75          // get the attribute
76          Attribute attr = entry.get( snode.getAttribute() );
77          
78          // if the attribute does not exist just return false
79          if ( null == attr )
80          {
81              return false;
82          }
83  
84          // compile the regular expression to search for a matching attribute
85          try 
86          {
87              regex = snode.getRegex( normalizer );
88          } 
89          catch ( RESyntaxException e ) 
90          {
91              NamingException ne = new NamingException( "SubstringNode '" 
92                  + node + "' had " + "incorrect syntax" );
93              ne.setRootCause( e );
94              throw ne;
95          }
96          
97          /*
98           * Cycle through the attribute values testing normalized version 
99           * obtained from using the substring matching rule's normalizer.
100          * The test uses the comparator obtained from the appropriate 
101          * substring matching rule.
102          */ 
103         NamingEnumeration list = attr.getAll(); 
104         while ( list.hasMore() ) 
105         {
106             String value = ( String ) 
107                 normalizer.normalize( list.next() );
108             
109             // Once match is found cleanup and return true
110             if ( regex.match( value ) ) 
111             {
112                 list.close();
113                 return true;
114             }
115         }
116 
117         // we fell through so a match was not found - assertion was false.
118         return false;
119     }
120 }