Merged changes done in trunk into the branch git-svn-id: https://svn.apache.org/repos/asf/directory/shared/branches/shared-subtree@966528 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ldap/src/main/java/org/apache/directory/shared/ldap/name/DN.java b/ldap/src/main/java/org/apache/directory/shared/ldap/name/DN.java index c440598..5c754ca 100644 --- a/ldap/src/main/java/org/apache/directory/shared/ldap/name/DN.java +++ b/ldap/src/main/java/org/apache/directory/shared/ldap/name/DN.java
@@ -41,6 +41,7 @@ import org.apache.directory.shared.ldap.exception.LdapException; import org.apache.directory.shared.ldap.exception.LdapInvalidDnException; import org.apache.directory.shared.ldap.message.ResultCodeEnum; +import org.apache.directory.shared.ldap.schema.SchemaManager; import org.apache.directory.shared.ldap.schema.normalizers.OidNormalizer; import org.apache.directory.shared.ldap.util.StringTools; import org.apache.directory.shared.ldap.util.UTFUtils; @@ -110,6 +111,8 @@ /** A null DN */ public static final DN EMPTY_DN = new DN(); + /** the schema manager */ + private transient SchemaManager schemaManager; // ~ Methods // ------------------------------------------------------------------------------------ @@ -119,20 +122,42 @@ */ public DN() { + this( ( SchemaManager ) null ); + } + + + /** + * Construct an empty DN object + */ + public DN( SchemaManager schemaManger ) + { + this.schemaManager = schemaManger; upName = ""; normName = null; normalized = true; } + + /** + * @see #DN(DN, SchemaManager) + */ + public DN( DN dn ) throws LdapInvalidDnException + { + this( dn, null); + } + /** * Copies a DN to an DN. * * @param dn composed of String name components. + * @param schemaManager the schema manager * @throws LdapInvalidDnException If the Name is invalid. */ - public DN( DN dn ) throws LdapInvalidDnException + public DN( DN dn, SchemaManager schemaManager ) throws LdapInvalidDnException { + this.schemaManager = schemaManager; + if ( ( dn != null ) && ( dn.size() != 0 ) ) { for ( int ii = 0; ii < dn.size(); ii++ ) @@ -142,10 +167,17 @@ } } - normalized = false; + if( schemaManager != null ) + { + normalized = true; + } + else + { + normalized = false; + } } - + /** * Parse a String and checks that it is a valid DN <br> * <p> @@ -160,20 +192,45 @@ */ public DN( String upName ) throws LdapInvalidDnException { + this( upName, null ); + } + + + + public DN( String upName, SchemaManager schemaManager ) throws LdapInvalidDnException + { if ( upName != null ) { DnParser.parseInternal( upName, rdns ); } + + if( schemaManager != null ) + { + this.schemaManager = schemaManager; + normalize( schemaManager.getNormalizerMapping() ); + normalized = true; + } + else + { + normalized = false; - // Stores the representations of a DN : internal (as a string and as a - // byte[]) and external. - normalizeInternal(); - normalized = false; + // Stores the representations of a DN : internal (as a string and as a + // byte[]) and external. + normalizeInternal(); + } this.upName = upName; } + /** + * @see #DN(SchemaManager, String...) + */ + public DN( String... upRdns ) throws LdapInvalidDnException + { + this( null, upRdns ); + } + /** * Creates a new instance of DN, using varargs to declare the RDNs. Each * String is either a full RDN, or a couple of AttributeType DI and a value. @@ -191,10 +248,11 @@ * baseDn); * </pre> * + * @param schemaManager the schema manager * @param upRdns * @throws LdapInvalidDnException */ - public DN( String... upRdns ) throws LdapInvalidDnException + public DN( SchemaManager schemaManager, String... upRdns ) throws LdapInvalidDnException { StringBuilder sb = new StringBuilder(); boolean valueExpected = false; @@ -237,10 +295,21 @@ // byte[]) and external. upName = sb.toString(); DnParser.parseInternal( upName, rdns ); - normalizeInternal(); - normalized = false; + + if( schemaManager != null ) + { + this.schemaManager = schemaManager; + normalize( schemaManager.getNormalizerMapping() ); + normalized = true; + } + else + { + normalized = false; + normalizeInternal(); + } } + /** * Create a DN when deserializing it. * @@ -269,7 +338,7 @@ */ public static DN normalize( String name, Map<String, OidNormalizer> oidsMap ) throws LdapInvalidDnException { - if ( ( name == null ) || ( name.length() == 0 ) || ( oidsMap == null ) || ( oidsMap.size() == 0 ) ) + if ( ( name == null ) || ( name.length() == 0 ) || ( oidsMap == null ) || ( oidsMap.isEmpty() ) ) { return DN.EMPTY_DN; } @@ -1102,7 +1171,7 @@ try { // We have to parse the nameComponent which is given as an argument - RDN newRdn = new RDN( comp ); + RDN newRdn = new RDN( comp, schemaManager ); rdns.add( 0, newRdn ); } @@ -1526,7 +1595,8 @@ */ public DN normalize( Map<String, OidNormalizer> oidsMap ) throws LdapInvalidDnException { - if ( ( oidsMap == null ) || ( oidsMap.size() == 0 ) ) + + if ( ( oidsMap == null ) || ( oidsMap.isEmpty() ) ) { return this; }
diff --git a/ldap/src/main/java/org/apache/directory/shared/ldap/name/RDN.java b/ldap/src/main/java/org/apache/directory/shared/ldap/name/RDN.java index d10db8f..743a41a 100644 --- a/ldap/src/main/java/org/apache/directory/shared/ldap/name/RDN.java +++ b/ldap/src/main/java/org/apache/directory/shared/ldap/name/RDN.java
@@ -37,6 +37,8 @@ import org.apache.directory.shared.ldap.entry.Value; import org.apache.directory.shared.ldap.exception.LdapException; import org.apache.directory.shared.ldap.exception.LdapInvalidDnException; +import org.apache.directory.shared.ldap.schema.AttributeType; +import org.apache.directory.shared.ldap.schema.SchemaManager; import org.apache.directory.shared.ldap.schema.normalizers.OidNormalizer; import org.apache.directory.shared.ldap.util.StringTools; import org.apache.directory.shared.ldap.util.UTFUtils; @@ -187,15 +189,31 @@ /** A flag used to tell if the RDN has been normalized */ private boolean normalized; + /** the schema manager */ + private transient SchemaManager schemaManager; + /** * A empty constructor. */ public RDN() { + this( ( SchemaManager ) null ); + } + + + /** + * + * Creates a new instance of RDN. + * + * @param schemaManager the schema manager + */ + public RDN( SchemaManager schemaManager ) + { // Don't waste space... This is not so often we have multiple // name-components in a RDN... So we won't initialize the Map and the // treeSet. + this.schemaManager = schemaManager; upName = ""; normName = ""; normalized = false; @@ -206,9 +224,10 @@ * A constructor that parse a String representing a RDN. * * @param rdn The String containing the RDN to parse + * @param schemaManager the schema manager * @throws LdapInvalidDnException If the RDN is invalid */ - public RDN( String rdn ) throws LdapInvalidDnException + public RDN( String rdn, SchemaManager schemaManager ) throws LdapInvalidDnException { start = 0; @@ -219,7 +238,18 @@ // create the internal normalized form // and store the user provided form - normalize(); + if ( schemaManager != null ) + { + this.schemaManager = schemaManager; + normalize( schemaManager.getNormalizerMapping() ); + normalized = true; + } + else + { + normalize(); + normalized = false; + } + upName = rdn; length = rdn.length(); } @@ -228,9 +258,20 @@ upName = ""; normName = ""; length = 0; + normalized = false; } + } - normalized = false; + + /** + * A constructor that parse a String representing a RDN. + * + * @param rdn The String containing the RDN to parse + * @throws LdapInvalidDnException + */ + public RDN( String rdn ) throws LdapInvalidDnException + { + this( rdn, ( SchemaManager ) null ); } @@ -244,23 +285,43 @@ * @param upValue The user provided value of the RDN * @param normType The normalized provided type of the RDN * @param normValue The normalized provided value of the RDN + * @param schemaManager the schema manager * @throws LdapInvalidDnException If the RDN is invalid */ - public RDN( String upType, String normType, String upValue, String normValue ) throws LdapInvalidDnException + public RDN( String upType, String normType, String upValue, String normValue, SchemaManager schemaManager ) throws LdapInvalidDnException { + this.schemaManager = schemaManager; + addAttributeTypeAndValue( upType, normType, new StringValue( upValue ), new StringValue( normValue ) ); upName = upType + '=' + upValue; start = 0; length = upName.length(); + // create the internal normalized form normalize(); - - // As strange as it seems, the RDN is *not* normalized against the schema at this point - normalized = false; + + if( schemaManager != null ) + { + normalized = true; + } + else + { + // As strange as it seems, the RDN is *not* normalized against the schema at this point + normalized = false; + } } + + /** + * @see #RDN(String, String, String, String, SchemaManager) + */ + public RDN( String upType, String normType, String upValue, String normValue ) throws LdapInvalidDnException + { + this( upType, normType, upValue, normValue, null ); + } + /** * A constructor that constructs a RDN from a type and a value. Constructs * an Rdn from the given attribute type and value. The string attribute @@ -269,22 +330,42 @@ * * @param upType The user provided type of the RDN * @param upValue The user provided value of the RDN + * @param schemaManager the schema manager * @throws LdapInvalidDnException If the RDN is invalid */ - public RDN( String upType, String upValue ) throws LdapInvalidDnException + public RDN( String upType, String upValue, SchemaManager schemaManager ) throws LdapInvalidDnException { addAttributeTypeAndValue( upType, upType, new StringValue( upValue ), new StringValue( upValue ) ); upName = upType + '=' + upValue; start = 0; length = upName.length(); - // create the internal normalized form - normalize(); - // As strange as it seems, the RDN is *not* normalized against the schema at this point - normalized = false; + if( schemaManager != null ) + { + this.schemaManager = schemaManager; + normalize( schemaManager.getNormalizerMapping() ); + normalized = true; + } + else + { + // create the internal normalized form + normalize(); + + // As strange as it seems, the RDN is *not* normalized against the schema at this point + normalized = false; + } } + + /** + * @see #RDN(String, String, SchemaManager) + */ + public RDN( String upType, String upValue ) throws LdapInvalidDnException + { + this( upType, upValue, null ); + } + /** * A constructor that constructs a RDN from a type, a position and a length. @@ -300,7 +381,7 @@ this.length = length; this.upName = upName; this.normName = normName; - normalized = false; + normalized = true; } @@ -407,7 +488,7 @@ * Transform a RDN by changing the value to its OID counterpart and * normalizing the value accordingly to its type. * - * @param oidsMap The map of all existing oids and normalizer. + * @param oidsMap The mapping between names and oids. * @throws LdapException If the RDN is invalid. */ public RDN normalize( Map<String, OidNormalizer> oidsMap ) throws LdapInvalidDnException @@ -439,14 +520,28 @@ Value<?> value ) throws LdapInvalidDnException { // First, let's normalize the type - String normalizedType = StringTools.lowerCaseAscii( type ); Value<?> normalizedValue = value; + String normalizedType = StringTools.lowerCaseAscii( type ); + + if( schemaManager != null ) + { + OidNormalizer oidNormalizer = schemaManager.getNormalizerMapping().get( normalizedType ); + normalizedType = oidNormalizer.getAttributeTypeOid(); + try + { + normalizedValue = oidNormalizer.getNormalizer().normalize( value ); + } + catch( LdapException e ) + { + throw new LdapInvalidDnException( e.getMessage() ); + } + } switch ( nbAtavs ) { case 0: // This is the first AttributeTypeAndValue. Just stores it. - atav = new AVA( upType, type, upValue, normalizedValue ); + atav = new AVA( upType, normalizedType, upValue, normalizedValue ); nbAtavs = 1; atavType = normalizedType; return; @@ -469,7 +564,7 @@ default: // add a new AttributeTypeAndValue - AVA newAtav = new AVA( upType, type, upValue, normalizedValue ); + AVA newAtav = new AVA( upType, normalizedType, upValue, normalizedValue ); atavs.add( newAtav ); atavTypes.put( normalizedType, newAtav ); @@ -1310,8 +1405,8 @@ return escapeValue( value ); } - - + + /** * Tells if the RDN has already been normalized or not *
diff --git a/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/DnComparator.java b/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/DnComparator.java index 36567ab..64e383d 100644 --- a/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/DnComparator.java +++ b/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/DnComparator.java
@@ -80,8 +80,7 @@ } else if ( obj instanceof String ) { - dn = new DN( ( String ) obj ); - dn.normalize( schemaManager.getNormalizerMapping() ); + dn = new DN( ( String ) obj, schemaManager ); } else {
diff --git a/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/UniqueMemberComparator.java b/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/UniqueMemberComparator.java index b7e5d28..5ed0f25 100644 --- a/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/UniqueMemberComparator.java +++ b/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/UniqueMemberComparator.java
@@ -169,8 +169,7 @@ } else if ( obj instanceof String ) { - dn = new DN( ( String ) obj ); - dn.normalize( schemaManager.getNormalizerMapping() ); + dn = new DN( ( String ) obj, schemaManager ); } else {
diff --git a/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/DnNormalizer.java b/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/DnNormalizer.java index 6e067ac..f02ad94 100644 --- a/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/DnNormalizer.java +++ b/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/DnNormalizer.java
@@ -60,9 +60,8 @@ String dnStr = value.getString(); - dn = new DN( dnStr ); + dn = new DN( dnStr, schemaManager ); - dn.normalize( schemaManager.getNormalizerMapping() ); return new StringValue( dn.getNormName() ); } @@ -74,9 +73,8 @@ { DN dn = null; - dn = new DN( value ); + dn = new DN( value, schemaManager ); - dn.normalize( schemaManager.getNormalizerMapping() ); return dn.getNormName(); } @@ -91,9 +89,8 @@ { DN dn = null; - dn = new DN( value ); + dn = new DN( value, schemaManager ); - dn.normalize( schemaManager.getNormalizerMapping() ); return dn.getNormName(); }
diff --git a/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/UniqueMemberNormalizer.java b/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/UniqueMemberNormalizer.java index 2f706c1..86f356c 100644 --- a/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/UniqueMemberNormalizer.java +++ b/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/UniqueMemberNormalizer.java
@@ -80,9 +80,7 @@ if ( sharpPos > 0 ) { - DN dn = new DN( nameAndUid.substring( 0, sharpPos ) ); - - dn.normalize( schemaManager.getNormalizerMapping() ); + DN dn = new DN( nameAndUid.substring( 0, sharpPos ), schemaManager ); return new StringValue( dn.getNormName() + '#' + uid ); } @@ -127,9 +125,7 @@ if ( sharpPos > 0 ) { - DN dn = new DN( value.substring( 0, sharpPos ) ); - - dn.normalize( schemaManager.getNormalizerMapping() ); + DN dn = new DN( value.substring( 0, sharpPos ), schemaManager ); return dn.getNormName() + '#' + uid; } @@ -142,7 +138,7 @@ { // No UID, the strValue is a DN // Return the normalized DN - return new DN( value ).normalize( schemaManager.getNormalizerMapping() ).getNormName(); + return new DN( value, schemaManager ).getNormName(); } }