Example usage for javax.naming.ldap LdapName toString

List of usage examples for javax.naming.ldap LdapName toString

Introduction

In this page you can find the example usage for javax.naming.ldap LdapName toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of this LDAP name in a format defined by <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a> and described in the class description.

Usage

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

/**
 * @param rootDn//from   ww  w.  j av a  2 s .c  o m
 * @param type
 * @param rdnValue
 */
public void addRdn(LdapName rootDn, RdnType type, String rdnValue) {
    DirContext ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());

        ModificationItem[] modifications = new ModificationItem[1];

        Attribute attribute = new BasicAttribute(type.toString(), rdnValue);

        modifications[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attribute);

        ctx.modifyAttributes("", modifications);

    }

    catch (NamingException e) {
        throw new RuntimeException(e);
    }

    finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                log.debug(e);
            }
        }
    }

}

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

/**
 * @param rootDn//w w  w .j a va2s  .  c  om
 * @param type
 * @param rdnValue
 */
public void removeRdn(LdapName rootDn, RdnType type, String rdnValue) {
    DirContext ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());

        ModificationItem[] modifications = new ModificationItem[1];

        Attribute attribute = new BasicAttribute(type.toString(), rdnValue);

        modifications[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attribute);

        ctx.modifyAttributes("", modifications);

    }

    catch (NamingException e) {
        throw new RuntimeException(type.toString() + "=" + rdnValue + "," + rootDn.toString(), e);
    }

    finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                log.debug(e);
            }
        }
    }

}

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

/**
 * @param parentDn/*from w w w. j  a  v a 2 s .co m*/
 * @param subContextName
 * @param attributes
 */
public void createSubContext(final LdapName parentDn, final Rdn subContextRdn, final Attributes attributes) {
    DirContext ctx = null;

    try {
        LdapName subContextName = NamingFactory.createName(subContextRdn);
        ctx = contextFactory.createContext(parentDn.toString());
        ctx.createSubcontext(subContextName.toString(), attributes);
    } catch (NamingException e) {
        throw new java.lang.RuntimeException(subContextRdn.toString() + "," + parentDn.toString(), e);
    }
}

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

/**
 * @param parentDn/*w  w  w .ja v a 2s .c o  m*/
 * @param subContextRdn
 */
public void deleteSubContext(LdapName parentDn, Rdn subContextToDelete) {
    DirContext ctx = null;

    try {
        ctx = contextFactory.createContext(parentDn.toString());
        ctx.destroySubcontext(subContextToDelete.toString());
    } catch (NamingException e) {
        throw new RuntimeException(subContextToDelete.toString() + "," + parentDn.toString(), e);
    } finally {
        if (contextFactory != null)
            contextFactory.closeContext(ctx);
    }

}

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

public void deleteSubContext(LdapName parentDn, LdapName subContextName) {
    DirContext ctx = null;//from w  w  w .ja  v  a2 s .c o m

    try {
        ctx = contextFactory.createContext(parentDn.toString());
        ctx.destroySubcontext(subContextName);
    } catch (NamingException e) {
        throw new RuntimeException(subContextName.toString() + "," + parentDn.toString(), e);
    } finally {
        contextFactory.closeContext(ctx);
    }

}

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

/**
 * @param dn//from  www  .  ja v  a2  s  .c o  m
 * @return
 */
public boolean isDnExists(LdapName dn) {

    boolean result = false;
    Context ctx = null;

    try {
        ctx = contextFactory.createContext(dn.toString());

        if (ctx != null)
            result = true;
        else
            result = false;
    } catch (NamingException e) {
        throw new RuntimeException(dn.toString(), e);
    } finally {
        if (contextFactory != null)
            contextFactory.closeContext(ctx);
    }

    return result;
}

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

/**
 * @param rootDn//from  ww w  .  j  a va  2 s . c o  m
 * @param type
 * @param value
 * @return
 */
public boolean isRdnExists(LdapName rootDn, RdnType type, String value) {
    Object result = null;
    Context ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());
        result = ctx.lookup(NamingFactory.createRdn(type, value).toString());

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

    if (result != null)
        return true;
    else
        return false;
}

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

public boolean isRdnExists(LdapName rootDn, LdapName rdnName) {
    Object result = null;/*w ww.j  a va 2 s  .c  om*/
    Context ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());
        result = ctx.lookup(rdnName);

    } catch (NamingException e) {
        throw new RuntimeException(rdnName.toString() + "," + rootDn.toString(), e);
    } finally {
        contextFactory.closeContext(ctx);
    }

    if (result != null)
        return true;
    else
        return false;
}

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

/**
 * @param rootDn//from   ww w . j a va  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/*from   ww  w .  j av  a  2  s.c  o  m*/
 * @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;
}