Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 77155 invoked from network); 7 Feb 2007 14:39:41 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 7 Feb 2007 14:39:41 -0000 Received: (qmail 85389 invoked by uid 500); 7 Feb 2007 14:39:48 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 85359 invoked by uid 500); 7 Feb 2007 14:39:48 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 85348 invoked by uid 99); 7 Feb 2007 14:39:48 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Feb 2007 06:39:48 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Feb 2007 06:39:40 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 4ED3B1A981A; Wed, 7 Feb 2007 06:39:20 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r504564 - /directory/apacheds/branches/1.0/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java Date: Wed, 07 Feb 2007 14:39:19 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070207143920.4ED3B1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: elecharny Date: Wed Feb 7 06:39:18 2007 New Revision: 504564 URL: http://svn.apache.org/viewvc?view=rev&rev=504564 Log: Added a helper method to deal with H-R attribute's values. They are now transformed to String if they are passed as byte[] (it can happen if the value is stored in a Ldif file as a base64 encoded value) Fix DIRSERVER-844 Modified: directory/apacheds/branches/1.0/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java Modified: directory/apacheds/branches/1.0/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java URL: http://svn.apache.org/viewvc/directory/apacheds/branches/1.0/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java?view=diff&rev=504564&r1=504563&r2=504564 ============================================================================== --- directory/apacheds/branches/1.0/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java (original) +++ directory/apacheds/branches/1.0/core/src/main/java/org/apache/directory/server/core/schema/SchemaService.java Wed Feb 7 06:39:18 2007 @@ -20,7 +20,9 @@ package org.apache.directory.server.core.schema; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; +import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -1597,6 +1599,7 @@ // 3) No attributes should be used if they are not part of MUST and MAY // 3-1) Except if the extensibleObject ObjectClass is used // 3-2) or if the AttributeType is COLLECTIVE + // 4) We also check that for H-R attributes, we have a valid String in the values Attribute objectClassAttr = entry.get( "objectClass" ); List ocs = new ArrayList(); @@ -1614,6 +1617,9 @@ { assertAllAttributesAllowed( dn, entry, allowed ); } + + // Check the attributes values and transform them to String if necessary + assertHumanReadible( entry ); } /** @@ -1676,7 +1682,105 @@ ResultCodeEnum.OBJECTCLASSVIOLATION ); } } - + + /** + * Check that all the attribute's values which are Human Readible can be transformed + * to valid String if they are stored as byte[]. + */ + private void assertHumanReadible( Attributes entry ) throws NamingException + { + NamingEnumeration attributes = entry.getAll(); + boolean isEntryModified = false; + Attributes cloneEntry = null; + + // First, loop on all attributes + while ( attributes.hasMoreElements() ) + { + Attribute attribute = ( Attribute ) attributes.nextElement(); + + AttributeType attributeType = globalRegistries.getAttributeTypeRegistry().lookup( attribute.getID() ); + + // If the attributeType is H-R, check alll of its values + if ( attributeType.getSyntax().isHumanReadible() ) + { + Enumeration values = attribute.getAll(); + Attribute clone = null; + boolean isModified = false; + + // Loop on each values + while ( values.hasMoreElements() ) + { + Object value = values.nextElement(); + + if ( value instanceof String ) + { + continue; + } + else if ( value instanceof byte[] ) + { + // Ve have a byte[] value. It should be a String UTF-8 encoded + // Let's transform it + try + { + String valStr = new String( (byte[])value, "UTF-8" ); + + if ( !isModified ) + { + // Don't create useless clones. We only clone + // if we have at least one value which is a byte[] + isModified = true; + clone = (Attribute)attribute.clone(); + } + + // Swap the value into the clone + clone.remove( value ); + clone.add( valStr ); + } + catch ( UnsupportedEncodingException uee ) + { + throw new NamingException( "The value is not a valid String" ); + } + } + else + { + throw new NamingException( "The value stored in an Human Readible attribute is not a String" ); + } + } + + // The attribute has been checked. If one of its value has been modified, + // we have to modify the cloned Attributes/ + if ( isModified ) + { + if ( !isEntryModified ) + { + // Again, let's avoid useless cloning. If no attribute is H-R + // or if no H-R value is stored as a byte[], we don't have to create a clone + // of the entry + cloneEntry = (Attributes)entry.clone(); + isEntryModified = true; + } + + // Swap the attribute into the cloned entry + cloneEntry.remove( attribute.getID() ); + cloneEntry.put( clone ); + } + } + } + + // At the end, we now have to switch the entries, if it has been modified + if ( isEntryModified ) + { + attributes = cloneEntry.getAll(); + + // We llop on all the attributes and modify them in the initial entry. + while ( attributes.hasMoreElements() ) + { + Attribute attribute = (Attribute)attributes.nextElement(); + entry.remove( attribute.getID() ); + entry.put( attribute ); + } + } + } /** * Checks to see if an attribute is required by as determined from an entry's