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.subtree;
21  
22  
23  import java.util.Iterator;
24  
25  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
26  import org.apache.directory.api.ldap.model.entry.Attribute;
27  import org.apache.directory.api.ldap.model.exception.LdapException;
28  import org.apache.directory.api.ldap.model.filter.EqualityNode;
29  import org.apache.directory.api.ldap.model.filter.SimpleNode;
30  import org.apache.directory.api.ldap.model.schema.AttributeType;
31  import org.apache.directory.api.ldap.model.schema.SchemaManager;
32  import org.apache.directory.server.i18n.I18n;
33  
34  
35  /**
36   * A refinement leaf node evaluator.  This evaluator checks to see if the
37   * objectClass attribute of a candidate entry is matched by a leaf node in
38   * a refinement filter expression tree.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class RefinementLeafEvaluator
43  {
44      /** A SchemaManager instance */
45      private final SchemaManager schemaManager;
46  
47      /** A storage for the ObjectClass attributeType */
48      private AttributeType OBJECT_CLASS_AT;
49  
50  
51      /**
52       * Creates a refinement filter's leaf node evaluator.
53       *
54       * @param schemaManager The server schemaManager
55       */
56      public RefinementLeafEvaluator( SchemaManager schemaManager )
57      {
58          this.schemaManager = schemaManager;
59          OBJECT_CLASS_AT = schemaManager.getAttributeType( SchemaConstants.OBJECT_CLASS_AT );
60      }
61  
62  
63      /**
64       * Evaluates whether or not a simple leaf node of a refinement filter selects an
65       * entry based on the entry's objectClass attribute values.
66       *
67       * @param node the leaf node of the refinement filter
68       * @param objectClasses the objectClass attribute's values
69       * @return true if the leaf node selects the entry based on objectClass values, false
70       * if it rejects the entry
71       * @throws LdapException
72       */
73      public boolean evaluate( SimpleNode node, Attribute objectClasses ) throws LdapException
74      {
75          if ( node == null )
76          {
77              throw new IllegalArgumentException( I18n.err( I18n.ERR_295 ) );
78          }
79  
80          if ( !( node instanceof EqualityNode ) )
81          {
82              throw new LdapException( I18n.err( I18n.ERR_301, node ) );
83          }
84  
85          if ( node.isSchemaAware() )
86          {
87              if ( !node.getAttributeType().equals( OBJECT_CLASS_AT ) )
88              {
89                  throw new IllegalArgumentException( I18n.err( I18n.ERR_302, node.getAttribute() ) );
90              }
91          }
92          else if ( !node.getAttribute().equalsIgnoreCase( SchemaConstants.OBJECT_CLASS_AT ) &&
93              !node.getAttribute().equalsIgnoreCase( SchemaConstants.OBJECT_CLASS_AT_OID ) )
94          {
95              throw new IllegalArgumentException( I18n.err( I18n.ERR_302, node.getAttribute() ) );
96          }
97  
98          if ( null == objectClasses )
99          {
100             throw new IllegalArgumentException( I18n.err( I18n.ERR_303 ) );
101         }
102 
103         if ( !( objectClasses.isInstanceOf( OBJECT_CLASS_AT ) ) )
104         {
105             throw new IllegalArgumentException( I18n.err( I18n.ERR_304 ) );
106         }
107 
108         // check if Ava value exists in attribute
109         // If the filter value for the objectClass is an OID we need to resolve a name
110         String value = node.getValue().getString();
111 
112         if ( objectClasses.contains( value ) )
113         {
114             return true;
115         }
116 
117         if ( Character.isDigit( value.charAt( 0 ) ) )
118         {
119             Iterator<String> list = schemaManager.getGlobalOidRegistry().getNameSet( value ).iterator();
120 
121             while ( list.hasNext() )
122             {
123                 String objectClass = list.next();
124 
125                 if ( objectClasses.contains( objectClass ) )
126                 {
127                     return true;
128                 }
129             }
130         }
131 
132         // no match so return false
133         return false;
134     }
135 }