harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From qi...@apache.org
Subject svn commit: r660412 - 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 Tue, 27 May 2008 07:18:29 GMT
Author: qiuxx
Date: Tue May 27 00:18:29 2008
New Revision: 660412

URL: http://svn.apache.org/viewvc?rev=660412&view=rev
Log:
Apply patch for HARMONY-5841, ([classlib][jndi][ldap] - add support for property java.naming.ldap.attributes.binary)

Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
    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/LdapSearchResult.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/SearchOp.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java?rev=660412&r1=660411&r2=660412&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
Tue May 27 00:18:29 2008
@@ -20,7 +20,9 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Vector;
 
 import javax.naming.NameNotFoundException;
 import javax.naming.NamingEnumeration;
@@ -44,29 +46,24 @@
 
     private static final long serialVersionUID = -6492847268062616321L;
 
-    /**
-     * whether the value of attribute is binary
-     */
-    private boolean isBinary;
-
     private LdapContextImpl context = null;
 
     private static HashSet<String> BINARY_ATTRIBUTE = new HashSet<String>();
     static {
-        BINARY_ATTRIBUTE.add("photo");
-        BINARY_ATTRIBUTE.add("personalSignature");
-        BINARY_ATTRIBUTE.add("audio");
-        BINARY_ATTRIBUTE.add("jpegPhoto");
-        BINARY_ATTRIBUTE.add("javaSerializedData");
-        BINARY_ATTRIBUTE.add("thumbnailPhoto");
-        BINARY_ATTRIBUTE.add("thumbnailLogo");
-        BINARY_ATTRIBUTE.add("userPassword");
-        BINARY_ATTRIBUTE.add("userCertificate");
-        BINARY_ATTRIBUTE.add("cACertificate");
-        BINARY_ATTRIBUTE.add("authorityRevocationList");
-        BINARY_ATTRIBUTE.add("certificateRevocationList");
-        BINARY_ATTRIBUTE.add("crossCertificatePair");
-        BINARY_ATTRIBUTE.add("x500UniqueIdentifier");
+        BINARY_ATTRIBUTE.add("photo".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("personalSignature".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("audio".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("jpegPhoto".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("javaSerializedData".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("thumbnailPhoto".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("thumbnailLogo".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("userPassword".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("userCertificate".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("cACertificate".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("authorityRevocationList".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("certificateRevocationList".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("crossCertificatePair".toLowerCase()); //$NON-NLS-1$
+        BINARY_ATTRIBUTE.add("x500UniqueIdentifier".toLowerCase()); //$NON-NLS-1$
     }
 
     /**
@@ -79,7 +76,6 @@
 
     public LdapAttribute(String id, LdapContextImpl ctx) {
         super(id, false);
-        isBinary = isBinary(id);
         context = ctx;
     }
 
@@ -94,9 +90,9 @@
      *            may never be <code>null</code>
      * @throws NamingException
      */
-    public LdapAttribute(Attribute attr, LdapContextImpl ctx) throws NamingException {
+    public LdapAttribute(Attribute attr, LdapContextImpl ctx)
+            throws NamingException {
         super(attr.getID(), attr.isOrdered());
-        isBinary = isBinary(getID());
         NamingEnumeration<?> enu = attr.getAll();
         while (enu.hasMore()) {
             Object value = enu.next();
@@ -109,19 +105,10 @@
     public void decodeValues(Object[] vs) {
         byte[] type = (byte[]) vs[0];
         attrID = Utils.getString(type);
-        isBinary = isBinary(attrID);
         Collection<byte[]> list = (Collection<byte[]>) vs[1];
-        // FIXME: deal with java.naming.ldap.attributes.binary
-        if (!isBinary) {
-            for (byte[] bs : list) {
-                add(Utils.getString(bs));
-            }
-        } else {
-            for (byte[] bs : list) {
-                add(bs);
-            }
+        for (byte[] bs : list) {
+            add(bs);
         }
-
     }
 
     public void encodeValues(Object[] vs) {
@@ -130,7 +117,7 @@
         List<Object> list = new ArrayList<Object>(this.values.size());
 
         for (Object object : this.values) {
-            if (!isBinary && object instanceof String) {
+            if (object instanceof String) {
                 String str = (String) object;
                 object = Utils.getBytes(str);
             }
@@ -173,7 +160,35 @@
 
     }
 
-    private static boolean isBinary(String name) {
-        return BINARY_ATTRIBUTE.contains(name) || name.endsWith(";binary");
+    public void convertValueToString() {
+        // values can't be null
+        if (values.size() == 0) {
+            return;
+        }
+
+        Vector<Object> newValues = new Vector<Object>(values.size());
+        for (Iterator<Object> iter = values.iterator(); iter.hasNext();) {
+            Object value = iter.next();
+            newValues.add(Utils.getString(value));
+        }
+
+        values.clear();
+        values = newValues;
+    }
+
+    public static boolean isBinary(String name, String[] attrs) {
+        if (BINARY_ATTRIBUTE.contains(name.toLowerCase())
+                || name.endsWith(";binary")) { //$NON-NLS-1$
+            return true;
+        }
+
+        if (attrs != null) {
+            for (int i = 0; i < attrs.length; i++) {
+                if (name.equalsIgnoreCase(attrs[i])) {
+                    return true;
+                }
+            }
+        }
+        return false;
     }
 }

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=660412&r1=660411&r2=660412&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
Tue May 27 00:18:29 2008
@@ -143,6 +143,8 @@
 
     private List<UnsolicitedNotificationListener> unls;
 
+    private String[] binaryAttributes;
+
     private static final Control NON_CRITICAL_MANAGE_REF_CONTROL = new ManageReferralControl(
             Control.NONCRITICAL);
 
@@ -154,6 +156,8 @@
 
     private static final String LDAP_TYPES_ONLY = "java.naming.ldap.typesOnly"; //$NON-NLS-1$
 
+    private static final String LDAP_ATTRIBUTES_BINARY = "java.naming.ldap.attributes.binary";
//$NON-NLS-1$
+
     /**
      * Some properties, such as 'java.naming.security.authentication', changed
      * by <code>Context.addToEnvironment</code> or
@@ -1256,6 +1260,8 @@
             }
         }
 
+        op.getSearchResult().setBinaryAttributes(binaryAttributes);
+
         LdapMessage message = null;
         try {
             message = client.doOperation(op, requestControls);
@@ -1415,6 +1421,11 @@
             isReBind = true;
         }
 
+        if (s.equals(LDAP_ATTRIBUTES_BINARY)) {
+            String value = (String) o;
+            StringTokenizer st = new StringTokenizer(value.trim());
+            binaryAttributes = value.trim().split(" ");
+        }
         return preValue;
     }
 
@@ -1966,6 +1977,10 @@
             isReBind = true;
         }
 
+        if (s.equals(LDAP_ATTRIBUTES_BINARY)) {
+            binaryAttributes = null;
+        }
+
         return preValue;
     }
 

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=660412&r1=660411&r2=660412&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
Tue May 27 00:18:29 2008
@@ -49,6 +49,8 @@
 
     private String address;
 
+    private String[] binaryAttributes;
+
     public String getAddress() {
 		return address;
 	}
@@ -109,6 +111,9 @@
         for (Object[] objects : list) {
             LdapAttribute attr = new LdapAttribute();
             attr.decodeValues(objects);
+            if (!LdapAttribute.isBinary(attr.getID(), binaryAttributes)) {
+                attr.convertValueToString();
+            }
             attrs.put(attr);
         }
     }
@@ -140,4 +145,13 @@
     public void setRefURLs(List<String> refURLs) {
         this.refURLs = refURLs;
     }
+
+    public String[] getBinaryAttributes() {
+        return binaryAttributes;
+    }
+
+    public void setBinaryAttributes(String[] binaryAttributes) {
+        this.binaryAttributes = binaryAttributes;
+    }
+
 }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/SearchOp.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/SearchOp.java?rev=660412&r1=660411&r2=660412&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/SearchOp.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/SearchOp.java
Tue May 27 00:18:29 2008
@@ -44,6 +44,9 @@
     private LdapSearchResult result;
 
     public LdapSearchResult getSearchResult() {
+        if (result == null) {
+            result = new LdapSearchResult();
+        }
         return result;
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.java?rev=660412&r1=660411&r2=660412&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.java
Tue May 27 00:18:29 2008
@@ -81,10 +81,19 @@
         id = "cn";
         values = new Object[] { Utils.getBytes(id), valueList };
         la = new LdapAttribute();
-        // 'cn' is not binary attribute, should return string value
+        /*
+         * 'cn' is not binary attribute, but LdapAttribute.decodeValues()
+         * doesn't convert values to string, must call convertValueToString() to
+         * do it.
+         */
         la.decodeValues(values);
 
         for (int i = 0; i < la.size(); ++i) {
+            assertTrue(la.get() instanceof byte[]);
+        }
+
+        la.convertValueToString();
+        for (int i = 0; i < la.size(); ++i) {
             assertTrue(la.get() instanceof String);
         }
 
@@ -98,4 +107,41 @@
             assertTrue(la.get() instanceof byte[]);
         }
     }
+
+    public void test_isBinary() {
+        assertTrue(LdapAttribute.isBinary("photo", null));
+        assertTrue(LdapAttribute.isBinary("Photo", null));
+        assertTrue(LdapAttribute.isBinary("photo", new String[0]));
+        assertTrue(LdapAttribute.isBinary("cn;binary", null));
+        assertTrue(LdapAttribute.isBinary("cn;binary", new String[0]));
+
+        assertFalse(LdapAttribute.isBinary("cn", null));
+        assertFalse(LdapAttribute.isBinary("cn",
+                new String[] { "ou", "person" }));
+        assertTrue(LdapAttribute.isBinary("cn", new String[] { "ou", "person",
+                "cn" }));
+        assertTrue(LdapAttribute.isBinary("cn", new String[] { "ou", "person",
+                "Cn" }));
+    }
+
+    public void test_convertValueToString() throws Exception {
+        LdapAttribute attr = new LdapAttribute();
+
+        // do nothing
+        attr.convertValueToString();
+
+        BasicAttribute basicAttribute = new BasicAttribute("cn");
+        attr = new LdapAttribute(basicAttribute, null);
+
+        // do nothing
+        attr.convertValueToString();
+        attr.add(Utils.getBytes("test"));
+        attr.add(Utils.getBytes("binary"));
+
+        attr.convertValueToString();
+
+        assertEquals(2, attr.size());
+        assertEquals("test", attr.get(0));
+        assertEquals("binary", attr.get(1));
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java?rev=660412&r1=660411&r2=660412&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
Tue May 27 00:18:29 2008
@@ -363,7 +363,7 @@
                         new EncodableLdapResult(), null) });
 
         context.search("cn=test", null);
-        
+
     }
 
     public void testReferralFollow() throws Exception {
@@ -446,6 +446,43 @@
 
     }
 
+    public void testAddToEnvironment_binaryAttributeProp() throws Exception {
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        assertNull(env.get(Context.REFERRAL));
+
+        InitialDirContext initialDirContext = new InitialDirContext(env);
+
+        initialDirContext.addToEnvironment(
+                "java.naming.ldap.attributes.binary", "cn");
+        assertEquals("cn", initialDirContext.getEnvironment().get(
+                "java.naming.ldap.attributes.binary"));
+
+        initialDirContext.addToEnvironment(
+                "java.naming.ldap.attributes.binary", "cn ou");
+        assertEquals("cn ou", initialDirContext.getEnvironment().get(
+                "java.naming.ldap.attributes.binary"));
+
+        initialDirContext.addToEnvironment(
+                "java.naming.ldap.attributes.binary", " cn ");
+        assertEquals(" cn ", initialDirContext.getEnvironment().get(
+                "java.naming.ldap.attributes.binary"));
+
+        initialDirContext.addToEnvironment(
+                "java.naming.ldap.attributes.binary", " cn ou ");
+        assertEquals(" cn ou ", initialDirContext.getEnvironment().get(
+                "java.naming.ldap.attributes.binary"));
+
+        try {
+            initialDirContext.addToEnvironment(
+                    "java.naming.ldap.attributes.binary", new Object());
+            fail("Should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+    }
+
     public void testReconnect() throws Exception {
         Control[] expected = new Control[] { new PagedResultsControl(10,
                 Control.NONCRITICAL) };
@@ -527,6 +564,12 @@
         another.reconnect(null);
     }
 
+    /*
+     * This test would block on RI, that because of difference of inner
+     * implementation between RI and Harmony, It's hard to emulate using
+     * MockServer. If run in real environment, the test will pass both on RI and
+     * Harmony.
+     */
     public void testFederation() throws Exception {
         server.setResponseSeq(new LdapMessage[] { new LdapMessage(
                 LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
@@ -559,8 +602,8 @@
             assertEquals(1, e.getResolvedName().size());
             assertEquals("/", e.getResolvedName().toString());
             assertTrue(e.getAltNameCtx() instanceof LdapContext);
-            assertEquals(context.getNameInNamespace(), e
-                    .getAltNameCtx().getNameInNamespace());
+            assertEquals(context.getNameInNamespace(), e.getAltNameCtx()
+                    .getNameInNamespace());
             assertTrue(e.getResolvedObj() instanceof Reference);
 
             Reference ref = (Reference) e.getResolvedObj();
@@ -571,8 +614,8 @@
             assertEquals(1, ref.size());
             RefAddr addr = ref.get(0);
             assertTrue(addr.getContent() instanceof LdapContext);
-            assertEquals(context.getNameInNamespace(),
-                    ((LdapContext) addr.getContent()).getNameInNamespace());
+            assertEquals(context.getNameInNamespace(), ((LdapContext) addr
+                    .getContent()).getNameInNamespace());
             assertEquals("nns", addr.getType());
         }
 
@@ -607,8 +650,8 @@
             assertEquals(1, e.getResolvedName().size());
             assertEquals("/", e.getResolvedName().toString());
             assertTrue(e.getAltNameCtx() instanceof LdapContext);
-            assertEquals(context.getNameInNamespace(), e
-                    .getAltNameCtx().getNameInNamespace());
+            assertEquals(context.getNameInNamespace(), e.getAltNameCtx()
+                    .getNameInNamespace());
             assertTrue(e.getResolvedObj() instanceof Reference);
 
             Reference ref = (Reference) e.getResolvedObj();
@@ -619,11 +662,12 @@
             assertEquals(1, ref.size());
             RefAddr addr = ref.get(0);
             assertTrue(addr.getContent() instanceof LdapContext);
-            assertEquals(context.getNameInNamespace(),
-                    ((LdapContext) addr.getContent()).getNameInNamespace());
+            assertEquals(context.getNameInNamespace(), ((LdapContext) addr
+                    .getContent()).getNameInNamespace());
             assertEquals("nns", addr.getType());
         }
     }
+
     public void testUnsolicitedNotification() throws Exception {
         server.setResponseSeq(new LdapMessage[] { new LdapMessage(
                 LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });



Mime
View raw message