directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r358155 - /directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java
Date Tue, 20 Dec 2005 23:56:47 GMT
Author: elecharny
Date: Tue Dec 20 15:56:42 2005
New Revision: 358155

URL: http://svn.apache.org/viewcvs?rev=358155&view=rev
Log:
- Deleted some commented source
- Added a logger
- Fixed the clone() method ( it was not really a clone, 
the RDNs were still shared)
- Fixed the toOidName() method (multi ATAVs still to be fixed)

Modified:
    directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java

Modified: directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java?rev=358155&r1=358154&r2=358155&view=diff
==============================================================================
--- directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java
(original)
+++ directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java
Tue Dec 20 15:56:42 2005
@@ -14,119 +14,6 @@
  *   limitations under the License.
  *
  */
-/*package org.apache.asn1new.ldap.codec.primitives;
-
-import java.io.UnsupportedEncodingException;
-
-import org.apache.asn1.codec.DecoderException;
-import org.apache.asn1new.util.StringUtils;
-
-/**
- * This class parses a DN. 
- * 
- * The DN MUST respect this BNF grammar (as of RFC2253, par. 3, and RFC1779, fig. 1) <br>
- * 
- * <p>
- *-    &lt;distinguishedName&gt;      ::= &lt;name&gt; | e <br>
- *-    &lt;name&gt;                   ::= &lt;name-component&gt; &lt;name-components&gt;
<br>
- *-    &lt;name-components&gt;        ::= &lt;spaces&gt; &lt;separator&gt;
&lt;spaces&gt; &lt;name-component&gt; &lt;name-components&gt; | e
<br>
- *-    &lt;name-component&gt;         ::= &lt;attributeType&gt; &lt;spaces&gt;
'=' &lt;spaces&gt; &lt;attributeValue&gt; &lt;attributeTypeAndValues&gt;
<br>
- *-    &lt;attributeTypeAndValues&gt; ::= &lt;spaces&gt; '+' &lt;spaces&gt;
&lt;attributeType&gt; &lt;spaces&gt; '=' &lt;spaces&gt; &lt;attributeValue&gt;
&lt;attributeTypeAndValues&gt; | e <br>
- *-    &lt;attributeType&gt;          ::= [a-zA-Z] &lt;keychars&gt; | &lt;oidPrefix&gt;
[0-9] &lt;digits&gt; &lt;oids&gt; | [0-9] &lt;digits&gt; &lt;oids&gt;
<br>
- *-    &lt;keychars&gt;               ::= [a-zA-Z] &lt;keychars&gt; | [0-9]
&lt;keychars&gt; | '-' &lt;keychars&gt; | e <br>
- *-    &lt;oidPrefix&gt;              ::= 'OID.' | 'oid.' | e <br>
- *-    &lt;oids&gt;                   ::= '.' [0-9] &lt;digits&gt; &lt;oids&gt;
| e <br>
- *-    &lt;attributeValue&gt;         ::= &lt;pairs-or-strings&gt; | '#'
&lt;hexstring&gt; |'"' &lt;quotechar-or-pairs&gt; '"' <br>
- *-    &lt;pairs-or-strings&gt;       ::= '\' &lt;pairchar&gt; &lt;pairs-or-strings&gt;
| &lt;stringchar&gt; &lt;pairs-or-strings&gt; | e <br>
- *-    &lt;quotechar-or-pairs&gt;     ::= &lt;quotechar&gt; &lt;quotechar-or-pairs&gt;
| '\' &lt;pairchar&gt; &lt;quotechar-or-pairs&gt; | e <br>
- *-    &lt;pairchar&gt;               ::= ',' | '=' | '+' | '&lt;' | '&gt;'
| '#' | ';' | '\' | '"' | [0-9a-fA-F] [0-9a-fA-F]  <br>
- *-    &lt;hexstring&gt;              ::= [0-9a-fA-F] [0-9a-fA-F] &lt;hexpairs&gt;
<br>
- *-    &lt;hexpairs&gt;               ::= [0-9a-fA-F] [0-9a-fA-F] &lt;hexpairs&gt;
| e <br>
- *-    &lt;digits&gt;                 ::= [0-9] &lt;digits&gt; | e <br>
- *-    &lt;stringchar&gt;             ::= [0x00-0xFF] - [,=+&lt;&gt;#;\"\n\r]
<br>
- *-    &lt;quotechar&gt;              ::= [0x00-0xFF] - [\"] <br>
- *-    &lt;separator&gt;              ::= ',' | ';' <br>
- *-    &lt;spaces&gt;                 ::= ' ' &lt;spaces&gt; | e <br>
- * </p>
- *
- * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
- *
-public class LdapDN extends RelativeLdapDN
-{
-    //~ Static fields/initializers -----------------------------------------------------------------
-
-    /** A null LdapDN *
-    public transient static final LdapDN EMPTY_STRING = new LdapDN();
-
-    //~ Methods ------------------------------------------------------------------------------------
-
-    /**
-     * Construct an empty LdapDN object
-     *
-    public LdapDN()
-    {
-        super(0, false);
-    }
-    
-    /**
-     * Parse a buffer and checks that it is a valid DN <br>
-     * <p>
-     * &lt;distinguishedName&gt;     ::= &lt;name&gt; | e <br>
-     * &lt;name&gt;                ::= &lt;name-component&gt; &lt;name-components&gt;
<br>
-     * &lt;name-components&gt;    ::= &lt;spaces&gt; &lt;separator&gt;
&lt;spaces&gt; &lt;name-component&gt; &lt;name-components&gt; | e
<br>
-     * </p>
-     * 
-     * @param bytes The byte buffer that contains the DN
-     * @exception A DecoderException is thrown if the buffer does not contains a valid DN.
-     *
-    public LdapDN( byte[] bytes ) throws DecoderException
-    {
-        if ( bytes == null || bytes.length == 0)
-        {
-            return;
-        }
-        
-        int pos = 0;
-
-        // <name>             ::= <name-component> <name-components>
-        // <name-components> ::= <spaces> <separator> <spaces> <name-component>
<name-components> | e
-        if ( ( pos = parseNameComponent( bytes, pos ) ) != -1 )
-        {
-
-            do
-            {
-
-                if ( ( StringUtils.isCharASCII( bytes, pos, ',' ) == false ) &&
-                        ( StringUtils.isCharASCII( bytes, pos, ';' ) == false ) )
-                {
-
-                    break;
-                }
-
-                bytes[pos] = ',';
-                pos++;
-
-                pos = parseSpaces( bytes, pos );
-            }
-            while ( ( pos = parseNameComponent( bytes, pos ) ) != -1 );
-        }
-        else
-        {
-            try 
-            {
-                throw new DecoderException( "Bad DN : " + new String( bytes, "UTF-8" ) );
-            }
-            catch ( UnsupportedEncodingException uee )
-            {
-                throw new DecoderException( "Bad DN : " + StringUtils.dumpBytes( bytes )
);
-            }
-            
-        }
-
-        setData(bytes);
-    }
-}
-*/
 
 package org.apache.ldap.common.name;
 
@@ -140,9 +27,13 @@
 
 import javax.naming.InvalidNameException;
 import javax.naming.Name;
+import javax.naming.NamingException;
 
 import org.apache.asn1.codec.util.StringUtils;
 import org.apache.ldap.common.LdapString;
+import org.apache.ldap.common.schema.OidNormalizer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * The LdapDN class contains a DN (Distinguished Name). Its specification can be found in
RFC 2253, 
@@ -162,6 +53,9 @@
  */
 public class LdapDN extends LdapString implements Name
 {
+    /** The LoggerFactory used by this class */
+    private static Logger log = LoggerFactory.getLogger( LdapDN.class );
+
     /**
      * Declares the Serial Version Uid.
      * 
@@ -930,10 +824,11 @@
         try
         {
             LdapDN dn = (LdapDN)super.clone();
+            dn.rdns = new ArrayList();
             
             for ( int i = 0; i < rdns.size(); i++ )
             {
-                dn.rdns.set( i, ((LdapRDN)rdns.get( i ) ).clone() );
+                dn.rdns.add( i, ((LdapRDN)rdns.get( i ) ).clone() );
             }
             
             return dn;
@@ -1031,9 +926,29 @@
         }
     }
 
-    public static Name toOidName( Name dn, Map oids ) throws InvalidNameException
+    /**
+     * Change the internal DN, using OID instead of names. As we still have the UP name
+     * of each RDN, we will be able to provide both representation of the DN.
+     * 
+     * example :
+     * 
+     *  dn: cn=People, dc=example, dc=com
+     *  
+     * will be transformed to :
+     * 
+     *  2.5.4.3=People, 0.9.2342.19200300.100.1.25=example, 0.9.2342.19200300.100.1.25=com
+     *  
+     * This is really important do have such a representation, as 'cn' and 'commonname'
+     * share the same OID. To find a DN into the backend, we will use the OID as a key.
+     *  
+     * @param dn The DN to transform
+     * @param oids The mapping between names and oids. 
+     * @return A OID form of the DN
+     * @throws InvalidNameException If the DN is invalid
+     */
+    public static Name toOidName( Name dn, Map oids ) throws InvalidNameException, NamingException
     {
-    	if ( ( dn == null ) || ( dn.size() == 0 ) )
+    	if ( ( dn == null ) || ( dn.size() == 0 ) || ( oids== null ) || ( oids.size() == 0 )
)
     	{
     		return dn;
     	}
@@ -1042,38 +957,45 @@
     	
     	Enumeration rdns = newDn.getAll();
     	
+    	// Loop on all RDNs
     	while ( rdns.hasMoreElements() )
     	{
     		 LdapRDN rdn = (LdapRDN)rdns.nextElement();
     		 
     		 if ( rdn.getNbAtavs() > 1 )
     		 {
+    			 // We have more than one ATAV for this RDN. We will loop on all ATAVs
     			 
     		 }
     		 else
     		 {
+    			 // We have only one ATAV
     			 AttributeTypeAndValue atav = rdn.getAtav();
     			 String type = StringUtils.trim( atav.getType() );
     				 
-    			if ( StringUtils.isEmpty( StringUtils.lowerCase( type ) ) == false )
-    			{
-    				String oid = (String)oids.get( type );
+    			 if ( StringUtils.isNotEmpty( StringUtils.lowerCase( type ) ) )
+    			 {
+    				OidNormalizer oidNormalizer = (OidNormalizer)oids.get( type );
     					 
-					 if ( oid != null )
+					 if ( oidNormalizer != null )
 					 {
 						 rdn.removeAttributeTypeAndValue( atav.getType() );
-						 rdn.addAttributeTypeAndValue( oid, atav.getValue() );
+						 rdn.addAttributeTypeAndValue( oidNormalizer.getOid(), 
+								 (String)oidNormalizer.getNormalizer().normalize( atav.getValue() ) );
 					 }
 					 else
 					 {
-						 return null;
+						 // We don't have a normalizer for this OID : just do nothing.
+						 continue;
 					 }
 				 }
 				 else
 				 {
-					 return null;
+					 // The type is empty : this is not possible... 
+					 log.error( "There is an empty type fo the DN '" + newDn.upName +"'. This is not allowed"
);
+					 throw new InvalidNameException( "Empty type not allowed in a DN" );
 				 }
-			 }
+    		 }
     	}
     	
     	return newDn;



Mime
View raw message