harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r583708 - 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, 11 Oct 2007 07:16:22 GMT
Author: tonywu
Date: Thu Oct 11 00:16:20 2007
New Revision: 583708

URL: http://svn.apache.org/viewvc?rev=583708&view=rev
Log:
Apply patch Harmony-4910 ([classlib][jndi][ldap] implements Ldap add operation request and
response messages)

Added:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/AddOp.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapOperation.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapResult.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Decodable.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Encodable.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1SequenceWrap.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/LdapASN1Constant.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/Utils.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/AddOpTest.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapResultTest.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1TestUtils.java
  (with props)

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/AddOp.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/AddOp.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/AddOp.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/AddOp.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,81 @@
+/* 
+ *  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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Decodable;
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable;
+import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
+import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
+
+/**
+ * This class represents add operation in ldap protocol, inclue one request and
+ * one response. It encode request to ASN.1 and decode response from ASN.1
+ * formate bytes to java objects.
+ */
+final public class AddOp implements LdapOperation, ASN1Encodable {
+    private List<LdapAttribute> attrList;
+
+    // LDAP distinguished name
+    private String entry;
+
+    // Attribute list
+    private LdapResult result;
+
+    public AddOp(String entry, List<LdapAttribute> attrList) {
+        this.entry = entry;
+        this.attrList = (attrList == null) ? new ArrayList<LdapAttribute>()
+                : attrList;
+    }
+
+    public AddOp(String entry) {
+        this(entry, null);
+    }
+
+    public void addAttribute(LdapAttribute attr) {
+        attrList.add(attr);
+    }
+
+    public ASN1Encodable getRequest() {
+        return this;
+    }
+
+    public int getRequestId() {
+        return LdapASN1Constant.OP_ADD_REQUEST;
+    }
+
+    public ASN1Decodable getResponse() {
+        return (result == null) ? new LdapResult() : result;
+    }
+
+    public int getResponseId() {
+        return LdapASN1Constant.OP_ADD_RESPONSE;
+    }
+
+    public LdapResult getResult() {
+        return result;
+    }
+
+    public void encodeValues(Object[] values) {
+        values[0] = Utils.getBytes(entry);
+        values[1] = attrList;
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/AddOp.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,121 @@
+/* 
+ *  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;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.DirContext;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Decodable;
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable;
+import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
+
+/**
+ * This class add supports to <code>getAttributeDefinition()</code> and
+ * <code>getAttributeSyntaxDefinition()</code> methods.
+ * 
+ */
+public class LdapAttribute extends BasicAttribute implements ASN1Decodable,
+        ASN1Encodable {
+
+    private static final long serialVersionUID = -6492847268062616321L;
+
+    /**
+     * TODO: when to initialize it?
+     */
+    private DirContext attributeDefinition = null;
+
+    /**
+     * TODO: when to initialize it?
+     */
+    private DirContext attributeSyntaxDefinition = null;
+
+    /**
+     * constructor for LdapAttribute
+     * 
+     */
+    protected LdapAttribute() {
+        super("", false); //$NON-NLS-1$
+    }
+
+    public LdapAttribute(String id) {
+        super(id, false);
+    }
+
+    /**
+     * Constructs instance from already existing <code>Attribute</code>
+     * 
+     * @param attr
+     * @throws NamingException
+     */
+    public LdapAttribute(Attribute attr) throws NamingException {
+        super(attr.getID(), attr.isOrdered());
+        NamingEnumeration<?> values = attr.getAll();
+        while (values.hasMore()) {
+            add(values.next());
+        }
+
+        attributeDefinition = attr.getAttributeDefinition();
+        attributeSyntaxDefinition = attr.getAttributeSyntaxDefinition();
+    }
+
+    @SuppressWarnings("unchecked")
+    public void decodeValues(Object[] values) {
+        byte[] type = (byte[]) values[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));
+        }
+
+    }
+
+    public void encodeValues(Object[] values) {
+        values[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) {
+                String str = (String) object;
+                object = Utils.getBytes(str);
+            }
+
+            list.add(object);
+        }
+        values[1] = list;
+    }
+
+    @Override
+    public DirContext getAttributeDefinition() throws NamingException {
+        return attributeDefinition;
+    }
+
+    @Override
+    public DirContext getAttributeSyntaxDefinition() throws NamingException {
+        return attributeSyntaxDefinition;
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapAttribute.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapOperation.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapOperation.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapOperation.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapOperation.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,56 @@
+/* 
+ *  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;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Decodable;
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable;
+
+/**
+ * The Ldap operation interface, contains methods for getting encodable request
+ * and decoded response.
+ */
+public interface LdapOperation {
+
+    /**
+     * Get encodable request instance of this operation.
+     * 
+     * @return encodable request instance of the operation
+     */
+    public ASN1Encodable getRequest();
+
+    /**
+     * Get decodable response instance of this operation.
+     * 
+     * @return decodable response instance of this operation
+     */
+    public ASN1Decodable getResponse();
+
+    /**
+     * Get request index to determine which operation schema to be used.
+     * 
+     * @return index of request of this operation
+     */
+    public int getRequestId();
+
+    /**
+     * Get response index.
+     * 
+     * @return index of response of this operation
+     */
+    public int getResponseId();
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapOperation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapResult.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapResult.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapResult.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapResult.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,79 @@
+/* 
+ *  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;
+
+import java.util.Collection;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Decodable;
+import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
+import org.apache.harmony.security.asn1.ASN1Integer;
+
+/**
+ * This class represents LDAPResult defined in RFC 2251 page 16.
+ */
+public class LdapResult implements ASN1Decodable {
+
+    private int resultCode;
+
+    private String machedDN;
+
+    private String errorMessage;
+
+    private String[] referrals;
+
+    @SuppressWarnings("unchecked")
+    public void decodeValues(Object[] values) {
+        resultCode = ASN1Integer.toIntValue(values[0]);
+        machedDN = Utils.getString((byte[]) values[1]);
+        errorMessage = Utils.getString((byte[]) values[2]);
+
+        if (values[3] != null) {
+            Collection<byte[]> list = (Collection<byte[]>) values[3];
+            if (list.size() != 0) {
+                referrals = new String[list.size()];
+                int index = 0;
+                for (byte[] bytes : list) {
+                    referrals[index++] = Utils.getString(bytes);
+                }
+            }
+        }
+    }
+
+    public String getErrorMessage() {
+        return errorMessage;
+    }
+
+    public String getMachedDN() {
+        return machedDN;
+    }
+
+    /**
+     * Retrieves the referrals.
+     * 
+     * @return A prossibly null array. <code>null</code> means no referrals
+     *         retrieved from server
+     */
+    public String[] getReferrals() {
+        return referrals;
+    }
+
+    public int getResultCode() {
+        return resultCode;
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapResult.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Decodable.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Decodable.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Decodable.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Decodable.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,33 @@
+/* 
+ *  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;
+
+/**
+ * Retrieves data from ASN.1 data stream.
+ * 
+ */
+public interface ASN1Decodable {
+
+    /**
+     * Decode <code>values</code> to corresponding java type.
+     * 
+     * @param values
+     *            values to be decoded
+     */
+    public void decodeValues(Object[] values);
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Decodable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Encodable.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Encodable.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Encodable.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Encodable.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,49 @@
+/* 
+ *  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;
+
+/**
+ * This interface is responsible for encoding data according to ASN.1 type schema.
+ * Below is type mapping between ASN.1 and Java.
+ * <p>
+ * <code>
+ * ASN.1                Java
+ * BOOLEAN              Boolean
+ * INTEGER              byte[] (encode: ASN1Integer.fromIntValue(int value)  decode: ASN1Integer.toIntValue(Object
decoded))
+ * OCTET STRING         byte[] (encode: Utils.getBytes(String s)    decode: getString(byte[]
bytes))
+ * ENUMERATED           byte[] (encode: ASN1Integer.fromIntValue(int value)  decode: ASN1Integer.toIntValue(Object
decoded))
+ * SEQUENCE             Object[] or ASN1Encodable
+ * SEQUENCE OF          java.util.List
+ * SET OF               java.util.List
+ * CHOICE               Object[] or ChosenValue
+ * </code>
+ * 
+ */
+public interface ASN1Encodable {
+
+    /**
+     * Encodes data into <code>values</code>. It is caller's responsibility
+     * to create <code>values</code> array, and the size of
+     * <code>values</code> is defined by the ASN.1 type schema which is to be
+     * encoded. Classes realize this interface need to fill encoded value into
+     * the <code>values</code> array.
+     * 
+     * @param values
+     */
+    public void encodeValues(Object[] values);
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Encodable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1SequenceWrap.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1SequenceWrap.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1SequenceWrap.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1SequenceWrap.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,46 @@
+/* 
+ *  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 org.apache.harmony.security.asn1.ASN1Sequence;
+import org.apache.harmony.security.asn1.ASN1Type;
+
+/**
+ * When encoding, the value bind to ASN1SequenceWrap should be either array of
+ * object or ASN1Encodable instance. If it is object array, the length of the
+ * arry should be equals to the length of ASN1SequenceWrap. If it is
+ * ASN1Encodable instance, <code>ASN1Encodable.encodeValues()</code> will be
+ * invoked.
+ * 
+ */
+public class ASN1SequenceWrap extends ASN1Sequence {
+
+    public ASN1SequenceWrap(ASN1Type[] type) {
+        super(type);
+    }
+
+    @Override
+    protected void getValues(Object object, Object values[]) {
+        if (object instanceof ASN1Encodable) {
+            ((ASN1Encodable) object).encodeValues(values);
+        } else {
+            Object[] objs = (Object[]) object;
+            System.arraycopy(objs, 0, values, 0, objs.length);
+        }
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1SequenceWrap.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/LdapASN1Constant.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/LdapASN1Constant.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/LdapASN1Constant.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/LdapASN1Constant.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,50 @@
+/* 
+ *  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 org.apache.harmony.security.asn1.ASN1Constants;
+import org.apache.harmony.security.asn1.ASN1Implicit;
+import org.apache.harmony.security.asn1.ASN1OctetString;
+import org.apache.harmony.security.asn1.ASN1SequenceOf;
+import org.apache.harmony.security.asn1.ASN1SetOf;
+import org.apache.harmony.security.asn1.ASN1Type;
+
+/**
+ * This class contains all ASN.1 type defined in RFC 2251.
+ */
+public class LdapASN1Constant {
+    public static final int OP_ADD_REQUEST = 9;
+    
+    public static final int OP_ADD_RESPONSE = 10;
+    
+    public static final ASN1Type Attribute = new ASN1SequenceWrap(
+            new ASN1Type[] { ASN1OctetString.getInstance(), // type
+                    new ASN1SetOf(ASN1OctetString.getInstance()) }); // vals
+    
+    public static final ASN1Type AttributeList = new ASN1SequenceOf(
+            new ASN1SequenceWrap(new ASN1Type[] {
+                    ASN1OctetString.getInstance(), // type
+                    new ASN1SetOf(ASN1OctetString.getInstance()) })); // values
+    
+    public static final ASN1Type AddRequest = new ASN1Implicit(
+            ASN1Constants.CLASS_APPLICATION, 8, new ASN1SequenceWrap(
+                    new ASN1Type[] { ASN1OctetString.getInstance(), // entry
+                            AttributeList })); // attributes
+    
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/LdapASN1Constant.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/Utils.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/Utils.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/Utils.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/Utils.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,59 @@
+/* 
+ *  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 java.io.UnsupportedEncodingException;
+
+public class Utils {
+
+    private static final String CODING_CHARSET = "UTF-8"; //$NON-NLS-1$
+
+    /**
+     * Convert <code>bytes</code> to <code>String</code> using UTF-8
+     * charset. when <code>bytes</code> is <code>null</code>, empty
String
+     * will be returned.
+     * 
+     * @param bytes
+     *            bytes to be decoded into a UTF-8 string
+     * @return UTF-8 String composed of bytes, never be empty string
+     */
+    public static String getString(byte[] bytes) {
+        try {
+            return (bytes == null) ? "" : new String(bytes, CODING_CHARSET); //$NON-NLS-1$
+        } catch (UnsupportedEncodingException e) {
+            // never reached, because UTF-8 is supported by all java platform
+            return ""; //$NON-NLS-1$
+        }
+    }
+
+    /**
+     * Encodes <code>s</code> into a sequence of bytes using UTF-8 charset.
+     * 
+     * @param s
+     *            String to be encoded
+     * @return UTF-8 String
+     */
+    public static byte[] getBytes(String s) {
+        try {
+            return (s == null) ? new byte[0] : s.getBytes(CODING_CHARSET);
+        } catch (UnsupportedEncodingException e) {
+            // never reached, because UTF-8 is supported by all java platform
+            return new byte[0];
+        }
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/Utils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/AddOpTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/AddOpTest.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/AddOpTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/AddOpTest.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,41 @@
+/* 
+ *  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;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1TestUtils;
+import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
+
+public class AddOpTest extends TestCase {
+    public void test_encodeValues_$LObject() {
+        String entry = "add entry";
+        AddOp op = new AddOp(entry);
+
+        ASN1TestUtils.checkEncode(op.getRequest(), LdapASN1Constant.AddRequest);
+
+        LdapAttribute attr = new LdapAttribute("attr1");
+
+        op.addAttribute(attr);
+        ASN1TestUtils.checkEncode(op.getRequest(), LdapASN1Constant.AddRequest);
+
+        attr.add("value");
+        attr.add("test");
+        ASN1TestUtils.checkEncode(op.getRequest(), LdapASN1Constant.AddRequest);
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/AddOpTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,48 @@
+/* 
+ *  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;
+
+import javax.naming.NamingException;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.ASN1TestUtils;
+import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
+import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
+
+public class LdapAttributeTest extends TestCase {
+    public void test_encode_decode() throws NamingException {
+        String id = "test";
+        byte[] v1 = Utils.getBytes("value1");
+        byte[] v2 = Utils.getBytes("value2");
+        byte[] v3 = Utils.getBytes("value3");
+        LdapAttribute attr = new LdapAttribute(id);
+        attr.add(v1);
+        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());
+
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapAttributeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapResultTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapResultTest.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapResultTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapResultTest.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,75 @@
+/* 
+ *  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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
+import org.apache.harmony.security.asn1.ASN1Integer;
+
+public class LdapResultTest extends TestCase {
+    public void test_decodeValues_$LObject() {
+        int resultCode = 10;
+        String matchedDN = "matchedDN";
+        String errorMessage = "errorMessage";
+        String[] referrals = new String[] { "referrals0", "referrals1",
+                "referrals2" };
+        List<byte[]> list = new ArrayList<byte[]>();
+        list.add(Utils.getBytes(referrals[0]));
+        list.add(Utils.getBytes(referrals[1]));
+        list.add(Utils.getBytes(referrals[2]));
+
+        Object[] values = new Object[] { ASN1Integer.fromIntValue(resultCode),
+                Utils.getBytes(matchedDN), Utils.getBytes(errorMessage), list };
+
+        LdapResult result = new LdapResult();
+        result.decodeValues(values);
+
+        assertEquals(resultCode, result.getResultCode());
+        assertEquals(matchedDN, result.getMachedDN());
+        assertEquals(errorMessage, result.getErrorMessage());
+        assertEquals(referrals.length, result.getReferrals().length);
+
+        for (int i = 0; i < result.getReferrals().length; ++i) {
+            String referral = result.getReferrals()[i];
+            assertEquals(referrals[i], referral);
+        }
+        
+        values[3] = null;
+        result = new LdapResult();
+        result.decodeValues(values);
+        
+        assertEquals(resultCode, result.getResultCode());
+        assertEquals(matchedDN, result.getMachedDN());
+        assertEquals(errorMessage, result.getErrorMessage());
+        assertNull(result.getReferrals());
+
+        values[3] = new ArrayList<byte[]>();
+        result = new LdapResult();
+        result.decodeValues(values);
+        
+        assertEquals(resultCode, result.getResultCode());
+        assertEquals(matchedDN, result.getMachedDN());
+        assertEquals(errorMessage, result.getErrorMessage());
+        assertNull(result.getReferrals());
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapResultTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1TestUtils.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1TestUtils.java?rev=583708&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1TestUtils.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1TestUtils.java
Thu Oct 11 00:16:20 2007
@@ -0,0 +1,95 @@
+/* 
+ *  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 java.lang.reflect.Field;
+import java.util.Collection;
+
+import junit.framework.Assert;
+
+import org.apache.harmony.security.asn1.ASN1Boolean;
+import org.apache.harmony.security.asn1.ASN1Enumerated;
+import org.apache.harmony.security.asn1.ASN1Implicit;
+import org.apache.harmony.security.asn1.ASN1Integer;
+import org.apache.harmony.security.asn1.ASN1OctetString;
+import org.apache.harmony.security.asn1.ASN1SequenceOf;
+import org.apache.harmony.security.asn1.ASN1SetOf;
+import org.apache.harmony.security.asn1.ASN1Type;
+import org.apache.harmony.security.asn1.ASN1ValueCollection;
+
+public class ASN1TestUtils {
+    public static void checkEncode(Object value, ASN1Type type) {
+        if (type instanceof ASN1Implicit) {
+            type = getWrappedType((ASN1Implicit) type);
+        }
+
+        if (type instanceof ASN1Integer 
+                || type instanceof ASN1Enumerated) {
+            Assert.assertTrue(value instanceof byte[]);
+            
+            Assert.assertFalse("value should not be zero-length byte array",
+                    ((byte[]) value).length != 0);
+        } else if (type instanceof ASN1Boolean) {
+            Assert.assertTrue(value instanceof Boolean);
+        } else if (type instanceof ASN1OctetString) {
+            Assert.assertTrue(value instanceof byte[]);
+        } else if (type instanceof ASN1SequenceWrap) {
+            checkEncodeSequence(value, (ASN1SequenceWrap) type);
+        } else if (type instanceof ASN1SequenceOf 
+                || type instanceof ASN1SetOf) {
+            Assert.assertTrue(value instanceof Collection);
+            Collection collection = (Collection) value;
+            for (Object object : collection) {
+                checkEncode(object, ((ASN1ValueCollection) type).type);
+            }
+        } else {
+            Assert.fail("Not supported ASN.1 type");
+        }
+    }
+    
+    private static void checkEncodeSequence(Object value, ASN1SequenceWrap type) {
+        if (value instanceof Object[]) {
+            Object[] objs = (Object[]) value;
+            Assert.assertEquals(type.type.length, objs.length);
+            for (int i = 0; i < objs.length; i++) {
+                if (objs[i] == null && type.OPTIONAL[i]) {
+                    continue;
+                }
+                checkEncode(objs[i], type.type[i]);
+            }
+        } else if (value instanceof ASN1Encodable) {
+            Object[] objs = new Object[type.type.length];
+            ((ASN1Encodable) value).encodeValues(objs);
+            checkEncodeSequence(objs, type);
+        } else {
+            Assert
+                    .fail("Value for ASN1SequenceWrap should be Object[], "
+                            + "or org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable");
+        }
+    }
+    
+    private static ASN1Type getWrappedType(ASN1Implicit type) {
+        try {
+            Field field = ASN1Implicit.class.getDeclaredField("type");
+            field.setAccessible(true);
+            return (ASN1Type) field.get(type);
+        } catch (Exception e) {
+            // can't reach, unless implement changed
+            throw new RuntimeException("Can't get wrapped type.", e);
+        } 
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1TestUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message