You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/10/29 01:44:45 UTC

svn commit: rev 55911 - in incubator/directory/eve/trunk/backend/core/src: java/org/apache/eve/jndi/ibs test/org/apache/eve/jndi/ibs

Author: akarasulu
Date: Thu Oct 28 16:44:44 2004
New Revision: 55911

Added:
   incubator/directory/eve/trunk/backend/core/src/test/org/apache/eve/jndi/ibs/BinaryAttributeFilterTest.java
Modified:
   incubator/directory/eve/trunk/backend/core/src/java/org/apache/eve/jndi/ibs/SchemaService.java
Log:
finished, tested and debugged binary attribute handling

Modified: incubator/directory/eve/trunk/backend/core/src/java/org/apache/eve/jndi/ibs/SchemaService.java
==============================================================================
--- incubator/directory/eve/trunk/backend/core/src/java/org/apache/eve/jndi/ibs/SchemaService.java	(original)
+++ incubator/directory/eve/trunk/backend/core/src/java/org/apache/eve/jndi/ibs/SchemaService.java	Thu Oct 28 16:44:44 2004
@@ -17,18 +17,26 @@
 package org.apache.eve.jndi.ibs;
 
 
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collections;
 import javax.naming.Name;
 import javax.naming.NamingException;
+import javax.naming.NamingEnumeration;
 import javax.naming.ldap.LdapContext;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.SearchControls;
+import javax.naming.directory.Attribute;
 
 import org.apache.eve.jndi.BaseInterceptor;
 import org.apache.eve.RootNexus;
 import org.apache.eve.db.SearchResultFilter;
 import org.apache.eve.db.DbSearchResult;
 import org.apache.eve.schema.GlobalRegistries;
-import org.apache.ldap.common.NotImplementedException;
+import org.apache.eve.schema.AttributeTypeRegistry;
+
+import org.apache.ldap.common.schema.AttributeType;
+import org.apache.ldap.common.message.LockableAttributeImpl;
 
 
 /**
@@ -41,7 +49,10 @@
 {
     /** the root nexus to all database partitions */
     private final RootNexus nexus;
-    private FilterService filterService;
+    /** a binary attribute tranforming filter: String -> byte[] */
+    private final BinaryAttributeFilter binaryAttributeFilter;
+    /** the filter service used by the schema service */
+    private final FilterService filterService;
     /** the global schema object registries */
     private final GlobalRegistries globalRegistries;
 
@@ -73,27 +84,113 @@
         {
             throw new NullPointerException( "the filter service cannot be null" );
         }
+
+        binaryAttributeFilter = new BinaryAttributeFilter(
+                globalRegistries.getAttributeTypeRegistry() );
+        filterService.addLookupFilter( binaryAttributeFilter );
+        filterService.addSearchResultFilter( binaryAttributeFilter );
     }
 
 
+    /**
+     * A special filter over entry attributes which replaces Attribute String
+     * values with their respective byte[] representations using schema
+     * information and the value held in the JNDI environment property:
+     * <code>java.naming.ldap.attributes.binary</code>.
+     *
+     * @see <a href=
+     * "http://java.sun.com/j2se/1.4.2/docs/guide/jndi/jndi-ldap-gl.html#binary">
+     * java.naming.ldap.attributes.binary</a>
+     */
     private class BinaryAttributeFilter implements LookupFilter, SearchResultFilter
     {
-        public void filter( LdapContext ctx, Name dn, Attributes entry )
+        private final static String BINARY_KEY =
+                "java.naming.ldap.attributes.binary";
+        private final AttributeTypeRegistry registry;
+
+
+        public BinaryAttributeFilter( AttributeTypeRegistry registry )
+        {
+            this.registry = registry;
+        }
+
+
+        private void doFilter( LdapContext ctx, Attributes entry )
                 throws NamingException
         {
-            throw new NotImplementedException( "filter in org.apache.eve.jndi.ibs.SchemaService.BinaryAttributeFilter not implemented!" );
+            // set of AttributeType objects that are to behave as binaries
+            Set binaries;
+
+            // construct the set for fast lookups while filtering
+            String binaryIds = ( String ) ctx.getEnvironment().get( BINARY_KEY );
+            if ( binaryIds == null )
+            {
+                binaries = Collections.EMPTY_SET;
+            }
+            else
+            {
+                String[] binaryArray = binaryIds.split( " " );
+                binaries = new HashSet( binaryArray.length );
+                for ( int ii = 0; ii < binaryArray.length; ii++ )
+                {
+                    AttributeType type = registry.lookup( binaryArray[ii] );
+                    binaries.add( type );
+                }
+            }
+
+            /*
+             * start converting values of attributes to byte[]s which are not
+             * human readable and those that are in the binaries set
+             */
+            NamingEnumeration list = entry.getIDs();
+            while ( list.hasMore() )
+            {
+                String id = ( String ) list.next();
+                AttributeType type = registry.lookup( id );
+                boolean isBinary = ! type.getSyntax().isHumanReadable();
+
+                if ( isBinary || binaries.contains( type ) )
+                {
+                    Attribute attribute = entry.get( id );
+                    Attribute binary = new LockableAttributeImpl( id );
+
+                    for ( int ii = 0; ii < attribute.size(); ii++ )
+                    {
+                        Object value = attribute.get( ii );
+                        if ( value instanceof String )
+                        {
+                            binary.add( ii, ( ( String ) value ).getBytes() );
+                        }
+                        else
+                        {
+                            binary.add( ii, value );
+                        }
+                    }
+
+                    entry.remove( id );
+                    entry.put( binary );
+                }
+            }
+        }
+
+
+        public void filter( LdapContext ctx, Name dn, Attributes entry ) throws NamingException
+        {
+            doFilter( ctx, entry );
         }
 
 
-        public void filter( LdapContext ctx, Name dn, Attributes entry, String[] ids ) throws NamingException
+        public void filter( LdapContext ctx, Name dn, Attributes entry, String[] ids )
+                throws NamingException
         {
-            throw new NotImplementedException( "filter in org.apache.eve.jndi.ibs.SchemaService.BinaryAttributeFilter not implemented!" );
+            doFilter( ctx, entry );
         }
 
 
         public boolean accept( LdapContext ctx, DbSearchResult result, SearchControls controls ) throws NamingException
         {
-            throw new NotImplementedException( "accept in org.apache.eve.jndi.ibs.SchemaService.BinaryAttributeFilter not implemented!" );
+            doFilter( ctx, result.getAttributes() );
+            return true;
         }
     }
 }

Added: incubator/directory/eve/trunk/backend/core/src/test/org/apache/eve/jndi/ibs/BinaryAttributeFilterTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/eve/trunk/backend/core/src/test/org/apache/eve/jndi/ibs/BinaryAttributeFilterTest.java	Thu Oct 28 16:44:44 2004
@@ -0,0 +1,89 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.eve.jndi.ibs;
+
+
+import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.Attribute;
+
+import org.apache.eve.jndi.AbstractJndiTest;
+
+
+/**
+ * Tests to see that the binary property filtering in the schema service's
+ * filter class {@link SchemaService.BinaryAttributeFilter} is working
+ * properly.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class BinaryAttributeFilterTest extends AbstractJndiTest
+{
+    private static final String BINARY_KEY = "java.naming.ldap.attributes.binary";
+
+
+    public void testBinaryExtension() throws NamingException
+    {
+        Attributes attributes = new BasicAttributes();
+        attributes.put( "objectClass", "top" );
+        attributes.put( "objectClass", "organizationalUnit" );
+        attributes.put( "objectClass", "extensibleObject" );
+        attributes.put( "ou", "testing" );
+        sysRoot.createSubcontext( "ou=test", attributes );
+
+        // test without turning on the property
+        DirContext ctx = ( DirContext ) sysRoot.lookup( "ou=test" ) ;
+        Attribute ou = ctx.getAttributes( "" ).get( "ou" );
+        Object value = ou.get();
+        assertTrue( value instanceof String );
+
+        // test with the property now making ou into a binary value
+        sysRoot.addToEnvironment( BINARY_KEY, "ou" );
+        ctx = ( DirContext ) sysRoot.lookup( "ou=test" ) ;
+        ou = ctx.getAttributes( "" ).get( "ou" );
+        value = ou.get();
+        assertTrue( value instanceof byte[] );
+
+        byte[] keyValue = new byte[] { 0x45, 0x23, 0x7d, 0x7f };
+        // try krb5key which should be binary automatically
+        attributes.put( "krb5Key", keyValue );
+        sysRoot.createSubcontext( "ou=anothertest", attributes );
+        ctx = ( DirContext ) sysRoot.lookup( "ou=anothertest" ) ;
+        ou = ctx.getAttributes( "" ).get( "ou" );
+        value = ou.get();
+        assertTrue( value instanceof byte[] );
+
+        Attribute krb5Key = ctx.getAttributes( "" ).get( "krb5Key" );
+        value = krb5Key.get();
+        assertTrue( value instanceof byte[] );
+
+        attributes.remove( "krb5Key" );
+        attributes.put( "krb5Key", "testing a string" );
+        sysRoot.createSubcontext( "ou=yetanothertest", attributes );
+        ctx = ( DirContext ) sysRoot.lookup( "ou=yetanothertest" ) ;
+        ou = ctx.getAttributes( "" ).get( "ou" );
+        value = ou.get();
+        assertTrue( value instanceof byte[] );
+
+        krb5Key = ctx.getAttributes( "" ).get( "krb5Key" );
+        value = krb5Key.get();
+        assertTrue( value instanceof byte[] );
+    }
+}