Example usage for javax.naming NamingEnumeration hasMore

List of usage examples for javax.naming NamingEnumeration hasMore

Introduction

In this page you can find the example usage for javax.naming NamingEnumeration hasMore.

Prototype

public boolean hasMore() throws NamingException;

Source Link

Document

Determines whether there are any more elements in the enumeration.

Usage

From source file:org.swordess.ldap.odm.core.SessionImpl.java

public List<String> lookup(String context, String filter) {
    if (null == filter) {
        return null;
    }/*from w  w  w. ja  v a 2  s. c  om*/

    LogUtils.debug(LOG, String.format("search DNs with context=%s, filter=%s", context, filter));

    SearchControls ctrl = new SearchControls();
    ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctrl.setReturningAttributes(new String[] {});

    try {
        List<String> retVal = new ArrayList<String>();
        NamingEnumeration<SearchResult> results = ctx.search(context, filter, ctrl);
        while (results.hasMore()) {
            retVal.add(results.next().getNameInNamespace());
        }
        return retVal;
    } catch (NamingException e) {
        throw new SessionException(e.getMessage(), e);
    }
}

From source file:org.olat.ldap.LDAPLoginManagerImpl.java

/**
 * Creates list of all OLAT Users which have been deleted out of the LDAP directory but still exits in OLAT Configuration: Required Attributes = olatextconfig.xml
 * (property=reqAttrs) LDAP Base = olatextconfig.xml (property=ldapBase)
 * /*  w  ww  .  ja  v  a2 s. c  om*/
 * @param syncTime The time to search in LDAP for changes since this time. SyncTime has to formatted: JJJJMMddHHmm
 * @param ctx The LDAP system connection, if NULL or closed NamingExecpiton is thrown
 * @return Returns list of Identity from the user which have been deleted in LDAP
 * @throws NamingException
 */
public List<Identity> getIdentitysDeletedInLdap(final LdapContext ctx) {
    if (ctx == null) {
        return null;
    }
    // Find all LDAP Users
    final String userID = LDAPLoginModule.mapOlatPropertyToLdapAttribute(LDAPConstants.LDAP_USER_IDENTIFYER);
    final String objctClass = LDAPLoginModule.getLdapUserObjectClass();
    final List<String> ldapList = new ArrayList<String>();

    searchInLdap(new LdapVisitor() {
        public void visit(final SearchResult result) throws NamingException {
            final Attributes attrs = result.getAttributes();
            final NamingEnumeration<? extends Attribute> aEnum = attrs.getAll();
            while (aEnum.hasMore()) {
                final Attribute attr = aEnum.next();
                // use lowercase username
                ldapList.add(attr.get().toString().toLowerCase());
            }
        }
    }, "(objectClass=" + objctClass + ")", new String[] { userID }, ctx);

    if (ldapList.isEmpty()) {
        logWarn("No users in LDAP found, can't create deletionList!!", null);
        return null;
    }

    // Find all User in OLAT, members of LDAPSecurityGroup
    final SecurityGroup ldapGroup = securityManager.findSecurityGroupByName(LDAPConstants.SECURITY_GROUP_LDAP);
    if (ldapGroup == null) {
        logError("Error getting users from OLAT security group '" + LDAPConstants.SECURITY_GROUP_LDAP
                + "' : group does not exist", null);
        return null;
    }

    final List<Identity> identityListToDelete = new ArrayList<Identity>();
    final List<Identity> olatListIdentity = securityManager.getIdentitiesOfSecurityGroup(ldapGroup);
    for (final Identity ida : olatListIdentity) {
        // compare usernames with lowercase
        if (!ldapList.contains(ida.getName().toLowerCase())) {
            identityListToDelete.add(ida);
        }
    }
    return identityListToDelete;
}

From source file:org.olat.ldap.LDAPLoginManagerImpl.java

private boolean isPagedResultControlSupported(final LdapContext ctx) {
    try {/*from  ww  w.  j a v  a  2s  . com*/
        final SearchControls ctl = new SearchControls();
        ctl.setReturningAttributes(new String[] { "supportedControl" });
        ctl.setSearchScope(SearchControls.OBJECT_SCOPE);

        /* search for the rootDSE object */
        final NamingEnumeration<SearchResult> results = ctx.search("", "(objectClass=*)", ctl);

        while (results.hasMore()) {
            final SearchResult entry = results.next();
            final NamingEnumeration<? extends Attribute> attrs = entry.getAttributes().getAll();
            while (attrs.hasMore()) {
                final Attribute attr = attrs.next();
                final NamingEnumeration<?> vals = attr.getAll();
                while (vals.hasMore()) {
                    final String value = (String) vals.next();
                    if (value.equals(PAGED_RESULT_CONTROL_OID)) {
                        return true;
                    }
                }
            }
        }
        return false;
    } catch (final Exception e) {
        logError("Exception when trying to know if the server support paged results.", e);
        return false;
    }
}

From source file:org.olat.ldap.LDAPLoginManagerImpl.java

/**
 * Find the user dn with its uid//from   w  w w  . j av a 2  s  .c om
 * 
 * @param uid
 * @param ctx
 * @return user's dn
 */
private String searchUserDN(final String uid, final DirContext ctx) {
    if (ctx == null) {
        return null;
    }

    final List<String> ldapBases = LDAPLoginModule.getLdapBases();
    final String objctClass = LDAPLoginModule.getLdapUserObjectClass();
    final String[] serachAttr = { "dn" };

    final String ldapUserIDAttribute = LDAPLoginModule
            .mapOlatPropertyToLdapAttribute(LDAPConstants.LDAP_USER_IDENTIFYER);
    final String filter = "(&(objectClass=" + objctClass + ")(" + ldapUserIDAttribute + "=" + uid + "))";
    final SearchControls ctls = new SearchControls();
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctls.setReturningAttributes(serachAttr);

    String userDN = null;
    for (final String ldapBase : ldapBases) {
        try {
            final NamingEnumeration<SearchResult> enm = ctx.search(ldapBase, filter, ctls);
            while (enm.hasMore()) {
                final SearchResult result = enm.next();
                userDN = result.getNameInNamespace();
            }
            if (userDN != null) {
                break;
            }
        } catch (final NamingException e) {
            logError("NamingException when trying to bind user with username::" + uid + " on ldapBase::"
                    + ldapBase, e);
        }
    }

    return userDN;
}

From source file:org.swordess.ldap.odm.core.SessionImpl.java

@Override
public List<Map<String, Object>> search(String context, String filter, String[] returningAttrs) {
    if (null == filter) {
        return null;
    }/*from  ww  w  .j  a  v a  2 s  . com*/

    LogUtils.debug(LOG, String.format("search %s with filter=%s, returningAttrs=%s", context, filter,
            Arrays.toString(returningAttrs)));

    SearchControls ctrl = new SearchControls();
    ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctrl.setReturningAttributes(returningAttrs);

    try {
        List<Map<String, Object>> retVal = new ArrayList<Map<String, Object>>();
        NamingEnumeration<SearchResult> results = ctx.search(context, filter, ctrl);
        while (results.hasMore()) {
            try {
                SearchResult result = results.next();
                retVal.add(fromAttributesToMap(result.getAttributes()));
            } catch (NamingException e) {
                LogUtils.error(LOG, "Unable to construct the map", e);
            }
        }
        return retVal;
    } catch (NamingException e) {
        throw new SessionException(e.getMessage(), e);
    }
}

From source file:org.olat.ldap.LDAPLoginManagerImpl.java

private void searchInLdap(final LdapVisitor visitor, final String filter, final String[] returningAttrs,
        final LdapContext ctx) {
    final SearchControls ctls = new SearchControls();
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctls.setReturningAttributes(returningAttrs);
    ctls.setCountLimit(0); // set no limits

    final boolean paging = isPagedResultControlSupported(ctx);
    for (final String ldapBase : LDAPLoginModule.getLdapBases()) {
        int counter = 0;
        try {/* ww w.jav  a  2  s  .c o  m*/
            if (paging) {
                byte[] cookie = null;
                ctx.setRequestControls(
                        new Control[] { new PagedResultsControl(PAGE_SIZE, Control.NONCRITICAL) });
                do {
                    final NamingEnumeration<SearchResult> enm = ctx.search(ldapBase, filter, ctls);
                    while (enm.hasMore()) {
                        visitor.visit(enm.next());
                    }
                    cookie = getCookie(ctx);
                } while (cookie != null);
            } else {
                final NamingEnumeration<SearchResult> enm = ctx.search(ldapBase, filter, ctls);
                while (enm.hasMore()) {
                    visitor.visit(enm.next());
                }
                counter++;
            }
        } catch (final SizeLimitExceededException e) {
            logError("SizeLimitExceededException after " + counter
                    + " records when getting all users from LDAP, reconfigure your LDAP server, hints: http://www.ldapbrowser.com/forum/viewtopic.php?t=14",
                    null);
        } catch (final NamingException e) {
            logError("NamingException when trying to fetch deleted users from LDAP using ldapBase::" + ldapBase
                    + " on row::" + counter, e);
        } catch (final Exception e) {
            logError("Exception when trying to fetch deleted users from LDAP using ldapBase::" + ldapBase
                    + " on row::" + counter, e);
        }
    }
}

From source file:org.swordess.ldap.odm.core.SessionImpl.java

@Override
public <T> List<T> searchIndirections(Class<T> clazz, String filter) {
    if (null == filter) {
        return null;
    }/*w  w  w.jav a 2  s.c  o  m*/

    LogUtils.debug(LOG, String.format("search %s with filter=%s", clazz.getName(), filter));

    OneMetaData oneMetaData = IndirectionsMetaData.get(clazz).getOne();

    SearchControls ctrl = new SearchControls();
    ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctrl.setReturningAttributes(new String[] { oneMetaData.getIdAttr(), oneMetaData.getIndirectionAttr() });

    try {
        List<T> retVal = new ArrayList<T>();
        NamingEnumeration<SearchResult> results = ctx.search(oneMetaData.getContext(), filter, ctrl);
        while (results.hasMore()) {
            SearchResult result = results.next();
            retVal.add(fromAttributesToIndirections(clazz, result.getAttributes()));
        }
        return retVal;
    } catch (NamingException e) {
        throw new SessionException(e.getMessage(), e);
    }
}

From source file:org.olat.ldap.LDAPLoginManagerImpl.java

/**
 * Checks if LDAP properties are different then OLAT properties of a User. If they are different a Map (OlatPropertyName,LDAPValue) is returned.
 * // ww  w  .  j ava 2  s  .co m
 * @param attributes Set of LDAP Attribute of Identity
 * @param identity Identity to compare
 * @return Map(OlatPropertyName,LDAPValue) of properties Identity, where property has changed. NULL is returned it no attributes have to be synced
 */
@SuppressWarnings("unchecked")
public Map<String, String> prepareUserPropertyForSync(final Attributes attributes, final Identity identity) {
    final Map<String, String> olatPropertyMap = new HashMap<String, String>();
    final User user = identity.getUser();
    final NamingEnumeration<Attribute> neAttrs = (NamingEnumeration<Attribute>) attributes.getAll();
    try {
        while (neAttrs.hasMore()) {
            final Attribute attr = neAttrs.next();
            final String olatProperty = mapLdapAttributeToOlatProperty(attr.getID());
            if (olatProperty == null) {
                continue;
            }
            final String ldapValue = getAttributeValue(attr);
            final String olatValue = user.getProperty(olatProperty, null);
            if (olatValue == null) {
                // new property or user ID (will always be null, pseudo property)
                olatPropertyMap.put(olatProperty, ldapValue);
            } else {
                if (ldapValue.compareTo(olatValue) != 0) {
                    olatPropertyMap.put(olatProperty, ldapValue);
                }
            }
        }
        if (olatPropertyMap.size() == 1 && olatPropertyMap.get(LDAPConstants.LDAP_USER_IDENTIFYER) != null) {
            return null;
        }
        return olatPropertyMap;

    } catch (final NamingException e) {
        logError("NamingException when trying to prepare user properties for LDAP sync", e);
        return null;
    }
}

From source file:org.swordess.ldap.odm.core.SessionImpl.java

@Override
public List<Map<String, Object>> search(Class<?> clazz, String filter, String[] returningAttrs) {
    if (null == filter) {
        return null;
    }//from w ww.  j a v a 2  s.  c o m

    LogUtils.debug(LOG, String.format("search %s with filter=%s, returningAttrs=%s", clazz.getName(), filter,
            Arrays.toString(returningAttrs)));

    SearchControls ctrl = new SearchControls();
    ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctrl.setReturningAttributes(returningAttrs);

    try {
        List<Map<String, Object>> retVal = new ArrayList<Map<String, Object>>();
        NamingEnumeration<SearchResult> results = ctx.search(EntityMetaData.get(clazz).context(), filter, ctrl);
        while (results.hasMore()) {
            try {
                SearchResult result = results.next();
                retVal.add(fromAttributesToMap(clazz, result.getAttributes()));
            } catch (NamingException e) {
                LogUtils.error(LOG, "Unable to construct the map", e);
            }
        }
        return retVal;
    } catch (NamingException e) {
        throw new SessionException(e.getMessage(), e);
    }
}

From source file:org.swordess.ldap.odm.core.SessionImpl.java

@Override
public <T> List<T> search(Class<T> clazz, String filter) {
    if (null == filter) {
        return null;
    }/*from   ww  w.  j  ava  2  s . co  m*/

    LogUtils.debug(LOG, "search " + clazz.getName() + " with filter=" + filter);

    SearchControls ctrl = new SearchControls();
    ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctrl.setReturningAttributes(EntityMetaData.getDefinedAttrNames(clazz));

    List<T> retVal = new ArrayList<T>();
    try {
        NamingEnumeration<SearchResult> results = ctx.search(EntityMetaData.get(clazz).context(), filter, ctrl);
        while (results.hasMore()) {
            try {
                SearchResult result = results.next();
                T entity = null;
                if (sessionCache.containsKey(result.getNameInNamespace())) {
                    // guarantee the reference integrity for one search result
                    entity = (T) sessionCache.get(result.getNameInNamespace());
                } else {
                    entity = fromAttributesToEntity(clazz, result.getAttributes());
                    sessionCache.put(result.getNameInNamespace(), entity);
                }
                retVal.add(entity);
            } catch (NamingException e) {
                LogUtils.error(LOG, "Unable to construct the entity", e);
            }
        }
    } catch (NamingException e) {
        throw new SessionException(e.getMessage(), e);
    }
    return retVal;
}