Example usage for javax.naming.directory DirContext search

List of usage examples for javax.naming.directory DirContext search

Introduction

In this page you can find the example usage for javax.naming.directory DirContext search.

Prototype

public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes)
        throws NamingException;

Source Link

Document

Searches in a single context for objects that contain a specified set of attributes.

Usage

From source file:edu.vt.middleware.ldap.AbstractLdap.java

/**
 * This will return the LDAP schema associated with the supplied dn. The
 * resulting <code>Iterator</code> is a deep copy of the original search
 * results. See {@link javax.naming.DirContext#getSchema(String)}.
 *
 * @param  dn  <code>String</code> named object in the LDAP
 *
 * @return  <code>Iterator</code> - LDAP search result
 *
 * @throws  NamingException  if the LDAP returns an error
 *///from   ww  w  .  j  a  va 2 s  .co m
protected Iterator<SearchResult> getSchema(final String dn) throws NamingException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Schema search with the following parameters:");
        this.logger.debug("  dn = " + dn);
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("  config = " + this.config.getEnvironment());
        }
    }

    List<SearchResult> results = null;
    LdapContext ctx = null;
    DirContext schema = null;
    NamingEnumeration<SearchResult> en = null;
    try {
        for (int i = 0; i <= this.config.getOperationRetry() || this.config.getOperationRetry() == -1; i++) {
            try {
                ctx = this.getContext();
                schema = ctx.getSchema(dn);
                en = schema.search("", null);

                results = SR_COPY_RESULT_HANDLER.process(null, en, this.config.getHandlerIgnoreExceptions());

                break;
            } catch (NamingException e) {
                this.operationRetry(ctx, e, i);
            }
        }
    } finally {
        if (schema != null) {
            schema.close();
        }
        if (en != null) {
            en.close();
        }
        if (ctx != null) {
            ctx.close();
        }
    }
    return results.iterator();
}

From source file:org.easy.ldap.LdapDao.java

/**
 * @param rootDn//from   ww  w .ja v a 2  s  . c o  m
 * @param type
 * @return
 */
public List<String> findRdnValue(LdapName rootDn, RdnType type) {
    NamingEnumeration<SearchResult> result = null;
    List<String> out = new ArrayList<String>(0);

    DirContext ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());
        Attributes attributes = new BasicAttributes();
        attributes.put(new BasicAttribute(type.toString()));

        result = ctx.search("", attributes);

        while (result.hasMore()) {
            attributes = result.next().getAttributes();
            out.add(attributes.get(type.toString()).get().toString());
        }

    } catch (NamingException e) {
        throw new RuntimeException(type.toString() + "," + rootDn.toString(), e);
    } finally {
        if (contextFactory != null)
            contextFactory.closeContext(ctx);
    }

    return out;
}

From source file:org.easy.ldap.LdapDao.java

/**
 * @param rootDn/*  w w w .j a  va 2  s  .  c  om*/
 * @param type
 * @return
 */
public List<String> findRdnValues(LdapName rootDn, Attributes attributesToMatch, RdnType returnType) {
    NamingEnumeration<SearchResult> result = null;
    List<String> out = new ArrayList<String>(0);

    DirContext ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());
        result = ctx.search("", attributesToMatch);

        Attributes attributes;

        while (result.hasMore()) {
            attributes = result.next().getAttributes();
            out.add(attributes.get(returnType.toString()).get().toString());
        }

    } catch (NamingException e) {
        throw new RuntimeException(returnType.toString() + "," + rootDn.toString(), e);
    } finally {
        if (contextFactory != null)
            contextFactory.closeContext(ctx);
    }

    return out;
}

From source file:org.easy.ldap.LdapDao.java

/**
 * @param rootDn//  w w  w . jav  a  2 s .com
 * @param attributesToMatch
 * @return
 */
public NamingEnumeration<SearchResult> findAll(LdapName rootDn, Attributes attributesToMatch) {
    NamingEnumeration<SearchResult> result = null;

    DirContext ctx = null;
    try {
        ctx = contextFactory.createContext(rootDn.toString());
        result = ctx.search("", attributesToMatch);
    } catch (NamingException e) {
        throw new RuntimeException(rootDn.toString(), e);
    } finally {
        if (contextFactory != null)
            contextFactory.closeContext(ctx);
    }

    return result;
}

From source file:org.gbif.portal.registration.LDAPUtils.java

/**
 * Checks to see if the supplied user name is in use.
 * //from   ww w  . j  a v  a  2 s . c om
 * @param userName
 * @return
 * @throws NamingException
 */
public boolean userNameInUse(String userName) throws NamingException {
    DirContext ctx = getUserContext();
    Attributes attributes = new BasicAttributes();
    try {
        NamingEnumeration searchContext = ctx.search("uid=" + userName, attributes);
    } catch (Exception e) {
        //expected behaviour
        return false;
    }
    return true;
}

From source file:org.kitodo.production.services.data.LdapServerService.java

/**
 * Check if User already exists on system.
 *
 * @param user//from w ww.j av  a 2 s.  c o  m
 *            The User.
 * @return result as boolean
 */
public boolean isUserAlreadyExists(User user) {
    Hashtable<String, String> ldapEnvironment = initializeWithLdapConnectionSettings(
            user.getLdapGroup().getLdapServer());
    DirContext ctx;
    boolean result = false;
    try {
        ctx = new InitialDirContext(ldapEnvironment);
        Attributes matchAttrs = new BasicAttributes(true);
        NamingEnumeration<SearchResult> answer = ctx.search(buildUserDN(user), matchAttrs);
        result = answer.hasMoreElements();

        while (answer.hasMore()) {
            SearchResult sr = answer.next();
            logger.debug(">>>{}", sr.getName());
            Attributes attrs = sr.getAttributes();
            String givenName = getStringForAttribute(attrs, "givenName");
            String surName = getStringForAttribute(attrs, "sn");
            String mail = getStringForAttribute(attrs, "mail");
            String cn = getStringForAttribute(attrs, "cn");
            String homeDirectory = getStringForAttribute(attrs, "homeDirectory");

            logger.debug(givenName);
            logger.debug(surName);
            logger.debug(mail);
            logger.debug(cn);
            logger.debug(homeDirectory);
        }

        ctx.close();
    } catch (NamingException e) {
        logger.error(e.getMessage(), e);
    }
    return result;
}