harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r641836 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/
Date Thu, 27 Mar 2008 14:42:20 GMT
Author: tellison
Date: Thu Mar 27 07:42:19 2008
New Revision: 641836

URL: http://svn.apache.org/viewvc?rev=641836&view=rev
Log:
Apply patch HARMONY-5658 ([classlib][jndi][ldap] - Ldap Attributes should ignore case)

Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaAttrDefContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaClassDefContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java?rev=641836&r1=641835&r2=641836&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
Thu Mar 27 07:42:19 2008
@@ -426,7 +426,7 @@
 
     private Attributes convertSerialToAttribute(Serializable serializable)
             throws NamingException {
-        Attributes attrs = new BasicAttributes();
+        Attributes attrs = new BasicAttributes(true);
 
         Attribute objectClass = new BasicAttribute("objectClass");
         objectClass.add("top");
@@ -474,7 +474,7 @@
     }
 
     private Attributes convertRefToAttribute(Reference ref) {
-        Attributes attrs = new BasicAttributes();
+        Attributes attrs = new BasicAttributes(true);
 
         Attribute objectClass = new BasicAttribute("objectClass");
         objectClass.add("top");
@@ -533,7 +533,7 @@
          */
 
         if (attributes == null) {
-            attributes = new BasicAttributes();
+            attributes = new BasicAttributes(true);
             Attribute attr = new LdapAttribute("objectClass", this);
             attr.add("top");
             attr.add("javaContainer");
@@ -626,7 +626,7 @@
             return first;
         }
 
-        BasicAttributes attrs = new BasicAttributes();
+        BasicAttributes attrs = new BasicAttributes(true);
         NamingEnumeration<? extends Attribute> enu = first.getAll();
         while (enu.hasMore()) {
             attrs.put(enu.next());
@@ -741,7 +741,7 @@
         }
 
         // no attribute retrieved from server, return a empty Attributes
-        return new BasicAttributes();
+        return new BasicAttributes(true);
     }
 
     public Attributes getAttributes(String s) throws NamingException {
@@ -1276,7 +1276,7 @@
                 sr.setNameInNamespace(dn.substring(index + 1, dn.length()));
                 list.add(sr);
             } else {
-                String relativeName = convertToRelativeName(dn, baseDN);
+                String relativeName = LdapUtils.convertToRelativeName(dn, baseDN);
                 sr = new SearchResult(relativeName, null, entries.get(dn));
                 sr.setNameInNamespace(dn);
             }
@@ -1334,7 +1334,7 @@
                 sr.setNameInNamespace(dn.substring(index + 1, dn.length()));
                 list.add(sr);
             } else {
-                String relativeName = convertToRelativeName(dn, baseDN);
+                String relativeName = LdapUtils.convertToRelativeName(dn, baseDN);
                 sr = new SearchResult(relativeName, null, entries.get(dn));
                 sr.setNameInNamespace(dn);
             }
@@ -1736,7 +1736,7 @@
         tempName.addAll(name);
         String baseDN = tempName.toString();
         for (String dn : entries.keySet()) {
-            String relativeName = convertToRelativeName(dn, baseDN);
+            String relativeName = LdapUtils.convertToRelativeName(dn, baseDN);
             Attributes attrs = entries.get(dn);
             Attribute attrClass = attrs.get("javaClassName");
             String className = null;
@@ -1760,29 +1760,6 @@
                 .getException());
     }
 
-    /**
-     * convert absolute dn to the dn relatived to the dn of
-     * <code>targetContextDN</code>.
-     * 
-     * @param dn
-     *            absolute dn
-     * @param base
-     *            base dn of the relative name
-     * @return dn relatived to the <code>dn</code> of <code>base</code>
-     */
-    protected String convertToRelativeName(String dn, String base) {
-
-        if (base.equals("")) {
-            return dn;
-        }
-
-        int index = dn.lastIndexOf(base);
-        if (index == 0) {
-            return "";
-        }
-
-        return dn.substring(0, index - 1);
-    }
 
     protected String getTargetDN(Name name, Name prefix)
             throws NamingException, InvalidNameException {
@@ -2485,7 +2462,11 @@
                 // construct event
                 if (obj instanceof ECNotificationControl) {
                     ECNotificationControl control = (ECNotificationControl) obj;
-                    event = constructNamingEvent(this, control, baseDN);
+                    try {
+                        event = constructNamingEvent(this, control, baseDN);
+                    } catch (NamingException e) {
+                        // FIXME may never reach
+                    }
                 }
 
                 if (obj instanceof LdapResult) {
@@ -2579,36 +2560,36 @@
     }
 
     private NamingEvent constructNamingEvent(PersistentSearchResult result,
-            ECNotificationControl control, String baseDN) {
+            ECNotificationControl control, String baseDN) throws InvalidNameException, NamingException
{
         Binding newBinding = null;
         Binding oldBinding = null;
 
         switch (control.getChangeType()) {
         case ECNotificationControl.ADD:
-            String newName = convertToRelativeName(result.getDn(), baseDN);
+            String newName = LdapUtils.convertToRelativeName(result.getDn(), baseDN);
             newBinding = new Binding(newName, null);
             newBinding.setNameInNamespace(result.getDn());
             break;
         case ECNotificationControl.DELETE:
-            String deleteName = convertToRelativeName(result.getDn(), baseDN);
+            String deleteName = LdapUtils.convertToRelativeName(result.getDn(), baseDN);
             oldBinding = new Binding(deleteName, null);
             oldBinding.setNameInNamespace(result.getDn());
             break;
         case ECNotificationControl.MODIFY_DN:
             if (result.getDn() != null) {
-                newBinding = new Binding(convertToRelativeName(result.getDn(),
+                newBinding = new Binding(LdapUtils.convertToRelativeName(result.getDn(),
                         baseDN), null);
                 newBinding.setNameInNamespace(result.getDn());
             }
 
             if (control.getPreviousDN() != null) {
-                oldBinding = new Binding(convertToRelativeName(control
+                oldBinding = new Binding(LdapUtils.convertToRelativeName(control
                         .getPreviousDN(), baseDN), null);
                 oldBinding.setNameInNamespace(control.getPreviousDN());
             }
             break;
         case ECNotificationControl.MODIFY:
-            String relativeName = convertToRelativeName(result.getDn(), baseDN);
+            String relativeName = LdapUtils.convertToRelativeName(result.getDn(), baseDN);
             newBinding = new Binding(relativeName, null);
             newBinding.setNameInNamespace(result.getDn());
             // FIXME: how to get old binding?

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaAttrDefContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaAttrDefContextImpl.java?rev=641836&r1=641835&r2=641836&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaAttrDefContextImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaAttrDefContextImpl.java
Thu Mar 27 07:42:19 2008
@@ -47,7 +47,7 @@
 
     public Attributes getAttributes(Name name, String[] as)
             throws NamingException {
-        BasicAttributes attributes = new BasicAttributes();
+        BasicAttributes attributes = new BasicAttributes(true);
         Set<String> keySet = attrTree.keySet();
         Iterator<String> iter = keySet.iterator();
         while (iter.hasNext()) {

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaClassDefContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaClassDefContextImpl.java?rev=641836&r1=641835&r2=641836&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaClassDefContextImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaClassDefContextImpl.java
Thu Mar 27 07:42:19 2008
@@ -57,7 +57,7 @@
                         .findSchemaDefInfo(
                                 LdapSchemaContextImpl.OBJECT_CLASSES, className
                                         .toLowerCase());
-                as = new BasicAttributes();
+                as = new BasicAttributes(true);
                 Set<String> keySet = classDefInfo.keySet();
                 for (Iterator<String> iterator = keySet.iterator(); iterator
                         .hasNext();) {

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java?rev=641836&r1=641835&r2=641836&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
Thu Mar 27 07:42:19 2008
@@ -188,7 +188,7 @@
     public Attributes getAttributes(Name name) throws NamingException {
         Name targetDN = (rdn.size() != 0) ? name.addAll(rdn) : name;
 
-        BasicAttributes schemaAttributes = new BasicAttributes();
+        BasicAttributes schemaAttributes = new BasicAttributes(true);
 
         Set<String> keyset = null;
         int size = targetDN.size();
@@ -209,7 +209,7 @@
             if (null == classDef) {
                 throw new NameNotFoundException(name.toString());
             }
-            schemaAttributes = new BasicAttributes();
+            schemaAttributes = new BasicAttributes(true);
             keyset = classDef.keySet();
             for (Iterator<String> i = keyset.iterator(); i.hasNext();) {
                 String id = i.next();
@@ -228,7 +228,7 @@
             throws NamingException {
         Attributes attrs = getAttributes(name);
         Attribute attr = null;
-        Attributes filteredAttrs = new BasicAttributes();
+        Attributes filteredAttrs = new BasicAttributes(true);
         for (int i = 0; i < as.length; i++) {
             attr = attrs.get(as[i]);
             if (attr != null) {
@@ -543,7 +543,7 @@
         boolean hasAttributes2Return = (null != as && 0 != as.length);
 
         // Attribute matcher
-        BasicAttributes attrMatcher = new BasicAttributes();
+        BasicAttributes attrMatcher = new BasicAttributes(true);
         if (hasMatchingAttributes) {
             NamingEnumeration<? extends Attribute> attrEnum = attributes
                     .getAll();
@@ -611,7 +611,7 @@
                      * encountered attribute value,
                      */
                     if (attribute.contains(schemaType)) {
-                        BasicAttributes basicAttributes = new BasicAttributes();
+                        BasicAttributes basicAttributes = new BasicAttributes(true);
                         /*
                          * if(objectclassIndex == -1), then No name was choose,
                          * which means SearchResult will have empty
@@ -629,7 +629,7 @@
             } else {
                 for (int i = 0; i < attrValues.size(); i++) {
                     schemaType = schemaLdap2Jndi.get(attrValues.get(i));
-                    BasicAttributes basicAttributes = new BasicAttributes();
+                    BasicAttributes basicAttributes = new BasicAttributes(true);
                     /*
                      * if(objectclassIndex == -1), then No name was choose,
                      * which means SearchResult will have empty BasicAttributes.
@@ -674,7 +674,7 @@
                         }
                     }
                     if (matched) {
-                        basicAttributes = new BasicAttributes();
+                        basicAttributes = new BasicAttributes(true);
                         for (Iterator<String> iterator = schemaDef.keySet()
                                 .iterator(); iterator.hasNext();) {
                             String key = iterator.next();
@@ -695,7 +695,7 @@
                 for (int i = 0; i < attrValues.size(); i++) {
                     Hashtable<String, Object> schemaDef = schemas
                             .get(attrValues.get(i));
-                    basicAttributes = new BasicAttributes();
+                    basicAttributes = new BasicAttributes(true);
                     for (Iterator<String> iterator = schemaDef.keySet()
                             .iterator(); iterator.hasNext();) {
                         String key = iterator.next();
@@ -782,7 +782,7 @@
             throw ex;
         }
 
-        BasicAttributes matchingAttrs = new BasicAttributes();
+        BasicAttributes matchingAttrs = new BasicAttributes(true);
         extractMatchingAttributes(f, matchingAttrs);
 
         return search(name, matchingAttrs, searchControls

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.java?rev=641836&r1=641835&r2=641836&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSearchResult.java
Thu Mar 27 07:42:19 2008
@@ -101,7 +101,7 @@
         if (entries.containsKey(name)) {
             attrs = entries.get(name);
         } else {
-            attrs = new BasicAttributes();
+            attrs = new BasicAttributes(true);
             entries.put(name, attrs);
         }
 

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java?rev=641836&r1=641835&r2=641836&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapUtils.java
Thu Mar 27 07:42:19 2008
@@ -26,6 +26,7 @@
 import javax.naming.ContextNotEmptyException;
 import javax.naming.InvalidNameException;
 import javax.naming.LimitExceededException;
+import javax.naming.Name;
 import javax.naming.NameAlreadyBoundException;
 import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
@@ -41,6 +42,7 @@
 import javax.naming.directory.InvalidSearchFilterException;
 import javax.naming.directory.NoSuchAttributeException;
 import javax.naming.directory.SchemaViolationException;
+import javax.naming.ldap.LdapName;
 
 import org.apache.harmony.jndi.internal.nls.Messages;
 import org.apache.harmony.jndi.provider.ldap.parser.FilterParser;
@@ -178,5 +180,44 @@
             return new NamingException(Messages.getString("ldap.35", //$NON-NLS-1$
                     new Object[] { errorCode }));
         }
+    }
+
+    /**
+     * convert absolute dn to the dn relatived to the dn of
+     * <code>targetContextDN</code>.
+     * 
+     * @param dn
+     *            absolute dn
+     * @param base
+     *            base dn of the relative name
+     * @return dn relatived to the <code>dn</code> of <code>base</code>
+     * @throws NamingException
+     * @throws InvalidNameException
+     */
+    public static String convertToRelativeName(String dn, String base)
+            throws InvalidNameException, NamingException {
+        return convertToRelativeName(new LdapName(dn), new LdapName(base))
+                .toString();
+    }
+
+    public static LdapName convertToRelativeName(LdapName dn, LdapName base)
+            throws NamingException {
+        if (base.size() == 0) {
+            return dn;
+        }
+
+        if (dn.size() < base.size()) {
+            // TODO add error message
+            throw new NamingException("");
+        }
+
+        Name prefix = dn.getPrefix(base.size());
+        if (!prefix.equals(base)) {
+            // TODO add error message
+            throw new NamingException("");
+        }
+
+        return (LdapName) dn.getSuffix(base.size());
+
     }
 }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.java?rev=641836&r1=641835&r2=641836&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapUtilsTest.java
Thu Mar 27 07:42:19 2008
@@ -18,9 +18,11 @@
 package org.apache.harmony.jndi.provider.ldap;
 
 import javax.naming.CommunicationException;
+import javax.naming.InvalidNameException;
 import javax.naming.NamingException;
 import javax.naming.TimeLimitExceededException;
 import javax.naming.directory.InvalidSearchFilterException;
+import javax.naming.ldap.LdapName;
 
 import junit.framework.TestCase;
 
@@ -85,11 +87,101 @@
         assertEquals("test", pair.getValue());
     }
 
-    private LdapResult getLdapResult(int errorCode, String message) {
-        LdapResult result = new LdapResult();
-        if (message == null) {
+    public void test_convertToRelativeName() throws Exception {
+        LdapName base = new LdapName("");
+        LdapName dn = new LdapName("cn=test,o=harmony");
+
+        LdapName relative = LdapUtils.convertToRelativeName(dn, base);
+        assertEquals(dn, relative);
+
+        base = new LdapName("o=harmony");
+        relative = LdapUtils.convertToRelativeName(dn, base);
+        assertEquals(new LdapName("cn=test"), relative);
+
+        base = new LdapName("o=harmony");
+        dn = new LdapName("cn=test,o=apache,o=harmony");
+        relative = LdapUtils.convertToRelativeName(dn, base);
+        assertEquals(new LdapName("cn=test,o=apache"), relative);
+
+        base = new LdapName("o=harmony");
+        dn = new LdapName("o=harmony");
+        relative = LdapUtils.convertToRelativeName(dn, base);
+        assertEquals(new LdapName(""), relative);
+    }
+
+    public void test_convertToRelativeName_String() throws Exception {
+        String base = "";
+        String dn = "cn=test,o=harmony";
+
+        String relative = LdapUtils.convertToRelativeName(dn, base);
+        assertEquals(dn, relative);
+
+        base = "o=harmony";
+        relative = LdapUtils.convertToRelativeName(dn, base);
+        assertEquals("cn=test", relative);
+
+        base = "o=harmony";
+        dn = "cn=test,o=apache,o=harmony";
+        relative = LdapUtils.convertToRelativeName(dn, base);
+        assertEquals("cn=test,o=apache", relative);
+
+        base = "o=harmony";
+        dn = "o=harmony";
+        relative = LdapUtils.convertToRelativeName(dn, base);
+        assertEquals("", relative);
+    }
+
+    public void test_convertToRelativeName_String_Exception() throws Exception {
+        String base = "cn=test,o=harmony";
+        String dn = "";
+
+        try {
+            LdapUtils.convertToRelativeName(dn, base);
+            fail("Should throw NamingException");
+        } catch (NamingException e) {
+            // expected
+        }
+
+        base = "o=harmony";
+        dn = "cn=test,o=apache";
+
+        try {
+            LdapUtils.convertToRelativeName(dn, base);
+            fail("Should throw NamingException");
+        } catch (NamingException e) {
+            // expected
+        }
+        try {
+            LdapUtils.convertToRelativeName("o+harmony", "");
+            fail("Should throw InvalidNameException");
+        } catch (InvalidNameException e) {
+            // expected
+        }
+    }
 
+    public void test_convertToRelativeName_Exception() throws Exception {
+        LdapName base = new LdapName("cn=test,o=harmony");
+        LdapName dn = new LdapName("");
+
+        try {
+            LdapUtils.convertToRelativeName(dn, base);
+            fail("Should throw NamingException");
+        } catch (NamingException e) {
+            // expected
         }
+
+        base = new LdapName("o=harmony");
+        dn = new LdapName("cn=test,o=apache");
+
+        try {
+            LdapUtils.convertToRelativeName(dn, base);
+            fail("Should throw NamingException");
+        } catch (NamingException e) {
+            // expected
+        }
+    }
+    private LdapResult getLdapResult(int errorCode, String message) {
+        LdapResult result = new LdapResult();
         Object[] values = new Object[] { ASN1Integer.fromIntValue(errorCode),
                 Utils.getBytes(""), Utils.getBytes(message), null };
         result.decodeValues(values);



Mime
View raw message