harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvj...@apache.org
Subject svn commit: r595195 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/provider/ldap/ main/java/org/apache/harmony/jndi/provider/ldap/asn1/ test/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/har...
Date Thu, 15 Nov 2007 03:22:50 GMT
Author: lvjing
Date: Wed Nov 14 19:22:49 2007
New Revision: 595195

URL: http://svn.apache.org/viewvc?rev=595195&view=rev
Log:
Apply for Harmony-5121.

Added:
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1LdapFilterTest.java
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/asn1/ASN1LdapFilter.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.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=595195&r1=595194&r2=595195&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
Wed Nov 14 19:22:49 2007
@@ -19,6 +19,7 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
 
 import javax.naming.NamingEnumeration;
@@ -52,28 +53,55 @@
     private DirContext attributeSyntaxDefinition = null;
 
     /**
-     * constructor for LdapAttribute
+     * whether the value of attribute is binary
+     */
+    private boolean isBinary;
+
+    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");
+    }
+
+    /**
+     * constructor for decode
      * 
      */
-    protected LdapAttribute() {
+    public LdapAttribute() {
         super("", false); //$NON-NLS-1$
     }
 
     public LdapAttribute(String id) {
         super(id, false);
+        isBinary = isBinary(id);
     }
 
     /**
      * Constructs instance from already existing <code>Attribute</code>
      * 
      * @param attr
+     *            may never be <code>null</code>
      * @throws NamingException
      */
     public LdapAttribute(Attribute attr) throws NamingException {
         super(attr.getID(), attr.isOrdered());
-        NamingEnumeration<?> values = attr.getAll();
-        while (values.hasMore()) {
-            add(values.next());
+        isBinary = isBinary(getID());
+        NamingEnumeration<?> enu = attr.getAll();
+        while (enu.hasMore()) {
+            Object value = enu.next();
+            add(value);
         }
 
         attributeDefinition = null;
@@ -81,32 +109,38 @@
     }
 
     @SuppressWarnings("unchecked")
-    public void decodeValues(Object[] values) {
-        byte[] type = (byte[]) values[0];
+    public void decodeValues(Object[] vs) {
+        byte[] type = (byte[]) vs[0];
         attrID = Utils.getString(type);
-        Collection<byte[]> list = (Collection<byte[]>) values[1];
-        for (byte[] bs : list) {
-            // TODO: convert to corresponding java type according to schema
-            add(Utils.getString(bs));
+        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);
+            }
         }
 
     }
 
-    public void encodeValues(Object[] values) {
-        values[0] = Utils.getBytes(attrID);
+    public void encodeValues(Object[] vs) {
+        vs[0] = Utils.getBytes(attrID);
 
         List<Object> list = new ArrayList<Object>(this.values.size());
 
         for (Object object : this.values) {
-            // TODO: convert other types to byte[]
-            if (object instanceof String) {
+            if (!isBinary && object instanceof String) {
                 String str = (String) object;
                 object = Utils.getBytes(str);
             }
 
             list.add(object);
         }
-        values[1] = list;
+        vs[1] = list;
     }
 
     @Override
@@ -126,5 +160,9 @@
         }
         // TODO: Not yet implemented
         throw new NotYetImplementedException();
+    }
+
+    private static boolean isBinary(String name) {
+        return BINARY_ATTRIBUTE.contains(name) || name.endsWith(";binary");
     }
 }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1LdapFilter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1LdapFilter.java?rev=595195&r1=595194&r2=595195&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1LdapFilter.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1LdapFilter.java
Wed Nov 14 19:22:49 2007
@@ -19,6 +19,7 @@
 
 import java.io.IOException;
 
+import org.apache.harmony.jndi.provider.ldap.Filter;
 import org.apache.harmony.jndi.provider.ldap.asn1.ASN1ChoiceWrap.ChosenValue;
 import org.apache.harmony.security.asn1.ASN1Choice;
 import org.apache.harmony.security.asn1.ASN1Type;
@@ -52,11 +53,14 @@
         if (type == null) {
             type = (ASN1Choice) LdapASN1Constant.Filter;
         }
-        ChosenValue chosen = (ChosenValue) out.content;
-        int index = chosen.getIndex();
-        byte[] bytes = type.type[index].encode(chosen.getValue());
-        out.content = bytes;
-        out.length = bytes.length;
+        // has not been decoded
+        if (!(out.content instanceof byte[])) {
+            ChosenValue chosen = (ChosenValue) out.content;
+            int index = chosen.getIndex();
+            byte[] bytes = type.type[index].encode(chosen.getValue());
+            out.content = bytes;
+            out.length = bytes.length;
+        }
         // TODO: Any way better to do this(append out.content to out.encoded)?
         out.encodeString();
     }
@@ -69,8 +73,24 @@
 
     @Override
     public void setEncodingContent(BerOutputStream out) {
-        // FIXME: do nothing if never be called
-        throw new RuntimeException();
+        if (type == null) {
+            type = (ASN1Choice) LdapASN1Constant.Filter;
+        }
+
+        ChosenValue chosen = null;
+        if (out.content instanceof Filter) {
+            Object[] values = new Object[1];
+            ((Filter) out.content).encodeValues(values);
+            chosen = (ChosenValue) values[0];
+        } else {
+            chosen = (ChosenValue) out.content;
+        }
+
+        int index = chosen.getIndex();
+        byte[] bytes = type.type[index].encode(chosen.getValue());
+
+        out.content = bytes;
+        out.length = bytes.length;
     }
 
 }

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=595195&r1=595194&r2=595195&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
Wed Nov 14 19:22:49 2007
@@ -17,6 +17,9 @@
 
 package org.apache.harmony.jndi.provider.ldap;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import javax.naming.NamingException;
 import javax.naming.directory.BasicAttribute;
 
@@ -37,23 +40,62 @@
         attr.add(v2);
         attr.add(v3);
         ASN1TestUtils.checkEncode(attr, LdapASN1Constant.Attribute);
-        
+
         Object[] encoded = new Object[2];
         attr.encodeValues(encoded);
         LdapAttribute decoded = new LdapAttribute("");
         decoded.decodeValues(encoded);
-        
+
         assertEquals(attr.getID(), decoded.getID());
 
     }
-    
+
     public void test_constructor_LAttribute() throws Exception {
         BasicAttribute attr = new BasicAttribute("cn");
         attr.add("test");
         attr.add("harmony");
         LdapAttribute la = new LdapAttribute(attr);
-        
+
         ASN1TestUtils.checkEncode(la, LdapASN1Constant.Attribute);
-        
+
+    }
+
+    public void test_binary_attribute() throws Exception {
+        String id = "photo";
+        List<byte[]> valueList = new ArrayList<byte[]>();
+        byte[] bs = new byte[] { 0, 1, 2, 3, 4, 5 };
+        valueList.add(bs);
+        bs = Utils.getBytes("value");
+        valueList.add(bs);
+
+        Object[] values = new Object[] { Utils.getBytes(id), valueList };
+
+        LdapAttribute la = new LdapAttribute();
+        // 'photo' is binary attribute
+        la.decodeValues(values);
+
+        for (int i = 0; i < la.size(); ++i) {
+            assertTrue(la.get(i) instanceof byte[]);
+        }
+
+        id = "cn";
+        values = new Object[] { Utils.getBytes(id), valueList };
+        la = new LdapAttribute();
+        // 'cn' is not binary attribute, should return string value
+        la.decodeValues(values);
+
+        for (int i = 0; i < la.size(); ++i) {
+            assertTrue(la.get() instanceof String);
+        }
+
+        id = "cn;binary";
+        values = new Object[] { Utils.getBytes(id), valueList };
+        la = new LdapAttribute();
+        // 'cn;binary' is binary attribute
+        la.decodeValues(values);
+
+        for (int i = 0; i < la.size(); ++i) {
+            assertTrue(la.get() instanceof byte[]);
+        }
     }
 }

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1LdapFilterTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1LdapFilterTest.java?rev=595195&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1LdapFilterTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1LdapFilterTest.java
Wed Nov 14 19:22:49 2007
@@ -0,0 +1,52 @@
+/* 
+ *  Licensed to the Apache Software Foundation (ASF) under one or more 
+ *  contributor license agreements.  See the NOTICE file distributed with 
+ *  this work for additional information regarding copyright ownership. 
+ *  The ASF licenses this file to You under the Apache License, Version 2.0 
+ *  (the "License"); you may not use this file except in compliance with 
+ *  the License.  You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0 
+ * 
+ *  Unless required by applicable law or agreed to in writing, software 
+ *  distributed under the License is distributed on an "AS IS" BASIS, 
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ *  See the License for the specific language governing permissions and 
+ *  limitations under the License. 
+ */
+
+package org.apache.harmony.jndi.provider.ldap.asn1;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.jndi.provider.ldap.Filter;
+import org.apache.harmony.jndi.provider.ldap.parser.FilterParser;
+
+public class ASN1LdapFilterTest extends TestCase {
+    public void test_encode() throws Exception {
+        FilterParser parser = new FilterParser("(&(cn=1)(o=2))");
+        Filter filter = parser.parse();
+        LdapASN1Constant.Filter.encode(filter);
+        ASN1TestUtils.checkEncode(filter, LdapASN1Constant.Filter);
+
+        parser = new FilterParser("(object=1)");
+        filter = parser.parse();
+        LdapASN1Constant.Filter.encode(filter);
+        ASN1TestUtils.checkEncode(filter, LdapASN1Constant.Filter);
+        
+        parser = new FilterParser("(cn=*hello*)");
+        filter = parser.parse();
+        LdapASN1Constant.Filter.encode(filter);
+        ASN1TestUtils.checkEncode(filter, LdapASN1Constant.Filter);
+        
+        parser = new FilterParser("(sn=Lu\\c4\\8di\\c4\\87)");
+        filter = parser.parse();
+        LdapASN1Constant.Filter.encode(filter);
+        ASN1TestUtils.checkEncode(filter, LdapASN1Constant.Filter);
+        
+        parser = new FilterParser("(&(objectClass=Person)(|(sn=Jensen)(cn=Babs J*)))");
+        filter = parser.parse();
+        LdapASN1Constant.Filter.encode(filter);
+        ASN1TestUtils.checkEncode(filter, LdapASN1Constant.Filter);
+    }
+}



Mime
View raw message