directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r429102 - in /directory/trunks/apacheds: core/src/main/java/org/apache/directory/server/core/schema/ server-unit/src/test/java/org/apache/directory/server/
Date Sun, 06 Aug 2006 03:58:41 GMT
Author: akarasulu
Date: Sat Aug  5 20:58:40 2006
New Revision: 429102

URL: http://svn.apache.org/viewvc?rev=429102&view=rev
Log:
fix for DIRSERVER-637: added schema checking code for required attribute presence based on
objectClasses

Added:
    directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/AddITest.java
      - copied unchanged from r429101, directory/branches/apacheds/1.0/server-unit/src/test/java/org/apache/directory/server/AddITest.java
Removed:
    directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/AddObjectClassesToEntryTest.java
Modified:
    directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java
    directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/ReferralTest.java

Modified: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java?rev=429102&r1=429101&r2=429102&view=diff
==============================================================================
--- directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java
(original)
+++ directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java
Sat Aug  5 20:58:40 2006
@@ -18,6 +18,7 @@
 
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -51,7 +52,6 @@
 import org.apache.directory.shared.ldap.message.LockableAttributesImpl;
 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
 import org.apache.directory.shared.ldap.name.LdapDN;
-import org.apache.directory.shared.ldap.name.Rdn;
 import org.apache.directory.shared.ldap.schema.AttributeType;
 import org.apache.directory.shared.ldap.schema.DITContentRule;
 import org.apache.directory.shared.ldap.schema.DITStructureRule;
@@ -1063,13 +1063,18 @@
     /**
      * Check that all the attributes exist in the schema for this entry.
      */
-    public void add(NextInterceptor next, LdapDN normName, Attributes attrs) throws NamingException
+    public void add( NextInterceptor next, LdapDN normName, Attributes attrs ) throws NamingException
     {
         AttributeTypeRegistry atRegistry = this.globalRegistries.getAttributeTypeRegistry();
         NamingEnumeration attrEnum = attrs.getIDs();
         while ( attrEnum.hasMoreElements() )
         {
             String name = ( String ) attrEnum.nextElement();
+            
+            // ---------------------------------------------------------------
+            // make sure all attributes are valid schema defined attributes
+            // ---------------------------------------------------------------
+
             if ( !atRegistry.hasAttributeType( name ) )
             {
                 throw new LdapInvalidAttributeIdentifierException( name + " not found in
attribute registry!" );
@@ -1077,6 +1082,102 @@
         }
 
         alterObjectClasses( attrs.get( "objectClass" ), this.globalRegistries.getObjectClassRegistry()
);
+        assertRequiredAttributesPresent( attrs );
         next.add(normName, attrs );
+    }
+    
+    
+    /**
+     * Checks to see the presence of all required attributes within an entry.
+     */
+    private void assertRequiredAttributesPresent( Attributes entry ) 
+        throws NamingException
+    {
+        AttributeType[] required = getRequiredAttributes( entry.get( "objectClass" ), 
+            this.globalRegistries.getObjectClassRegistry() );
+        for ( int ii = 0; ii < required.length; ii++ )
+        {
+            boolean aliasFound = false;
+            String[] aliases = required[ii].getNames();
+            for ( int jj = 0; jj < aliases.length; jj++ )
+            {
+                if ( entry.get( aliases[jj] ) != null )
+                {
+                    aliasFound = true;
+                    break;
+                }
+            }
+            
+            if ( ! aliasFound )
+            {
+                throw new LdapSchemaViolationException( "Required attribute " + 
+                    required[ii].getName() + " not found within entry.", 
+                    ResultCodeEnum.OBJECTCLASSVIOLATION );
+            }
+        }
+    }
+
+
+    private static final AttributeType[] EMPTY_ATTRIBUTE_TYPE_ARRAY = new AttributeType[0];
+    
+    /**
+     * Uses the objectClass registry to ascend super classes and collect 
+     * all attributeTypes within must lists until top is reached on each
+     * parent.
+     */
+    private static final AttributeType[] getRequiredAttributes( Attribute objectClass, 
+        ObjectClassRegistry registry ) throws NamingException
+    {
+        AttributeType[] attributeTypes;
+        Set set = new HashSet();
+        
+        for ( int ii = 0; ii < objectClass.size(); ii++ )
+        {
+            String ocString = ( String ) objectClass.get( ii );
+            ObjectClass oc = registry.lookup( ocString );
+            infuseMustList( set, oc );
+        }
+        
+        attributeTypes = ( AttributeType[] ) set.toArray( EMPTY_ATTRIBUTE_TYPE_ARRAY );
+        return attributeTypes;
+    }
+
+    
+    /**
+     * Recursive method that finds all the required attributes for an 
+     * objectClass and infuses them into the provided non-null set.
+     * 
+     * @param set set to infuse attributeTypes into
+     * @param oc the objectClass to ascent the polymorphic inheritance tree of 
+     */
+    private static final void infuseMustList( Set set, ObjectClass oc ) throws NamingException
+    {
+        // ignore top
+        if ( oc.getName().equalsIgnoreCase( "top" ) )
+        {
+            return;
+        }
+        
+        // add all the required attributes for this objectClass 
+        Collections.addAll( set, oc.getMustList() );
+        
+        // don't bother ascending if no parents exist
+        ObjectClass[] parents = oc.getSuperClasses();
+        if ( parents == null || parents.length == 0 )
+        {
+            return;
+        }
+        
+        // save on a for loop
+        if ( parents.length == 1 ) 
+        {
+            infuseMustList( set, parents[0] );
+            return;
+        }
+        
+        for ( int ii = 0; ii < parents.length; ii++ )
+        {
+            infuseMustList( set, parents[ii] );
+        }
     }
 }

Modified: directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/ReferralTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/ReferralTest.java?rev=429102&r1=429101&r2=429102&view=diff
==============================================================================
--- directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/ReferralTest.java
(original)
+++ directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/ReferralTest.java
Sat Aug  5 20:58:40 2006
@@ -775,7 +775,10 @@
         }
         try
         {
-            td.rootCtx.createSubcontext( "ou=deep" );
+            Attributes ouAttrs = new BasicAttributes( "objectClass", "top", true );
+            ouAttrs.get( "objectClass" ).add( "organizationalUnit" );
+            ouAttrs.put( "ou", "deep" );
+            td.rootCtx.createSubcontext( "ou=deep", ouAttrs );
             userCtx = ( LdapContext ) td.rootCtx.createSubcontext( "cn=akarasulu,ou=deep",
referral );
         }
         catch ( NameAlreadyBoundException e )



Mime
View raw message