harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r594738 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/internal/nls/ main/java/org/apache/harmony/jndi/provider/ldap/ main/java/org/apache/harmony/jndi/provider/ldap/asn1/ test/java/org/apache/harm...
Date Wed, 14 Nov 2007 02:15:02 GMT
Author: leoli
Date: Tue Nov 13 18:15:01 2007
New Revision: 594738

URL: http://svn.apache.org/viewvc?rev=594738&view=rev
Log:
Apply patch for HARMONY-5110([classlib][jndi][ldap] implements LdapContextImpl.list, search
and getAttributes methods).

Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/DeleteOp.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/LdapMessage.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/asn1/ASN1Encodable.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties?rev=594738&r1=594737&r2=594738&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties
Tue Nov 13 18:15:01 2007
@@ -198,6 +198,7 @@
 ldap.26=Only instances of CompositeName class or LdapName class are acceptable
 ldap.28=Parameter of filter should not be null
 ldap.29=Invalid search filter
+ldap.30=Illegal value: {0} for {1} property
 ldap.2B=LDAP URL should not be null
 ldap.2C=Invalid LDAP URL
 ldap.2D=LDAP URL may only contain host, port and dn components

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/DeleteOp.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/DeleteOp.java?rev=594738&r1=594737&r2=594738&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/DeleteOp.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/DeleteOp.java
Tue Nov 13 18:15:01 2007
@@ -27,7 +27,7 @@
  * {@link http://www.rfc-editor.org/rfc/rfc2251.txt} for detailed information
  * 
  */
-public class DeleteOp implements LdapOperation, ASN1Encodable, ASN1Decodable {
+public class DeleteOp implements LdapOperation, ASN1Encodable {
     /**
      * The distinguished name of the target entry
      */
@@ -61,10 +61,6 @@
 
     public LdapResult getResult() {
         return result;
-    }
-
-    public void decodeValues(Object[] values) {
-        dn = Utils.getString((byte[]) values[0]);
     }
 
     public String getDn() {

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=594738&r1=594737&r2=594738&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 Nov 13 18:15:01 2007
@@ -20,7 +20,9 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import javax.naming.Binding;
 import javax.naming.CannotProceedException;
@@ -40,6 +42,7 @@
 import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttributes;
 import javax.naming.directory.DirContext;
+import javax.naming.directory.InvalidSearchFilterException;
 import javax.naming.directory.ModificationItem;
 import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
@@ -56,7 +59,10 @@
 
 import org.apache.harmony.jndi.internal.Util;
 import org.apache.harmony.jndi.internal.nls.Messages;
+import org.apache.harmony.jndi.internal.parser.AttributeTypeAndValuePair;
 import org.apache.harmony.jndi.internal.parser.LdapNameParser;
+import org.apache.harmony.jndi.provider.ldap.parser.FilterParser;
+import org.apache.harmony.jndi.provider.ldap.parser.ParseException;
 
 /**
  * This context implements LdapContext, it's main entry point of all JNDI ldap
@@ -93,6 +99,10 @@
 
     private static final String LDAP_DELETE_RDN = "java.naming.ldap.deleteRDN"; //$NON-NLS-1$
 
+    private static final String LDAP_DEREF_ALIASES = "java.naming.ldap.derefAliases"; //$NON-NLS-1$
+
+    private static final String LDAP_TYPES_ONLY = "java.naming.ldap.typesOnly"; //$NON-NLS-1$
+
     /**
      * construct a new inherit <code>LdapContextImpl</code>
      * 
@@ -339,8 +349,55 @@
 
     public Attributes getAttributes(Name name, String[] as)
             throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+        checkName(name);
+
+        if (name instanceof CompositeName && name.size() > 1) {
+            if (!(name.getPrefix(0) instanceof LdapName)) {
+                throw new InvalidNameException(Messages.getString("ldap.26")); //$NON-NLS-1$
+            }
+            /*
+             * multi ns, find next ns context, delegate operation to the next
+             * context
+             */
+            DirContext nns = (DirContext) findNnsContext(name);
+            Name remainingName = name.getSuffix(1);
+            return nns.getAttributes(remainingName, as);
+        }
+
+        /*
+         * there is only one ldap ns
+         */
+        // absolute dn name to list
+        String targetDN = getTargetDN(name, contextDn);
+
+        // construct one-level search using filter "(objectclass=*)"
+        SearchControls controls = new SearchControls();
+        controls.setSearchScope(SearchControls.OBJECT_SCOPE);
+
+        /*
+         * none should be retrieved, send OID "1.1" to server, according to RFC
+         * 2251, 4.5.1
+         */
+        if (as != null && as.length == 0) {
+            // "1.1" means no attributes should return
+            as = new String[] { "1.1" }; //$NON-NLS-1$
+        }
+        controls.setReturningAttributes(as);
+
+        Filter filter = new Filter(Filter.PRESENT_FILTER);
+        filter.setValue("objectClass");
+
+        LdapSearchResult result = doSearch(targetDN, filter, controls);
+        Iterator<Attributes> it = result.getEntries().values().iterator();
+        if (it.hasNext()) {
+            // FIXME: there must be only one Attributes?
+            return it.next();
+        } else if (result.getException() != null) {
+            throw result.getException();
+        }
+
+        // no attribute retrieved from server, return a empty Attributes
+        return new BasicAttributes();
     }
 
     public Attributes getAttributes(String s) throws NamingException {
@@ -439,7 +496,7 @@
                 break;
             default:
                 throw new IllegalArgumentException(Messages.getString(
-                        "jndi.14", item.getModificationOp()));
+                        "jndi.14", item.getModificationOp())); //$NON-NLS-1$
             }
         }
 
@@ -474,15 +531,127 @@
 
     public NamingEnumeration<SearchResult> search(Name name,
             Attributes attributes, String[] as) throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+        checkName(name);
+
+        if (name instanceof CompositeName && name.size() > 1) {
+            /*
+             * multi ns, find next ns context, delegate operation to the next
+             * context
+             */
+            DirContext nns = (DirContext) findNnsContext(name);
+            Name remainingName = name.getSuffix(1);
+            return nns.search(remainingName, attributes, as);
+        }
+
+        /*
+         * there is only one ldap ns
+         */
+        // get absolute dn name
+        String targetDN = getTargetDN(name, contextDn);
+        Filter filter = null;
+
+        // construct filter
+        if (attributes == null || attributes.size() == 0) {
+            filter = new Filter(Filter.PRESENT_FILTER);
+            filter.setValue("objectClass");
+        } else {
+            NamingEnumeration<? extends Attribute> attrs = attributes.getAll();
+            filter = new Filter(Filter.AND_FILTER);
+            while (attrs.hasMore()) {
+                Attribute attr = attrs.next();
+                String type = attr.getID();
+                NamingEnumeration<?> enuValues = attr.getAll();
+                while (enuValues.hasMore()) {
+                    Object value = enuValues.next();
+                    Filter child = new Filter(Filter.EQUALITY_MATCH_FILTER);
+                    child.setValue(new AttributeTypeAndValuePair(type, value));
+                    filter.addChild(child);
+                }
+            }
+        }
+
+        SearchControls controls = new SearchControls();
+        controls.setReturningAttributes(as);
+        LdapSearchResult result = doSearch(targetDN, filter, controls);
+
+        List<SearchResult> list = new ArrayList<SearchResult>();
+        Map<String, Attributes> entries = result.getEntries();
+        Name tempName = new LdapName(contextDn.toString());
+        tempName.addAll(name);
+        String baseDN = tempName.toString();
+        for (String dn : entries.keySet()) {
+            String relativeName = convertToRelativeName(dn, baseDN);
+            SearchResult sr = new SearchResult(relativeName, null, entries
+                    .get(dn));
+            sr.setNameInNamespace(dn);
+            list.add(sr);
+        }
+
+        return new LdapNamingEnumeration<SearchResult>(list, result
+                .getException());
     }
 
     public NamingEnumeration<SearchResult> search(Name name, String filter,
             Object[] objs, SearchControls searchControls)
             throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+        checkName(name);
+
+        if (name instanceof CompositeName && name.size() > 1) {
+            /*
+             * multi ns, find next ns context, delegate operation to the next
+             * context
+             */
+            DirContext nns = (DirContext) findNnsContext(name);
+            Name remainingName = name.getSuffix(1);
+            return nns.search(remainingName, filter, objs, searchControls);
+        }
+
+        /*
+         * there is only one ldap ns
+         */
+        if (filter == null) {
+            throw new NullPointerException(Messages.getString("ldap.28")); //$NON-NLS-1$
+        }
+
+        if (objs == null) {
+            objs = new Object[0];
+        }
+
+        if (searchControls == null) {
+            searchControls = new SearchControls();
+        }
+
+        // get absolute dn name
+        String targetDN = getTargetDN(name, contextDn);
+        FilterParser filterParser = new FilterParser(filter);
+        filterParser.setArgs(objs);
+        Filter f = null;
+        try {
+            f = filterParser.parse();
+        } catch (ParseException e) {
+            InvalidSearchFilterException ex = new InvalidSearchFilterException(
+                    Messages.getString("ldap.29")); //$NON-NLS-1$
+            ex.setRootCause(e);
+            throw ex;
+        }
+
+        LdapSearchResult result = doSearch(targetDN, f, searchControls);
+
+        List<SearchResult> list = new ArrayList<SearchResult>();
+        Map<String, Attributes> entries = result.getEntries();
+        Name tempName = new LdapName(contextDn.toString());
+        tempName.addAll(name);
+        String baseDN = tempName.toString();
+        for (String dn : entries.keySet()) {
+            String relativeName = convertToRelativeName(dn, baseDN);
+            SearchResult sr = new SearchResult(relativeName, null, entries
+                    .get(dn));
+            sr.setNameInNamespace(dn);
+            list.add(sr);
+        }
+
+        return new LdapNamingEnumeration<SearchResult>(list, result
+                .getException());
     }
 
     public NamingEnumeration<SearchResult> search(Name name, String filter,
@@ -512,6 +681,91 @@
         return search(convertFromStringToName(name), filter, searchControls);
     }
 
+    LdapSearchResult doSearch(SearchOp op) throws NamingException {
+        if (env.get(LDAP_DEREF_ALIASES) != null) {
+            String derefAliases = (String) env.get(LDAP_DEREF_ALIASES);
+            if (derefAliases.equals("always")) {
+                op.setDerefAliases(0);
+            } else if (derefAliases.equals("never")) {
+                op.setDerefAliases(1);
+            } else if (derefAliases.equals("finding")) {
+                op.setDerefAliases(2);
+            } else if (derefAliases.equals("searching")) {
+                op.setDerefAliases(3);
+            } else {
+                throw new IllegalArgumentException(Messages.getString(
+                        "ldap.30", new Object[] { env.get(LDAP_DEREF_ALIASES), //$NON-NLS-1$
+                                LDAP_DEREF_ALIASES }));
+            }
+
+        }
+
+        if (env.containsKey(LDAP_TYPES_ONLY)) {
+            String typesOnly = (String) env.get(LDAP_TYPES_ONLY);
+            if ("true".equals(typesOnly)) {
+                op.setTypesOnly(true);
+            } else if ("false".equals(typesOnly)) {
+                op.setTypesOnly(false);
+            } else {
+                throw new IllegalArgumentException(Messages.getString(
+                        "ldap.30", new Object[] { env.get(LDAP_TYPES_ONLY), //$NON-NLS-1$
+                                LDAP_TYPES_ONLY }));
+            }
+        }
+
+        LdapMessage message = null;
+        try {
+            message = client.doOperation(op, requestControls);
+        } catch (IOException e) {
+            CommunicationException ex = new CommunicationException(e
+                    .getMessage());
+            ex.setRootCause(e);
+            if (op.getSearchResult() == null || op.getSearchResult().isEmpty()) {
+                throw ex;
+            }
+            op.getSearchResult().setException(ex);
+            // occurs errors, just return, doesn't deal with referral and
+            // controls
+            return op.getSearchResult();
+        }
+
+        // TODO: assume response controls returned in last message, it may
+        // be not correct
+        Control[] rawControls = message.getControls();
+        responseControls = narrowingControls(rawControls);
+
+        LdapResult result = op.getResult();
+
+        op.getSearchResult().setException(
+                Util.getExceptionFromErrorCode(result.getResultCode()));
+
+        // has error, not deal with referrals
+        if (op.getSearchResult().getException() != null) {
+            return op.getSearchResult();
+        }
+
+        // baseObject is not located at the server
+        if (result.getResultCode() == 10) {
+            // TODO deal with referrals
+            throw new NotYetImplementedException();
+        }
+
+        // there are SearchResultReference in search result
+        if (op.getSearchResult().getRefURLs() != null
+                && op.getSearchResult().getRefURLs().size() != 0) {
+            // TODO deal with referrals
+            throw new NotYetImplementedException();
+        }
+
+        return op.getSearchResult();
+    }
+
+    LdapSearchResult doSearch(String dn, Filter filter, SearchControls controls)
+            throws NamingException {
+        SearchOp op = new SearchOp(dn, controls, filter);
+        return doSearch(op);
+    }
+
     public Object addToEnvironment(String s, Object o) throws NamingException {
         // TODO not yet implemented
         throw new NotYetImplementedException();
@@ -659,13 +913,82 @@
 
     public NamingEnumeration<NameClassPair> list(Name name)
             throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+        checkName(name);
+
+        if (name instanceof CompositeName && name.size() > 1) {
+            if (!(name.getPrefix(0) instanceof LdapName)) {
+                throw new InvalidNameException(Messages.getString("ldap.26")); //$NON-NLS-1$
+            }
+            /*
+             * multi ns, find next ns context, delegate operation to the next
+             * context
+             */
+            Context nns = findNnsContext(name);
+            Name remainingName = name.getSuffix(1);
+            return nns.list(remainingName);
+        }
+
+        /*
+         * there is only one ldap ns
+         */
+        // absolute dn name to list
+        String targetDN = getTargetDN(name, contextDn);
+
+        // construct one-level search using filter "(objectclass=*)"
+        SearchControls controls = new SearchControls();
+        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+        Filter filter = new Filter(Filter.PRESENT_FILTER);
+        filter.setValue("objectClass");
+
+        LdapSearchResult result = doSearch(targetDN, filter, controls);
+
+        List<NameClassPair> list = new ArrayList<NameClassPair>();
+        Map<String, Attributes> entries = result.getEntries();
+        Name tempName = new LdapName(contextDn.toString());
+        tempName.addAll(name);
+        String baseDN = tempName.toString();
+        for (String dn : entries.keySet()) {
+            String relativeName = convertToRelativeName(dn, baseDN);
+            Attributes attrs = entries.get(dn);
+            Attribute attrClass = attrs.get("javaClassName");
+            String className = null;
+            if (attrClass != null) {
+                className = (String) attrClass.get(0);
+            } else {
+                className = DirContext.class.getName();
+            }
+            NameClassPair pair = new NameClassPair(relativeName, className,
+                    true);
+            pair.setNameInNamespace(dn);
+            list.add(pair);
+        }
+
+        return new LdapNamingEnumeration<NameClassPair>(list, result
+                .getException());
     }
 
-    public NamingEnumeration<NameClassPair> list(String s)
-            throws NamingException {
-        return list(convertFromStringToName(s));
+    /**
+     * 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>
+     */
+    private 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);
     }
 
     private String getTargetDN(Name name, Name prefix) throws NamingException,
@@ -736,6 +1059,11 @@
         cpe.setResolvedObj(ref);
 
         return DirectoryManager.getContinuationDirContext(cpe);
+    }
+
+    public NamingEnumeration<NameClassPair> list(String s)
+            throws NamingException {
+        return list(convertFromStringToName(s));
     }
 
     public NamingEnumeration<Binding> listBindings(Name name)

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java?rev=594738&r1=594737&r2=594738&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java
Tue Nov 13 18:15:01 2007
@@ -36,33 +36,34 @@
  * 
  */
 public class LdapMessage implements ASN1Encodable, ASN1Decodable {
-    
+
     /**
      * operation request which could be encoded using ASN.1 BER
      */
     private ASN1Encodable requestOp;
-    
+
     /**
      * operation response operation which could be decoded using ASN.1 BER
      */
     private ASN1Decodable responseOp;
-    
+
     /**
      * controls for this message
      */
     private Control[] controls;
-    
+
     /**
      * index of the operation, determine which operation is encapsulated in this
      * message.
      */
     private int opIndex;
-    
+
+
     /**
      * unique request id for each session
      */
     private int messageId;
-    
+
     private static int nextMessageId = 1;
 
     /**
@@ -73,6 +74,7 @@
     public static synchronized int getNextMessageId() {
         return nextMessageId++;
     }
+
     /**
      * Get message id of this message
      * 
@@ -100,7 +102,7 @@
         this.controls = controls;
         messageId = getNextMessageId();
     }
-    
+
     /**
      * Construct a response message. <code>op</code> indicate which operation
      * to be used, and the message would be initialized after calling
@@ -115,7 +117,7 @@
         opIndex = -1;
         messageId = -1;
     }
-    
+
     /**
      * Encode this message using ASN.1 Basic Encoding Rules (BER)
      * 
@@ -124,7 +126,7 @@
     public byte[] encode() {
         return LdapASN1Constant.LDAPMessage.encode(this);
     }
-    
+
     /**
      * Decode values from <code>InputStream</code> using ASN.1 BER, and the
      * decoded values will initialize this <code>LdapMessage</code> instance.
@@ -138,7 +140,7 @@
         Object[] values = (Object[]) LdapASN1Constant.LDAPMessage.decode(in);
         decodeValues(values);
     }
-    
+
     /**
      * Return controls of the message, if there is no control, <code>null</code>
      * will be returned.
@@ -148,13 +150,14 @@
     public Control[] getControls() {
         return controls;
     }
-    
+
     @SuppressWarnings("unchecked")
     public void decodeValues(Object[] values) {
         messageId = ASN1Integer.toIntValue(values[0]);
         if (values[1] == null) {
             return;
         }
+
         ChosenValue chosen = (ChosenValue) values[1];
         opIndex = chosen.getIndex();
         if (opIndex == LdapASN1Constant.OP_SEARCH_RESULT_DONE
@@ -194,11 +197,6 @@
         }
     }
 
-    /**
-     * Get message id of this message
-     * 
-     * @return id of this message
-     */
     /**
      * Get index of the operation, determine which operation is encapsulated in
      * this message. If this <code>LdapMessage</code> instance is not initial,

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=594738&r1=594737&r2=594738&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 Nov 13 18:15:01 2007
@@ -19,17 +19,14 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttributes;
-import javax.naming.directory.SearchResult;
 
-import org.apache.harmony.jndi.provider.dns.BasicNamingEnumerator;
 import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
 import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
 import org.apache.harmony.jndi.provider.ldap.asn1.ASN1ChoiceWrap.ChosenValue;
@@ -39,7 +36,7 @@
     /**
      * all search result entries
      */
-    private Map<String, SearchResult> entries = new HashMap<String, SearchResult>();
+    private Map<String, Attributes> entries = new HashMap<String, Attributes>();
 
     /**
      * SearchResultReference from server
@@ -49,6 +46,8 @@
 
     private LdapResult result;
 
+    private NamingException ex;
+
     public void decodeSearchResponse(Object[] values) {
         ChosenValue chosen = (ChosenValue) values[0];
         switch (chosen.getIndex()) {
@@ -79,13 +78,14 @@
     private void decodeEntry(Object value) {
         Object[] values = (Object[]) value;
         String name = Utils.getString((byte[]) values[0]);
+
         Attributes attrs = null;
 
         if (entries.containsKey(name)) {
-            attrs = entries.get(name).getAttributes();
+            attrs = entries.get(name);
         } else {
             attrs = new BasicAttributes();
-            entries.put(name, new SearchResult(name, new Object(), attrs));
+            entries.put(name, attrs);
         }
 
         Collection<Object[]> list = (Collection<Object[]>) values[1];
@@ -96,31 +96,8 @@
         }
     }
 
-    public NamingEnumeration<SearchResult> getEnumeration() {
-        //TODO: this is simple implementation, need to be completed
-        return new BasicNamingEnumerator<SearchResult>(
-                new Enumeration<SearchResult>() {
-                    private ArrayList<SearchResult> values = new ArrayList<SearchResult>(
-                            entries.values());
-
-                    private int index = -1;
-
-                    public boolean hasMoreElements() {
-                        if (index == -1) {
-                            index = 0;
-                        }
-                        
-                        if (index + 1 <= values.size()) {
-                            return true;
-                        }
-
-                        return false;
-                    }
-
-                    public SearchResult nextElement() {
-                        return values.get(index++);
-                    }
-                });
+    public Map<String, Attributes> getEntries() {
+        return entries;
     }
 
     public List<String> getRefURLs() {
@@ -129,5 +106,21 @@
 
     public LdapResult getResult() {
         return result;
+    }
+
+    public NamingException getException() {
+        return ex;
+    }
+
+    public void setException(NamingException ex) {
+        this.ex = ex;
+    }
+
+    public boolean isEmpty() {
+        return entries.size() == 0 && refURLs.size() == 0;
+    }
+
+    public void setRefURLs(List<String> refURLs) {
+        this.refURLs = refURLs;
     }
 }

Modified: 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=594738&r1=594737&r2=594738&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Encodable.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/asn1/ASN1Encodable.java
Tue Nov 13 18:15:01 2007
@@ -31,8 +31,8 @@
  * 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
+ * SEQUENCE OF          Collection
+ * SET OF               Collection
  * CHOICE               Object[] or ChosenValue
  * </code>
  * 

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java?rev=594738&r1=594737&r2=594738&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
Tue Nov 13 18:15:01 2007
@@ -24,16 +24,21 @@
 
 import javax.naming.CompositeName;
 import javax.naming.Context;
+import javax.naming.InvalidNameException;
 import javax.naming.Name;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttribute;
 import javax.naming.directory.BasicAttributes;
 import javax.naming.directory.DirContext;
+import javax.naming.directory.InvalidSearchFilterException;
+import javax.naming.directory.SearchControls;
 import javax.naming.ldap.BasicControl;
 import javax.naming.ldap.Control;
 import javax.naming.ldap.LdapName;
 
+import org.apache.harmony.jndi.internal.parser.AttributeTypeAndValuePair;
+
 import junit.framework.TestCase;
 
 public class LdapContextImplTest extends TestCase {
@@ -77,6 +82,32 @@
         assertEquals("bin/cn=ok/usr", result.toString());
     }
 
+    public void test_list_LName() throws Exception {
+        MockLdapClient client = new MockLdapClient();
+        context = new LdapContextImpl(client, new Hashtable<Object, Object>(),
+                "");
+
+        Name name = new LdapName("cn=test,o=harmony");
+        context.list(name);
+
+        SearchOp op = (SearchOp) client.getRequest();
+        assertEquals(name.toString(), op.getBaseObject());
+        assertFalse(op.isTypesOnly());
+        SearchControls controls = op.getControls();
+        assertEquals(SearchControls.ONELEVEL_SCOPE, controls.getSearchScope());
+        Filter filter = op.getFilter();
+        assertEquals(Filter.PRESENT_FILTER, filter.getType());
+        assertEquals("objectClass", (String) filter.getValue());
+
+        name = new CompositeName("usr/bin");
+        try {
+            context.list(name);
+            fail("should throws InvalidNameException");
+        } catch (InvalidNameException e) {
+            // expected
+        }
+    }
+
     public void test_createSubcontext_LName() throws Exception {
         MockLdapClient client = new MockLdapClient();
         context = new LdapContextImpl(client, new Hashtable<Object, Object>(),
@@ -161,6 +192,34 @@
         assertEquals("hello", attr.get(0));
     }
 
+    public void test_getAttributes() throws Exception {
+        MockLdapClient client = new MockLdapClient();
+        context = new LdapContextImpl(client, new Hashtable<Object, Object>(),
+                "cn=test");
+
+        Attributes attrs = context.getAttributes("o=apache");
+        SearchOp op = (SearchOp) client.getRequest();
+        assertEquals("o=apache,cn=test", op.getBaseObject());
+        SearchControls controls = op.getControls();
+        assertEquals(SearchControls.OBJECT_SCOPE, controls.getSearchScope());
+        assertNull(controls.getReturningAttributes());
+
+        Filter filter = op.getFilter();
+        assertEquals(Filter.PRESENT_FILTER, filter.getType());
+        assertEquals("objectClass", filter.getValue());
+
+        attrs = context.getAttributes("o=apache", new String[0]);
+        op = (SearchOp) client.getRequest();
+        assertEquals("o=apache,cn=test", op.getBaseObject());
+        controls = op.getControls();
+        assertEquals(SearchControls.OBJECT_SCOPE, controls.getSearchScope());
+        assertEquals(1, controls.getReturningAttributes().length);
+        assertEquals("1.1", controls.getReturningAttributes()[0]);
+        filter = op.getFilter();
+        assertEquals(Filter.PRESENT_FILTER, filter.getType());
+        assertEquals("objectClass", filter.getValue());
+    }
+
     public void test_modifyAttributes() throws Exception {
         MockLdapClient client = new MockLdapClient();
         context = new LdapContextImpl(client, new Hashtable<Object, Object>(),
@@ -295,6 +354,135 @@
         context.setRequestControls(controls);
         actual = context.getRequestControls();
         assertEquals(controls.length + 1, actual.length);
+    }
+
+    public void test_search_LAttribute() throws Exception {
+        MockLdapClient client = new MockLdapClient();
+        context = new LdapContextImpl(client, new Hashtable<Object, Object>(),
+                "cn=test");
+
+        Attributes attrs = new BasicAttributes();
+        attrs.put("cn", "hello");
+        Attribute attr = new BasicAttribute("o");
+        attr.add("harmony");
+        attr.add("apache");
+        attrs.put(attr);
+
+        context.search("cn=hello", attrs);
+
+        SearchOp op = (SearchOp) client.getRequest();
+        assertEquals("cn=hello,cn=test", op.getBaseObject());
+        SearchControls controls = op.getControls();
+        assertEquals(SearchControls.ONELEVEL_SCOPE, controls.getSearchScope());
+        assertEquals(0, controls.getCountLimit());
+        assertEquals(false, controls.getDerefLinkFlag());
+        assertEquals(false, controls.getReturningObjFlag());
+        assertEquals(null, controls.getReturningAttributes());
+
+        Filter filter = op.getFilter();
+        assertEquals(Filter.AND_FILTER, filter.getType());
+        List<Filter> children = filter.getChildren();
+
+        for (Filter f : children) {
+            assertEquals(Filter.EQUALITY_MATCH_FILTER, f.getType());
+            AttributeTypeAndValuePair pair = (AttributeTypeAndValuePair) f
+                    .getValue();
+            if (pair.getType().equals("cn")) {
+                assertEquals("hello", pair.getValue());
+            } else {
+                assertEquals("o", pair.getType());
+                assertTrue(pair.getValue().equals("harmony")
+                        || pair.getValue().equals("apache"));
+            }
+        }
+
+        String[] rtAttributes = new String[] { "cn", "o" };
+        context.search("cn=hello", attrs, rtAttributes);
+        op = (SearchOp) client.getRequest();
+        assertEquals("cn=hello,cn=test", op.getBaseObject());
+        controls = op.getControls();
+        String[] actual = controls.getReturningAttributes();
+
+        for (String attribute : rtAttributes) {
+            boolean found = false;
+            for (String s : actual) {
+                if (s.equals(attribute)) {
+                    found = true;
+                    break;
+                }
+            }
+            if (!found) {
+                fail("Attribute " + attribute + " not found.");
+            }
+        }
+    }
+
+    public void test_search_with_filter() throws Exception {
+        MockLdapClient client = new MockLdapClient();
+        context = new LdapContextImpl(client, new Hashtable<Object, Object>(),
+                "cn=test");
+        SearchControls controls = new SearchControls();
+        controls.setCountLimit(100);
+        controls.setDerefLinkFlag(true);
+        controls.setReturningObjFlag(true);
+        controls.setTimeLimit(5);
+
+        context.search("test=search", "(objectClass=*)", controls);
+
+        SearchOp op = (SearchOp) client.getRequest();
+        assertEquals("test=search,cn=test", op.getBaseObject());
+        Filter filter = op.getFilter();
+        assertEquals(Filter.PRESENT_FILTER, filter.getType());
+        assertEquals("objectClass", filter.getValue());
+        assertEquals(controls.getCountLimit(), op.getControls().getCountLimit());
+        assertEquals(controls.getDerefLinkFlag(), op.getControls()
+                .getDerefLinkFlag());
+        assertEquals(controls.getReturningObjFlag(), op.getControls()
+                .getReturningObjFlag());
+        assertEquals(controls.getSearchScope(), op.getControls()
+                .getSearchScope());
+        assertEquals(controls.getTimeLimit(), op.getControls().getTimeLimit());
+
+        try {
+            context.search("test=search", "objectClass=*", controls);
+            fail("Should throw InvalidSearchFilterException");
+        } catch (InvalidSearchFilterException e) {
+            // expected
+        }
+
+        context.search("test=search", "(objectClass=*)", null);
+        op = (SearchOp) client.getRequest();
+        assertEquals(0, op.getControls().getCountLimit());
+        assertEquals(false, op.getControls().getDerefLinkFlag());
+        assertEquals(false, op.getControls().getReturningObjFlag());
+        assertEquals(SearchControls.ONELEVEL_SCOPE, op.getControls()
+                .getSearchScope());
+        assertEquals(0, op.getControls().getTimeLimit());
+        assertNull(controls.getReturningAttributes());
+    }
+
+    public void test_search_invalid_properties() throws Exception {
+        MockLdapClient client = new MockLdapClient();
+
+        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+        env.put("java.naming.ldap.derefAliases", "error");
+        context = new LdapContextImpl(client, env, "cn=test");
+        try {
+            context.search("", null);
+            fail("Should throws IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        env = new Hashtable<Object, Object>();
+        env.put("java.naming.ldap.typesOnly", "error");
+        context = new LdapContextImpl(client, env, "cn=test");
+        try {
+            context.getAttributes("", null);
+            fail("Should throws IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
     }
 
 }

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java?rev=594738&r1=594737&r2=594738&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/MockLdapClient.java
Tue Nov 13 18:15:01 2007
@@ -23,6 +23,7 @@
 
 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;
 import org.apache.harmony.security.asn1.ASN1Integer;
 
@@ -47,6 +48,28 @@
                     Utils.getBytes(""), Utils.getBytes(""), null };
             result.decodeValues(values);
         }
+
+        if (response instanceof SearchOp) {
+            SearchOp op = (SearchOp) response;
+            op.setSearchResult(new LdapSearchResult() {
+                public LdapResult getResult() {
+                    LdapResult rs = new LdapResult();
+                    Object[] values = new Object[] { ASN1Integer.fromIntValue(0),
+                            Utils.getBytes(""), Utils.getBytes(""), null };
+                    rs.decodeValues(values);
+                    return rs;
+                }
+            });
+        }
+        
+        if (opIndex == LdapASN1Constant.OP_BIND_REQUEST) {
+            Object[] values = new Object[5];
+            values[0] = ASN1Integer.fromIntValue(0);
+            values[1] = Utils.getBytes("");
+            values[2] = Utils.getBytes("");
+            responseOp.decodeValues(values);
+        }
+
         return new LdapMessage(response);
     }
 



Mime
View raw message