Code example for SearchControls

Methods: setCountLimitsetSearchScopesetTimeLimit

0
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, ldapURL);
        env.put(Context.SECURITY_AUTHENTICATION, "none");
        try { 
            ctx = new InitialDirContext(env);
            SearchControls controls = new SearchControls();
            // ldap search timeout 
            controls.setTimeLimit(1000); //TODO maybe properties...
            // ldap search count 
            controls.setCountLimit(2);  //TODO maybe properties...
            // ldap search scope 
            String sub = UtilXml.childElementValue(rootElement, "Scope", "sub").toLowerCase().trim();
            if (sub.equals("sub")) {
                controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            } else if (sub.equals("one")) {
                controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
            } else { 
                controls.setSearchScope(SearchControls.OBJECT_SCOPE);
            } 
            String filter = UtilXml.childElementValue(rootElement, "Filter", "(objectclass=*)");
            String attribute = UtilXml.childElementValue(rootElement, "Attribute", "uid=%u");
            attribute = LdapUtils.getFilterWithValues(attribute, username);
            NamingEnumeration<SearchResult> answer = ctx.search(baseDN,
                    // Filter expression 
                    "(&(" + filter + ") (" + attribute +"))",
                    controls);
            if (answer.hasMoreElements()) {
                result = answer.next();
Experience pair programming with AI  Get Codota for Java