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:LdapNametoString.java

public static void main(String args[]) {
    String name = "cn=JuicyFruit, ou=Fruits";
    try {/*from  w w  w .j  a  v  a 2 s .  com*/
        LdapName dn = new LdapName(name);
        String str = dn.toString();
        System.out.println(str);
        LdapName dn2 = new LdapName(str);
        System.out.println(dn.equals(dn2));
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

From source file:com.evolveum.midpoint.prism.match.DistinguishedNameMatchingRule.java

@Override
public String normalize(String original) throws SchemaException {
    if (StringUtils.isBlank(original)) {
        return null;
    }/*w  w w  . j a va  2 s .  com*/
    LdapName dn;
    try {
        dn = new LdapName(original);
    } catch (InvalidNameException e) {
        throw new SchemaException("String '" + original + "' is not a DN: " + e.getMessage(), e);
    }
    return StringUtils.lowerCase(dn.toString());
}

From source file:edu.kit.scc.ldap.LdapPosixUserDao.java

/**
 * Deletes a POSIX user from the LDAP directory.
 * //  w  w  w  . j a  va2  s. c  om
 * @param posixUser the {@link PosixUser} to delete
 * @return true if success
 */
public boolean deleteUser(PosixUser posixUser) {
    LdapName userDn = LdapUtils.emptyLdapName();
    try {
        userDn = new LdapName(userBase);
        userDn.add("uid=" + posixUser.getUid());
        log.debug("Delete {}", userDn.toString());
        ldapTemplate.unbind(userDn);

        return true;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    }
    return false;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Deletes the {@link PosixGroup} from the LDAP directory.
 * /*  ww w .  j  a va2 s.  c o m*/
 * @param group the {@link PosixGroup} to delete
 * @return true if success
 */
public boolean deleteGroup(PosixGroup group) {
    LdapName groupDn = LdapUtils.emptyLdapName();
    try {
        groupDn = new LdapName(groupBase);
        groupDn.add("cn=" + group.getCommonName());
        log.debug("Delete {}", groupDn.toString());
        ldapTemplate.unbind(groupDn);

        return true;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    }
    return false;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Updates a POSIX group in the LDAP directory.
 * /*from   www . ja v a 2 s . c  o m*/
 * @param group the {@link PosixGroup} to update
 * @return the {@link PosixGroup} updated
 */
public PosixGroup updateGroup(PosixGroup group) {
    BasicAttribute posixGroupBasicAttribute = new BasicAttribute("objectclass");
    posixGroupBasicAttribute.add("posixGroup");

    Attributes posixGroupAttributes = new BasicAttributes();
    posixGroupAttributes.put(posixGroupBasicAttribute);

    if (group.getCommonName() != null) {
        posixGroupAttributes.put("cn", group.getCommonName());
    }
    if (group.getGidNumber() != null) {
        posixGroupAttributes.put("gidNumber", String.valueOf(group.getGidNumber()));
    }
    if (group.getUserPassword() != null) {
        posixGroupAttributes.put("userPassword", group.getUserPassword());
    }
    if (group.getDescription() != null) {
        posixGroupAttributes.put("description", group.getDescription());
    }
    LdapName groupDn = LdapUtils.emptyLdapName();
    try {
        groupDn = new LdapName(groupBase);
        groupDn.add("cn=" + group.getCommonName());
        log.debug("Update {}", groupDn.toString());
        ldapTemplate.bind(groupDn, null, posixGroupAttributes);

        return group;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return null;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Adds a POSIX user to the specified POSIX group.
 * //  w  w w  .java  2 s . co m
 * @param group the POSIX group
 * @param memberUid the POSIX user's uid
 * @return true on success
 */
public boolean addMember(PosixGroup group, String memberUid) {
    ModificationItem[] modificationItems = new ModificationItem[] {
            new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("memberUid", memberUid)) };
    LdapName groupDn = LdapUtils.emptyLdapName();
    try {
        groupDn = new LdapName(groupBase);
        groupDn.add("cn=" + group.getCommonName());
        log.debug("Add member {} to {}", memberUid, groupDn.toString());
        ldapTemplate.modifyAttributes(groupDn, modificationItems);
        return true;
    } catch (AttributeInUseException ex) {
        log.error("ERROR {}", ex.toString());
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return false;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Removes a POSIX user from the specified POSIX group.
 * //from   w  w w .  j a v a 2  s .com
 * @param group the POSIX group
 * @param memberUid the POSIX user's uid
 * @return true on success
 */
public boolean removeMember(PosixGroup group, String memberUid) {
    ModificationItem[] modificationItems = new ModificationItem[] {
            new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("memberUid", memberUid)) };
    LdapName groupDn = LdapUtils.emptyLdapName();
    try {
        groupDn = new LdapName(groupBase);
        groupDn.add("cn=" + group.getCommonName());
        log.debug("Remove member {} from {}", memberUid, groupDn.toString());
        ldapTemplate.modifyAttributes(groupDn, modificationItems);
        return true;
    } catch (AttributeInUseException ex) {
        log.error("ERROR {}", ex.toString());
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return false;
}

From source file:edu.kit.scc.ldap.LdapPosixUserDao.java

/**
 * Updates a POSIX user in the LDAP directory.
 * /*from  ww w  .j  a v a  2  s.  co  m*/
 * @param posixUser the {@link PosixUser} to update
 * @return the {@link PosixUser} updated
 */
public PosixUser updateUser(PosixUser posixUser) {
    BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
    personBasicAttribute.add("extensibleObject");
    personBasicAttribute.add("inetOrgPerson");
    personBasicAttribute.add("organizationalPerson");
    personBasicAttribute.add("person");
    personBasicAttribute.add("posixAccount");

    Attributes personAttributes = new BasicAttributes();
    personAttributes.put(personBasicAttribute);

    if (posixUser.getCommonName() != null) {
        personAttributes.put("cn", posixUser.getCommonName());
    }
    if (posixUser.getSurName() != null) {
        personAttributes.put("sn", posixUser.getSurName());
    }
    if (posixUser.getUid() != null) {
        personAttributes.put("uid", posixUser.getUid());
    }
    if (posixUser.getUidNumber() != null) {
        personAttributes.put("uidNumber", String.valueOf(posixUser.getUidNumber()));
    }
    if (posixUser.getGidNumber() != null) {
        personAttributes.put("gidNumber", String.valueOf(posixUser.getGidNumber()));
    }
    if (posixUser.getHomeDirectory() != null) {
        personAttributes.put("homeDirectory", posixUser.getHomeDirectory());
    }
    if (posixUser.getUniqueIdentifier() != null) {
        personAttributes.put("uniqueIdentifier", posixUser.getUniqueIdentifier());
    }
    if (posixUser.getDescription() != null) {
        personAttributes.put("description", posixUser.getDescription());
    }
    if (posixUser.getGecos() != null) {
        personAttributes.put("gecos", posixUser.getGecos());
    }
    if (posixUser.getLoginShell() != null) {
        personAttributes.put("loginShell", posixUser.getLoginShell());
    }
    if (posixUser.getUserPassword() != null) {
        personAttributes.put("userPassword", posixUser.getUserPassword());
    }
    if (posixUser.getGivenName() != null) {
        personAttributes.put("givenName", posixUser.getGivenName());
    }
    if (posixUser.getMail() != null) {
        personAttributes.put("mail", posixUser.getMail());
    }

    LdapName userDn = LdapUtils.emptyLdapName();
    try {
        userDn = new LdapName(userBase);
        userDn.add("uid=" + posixUser.getUid());
        log.debug("Update {}", userDn.toString());
        ldapTemplate.rebind(userDn, null, personAttributes);

        return posixUser;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    }
    return null;
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Inserts a new POSIX group into the LDAP directory.
 * /* www  .  j av a 2s  . c o  m*/
 * @param group the {@link PosixGroup} to insert
 * @return the {@link PosixGroup} inserted
 */
public PosixGroup insertGroup(PosixGroup group) {
    if (group.commonName == null || group.gidNumber == null) {
        log.warn("PosixGroup has missing mandatory attributes");
        return null;
    }

    BasicAttribute posixGroupBasicAttribute = new BasicAttribute("objectclass");
    posixGroupBasicAttribute.add("posixGroup");

    Attributes posixGroupAttributes = new BasicAttributes();
    posixGroupAttributes.put(posixGroupBasicAttribute);
    posixGroupAttributes.put("cn", group.getCommonName());
    posixGroupAttributes.put("gidNumber", String.valueOf(group.getGidNumber()));

    if (group.getUserPassword() != null) {
        posixGroupAttributes.put("userPassword", group.getUserPassword());
    }
    if (group.getDescription() != null) {
        posixGroupAttributes.put("description", group.getDescription());
    }
    LdapName newGroupDn = LdapUtils.emptyLdapName();
    try {
        newGroupDn = new LdapName(groupBase);
        newGroupDn.add("cn=" + group.getCommonName());
        log.debug("Insert {}", newGroupDn.toString());
        ldapTemplate.bind(newGroupDn, null, posixGroupAttributes);

        return group;
    } catch (NameAlreadyBoundException ex) {
        log.error("ERROR {}", ex.getMessage());
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.getMessage());
    }
    return null;
}

From source file:edu.kit.scc.ldap.LdapPosixUserDao.java

/**
 * Inserts a new POSIX user into the LDAP directory.
 * /*from ww  w  .j  av a  2  s  .  c  om*/
 * @param posixUser the {@link PosixUser} to insert
 * @return the {@link PosixUser} inserted
 */
public PosixUser insertUser(PosixUser posixUser) {
    if (posixUser.commonName == null || posixUser.gidNumber == null || posixUser.homeDirectory == null
            || posixUser.surName == null || posixUser.uid == null || posixUser.uidNumber == null) {
        log.warn("PosixUser has missing mandatory attributes");
        return null;
    }

    BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
    personBasicAttribute.add("extensibleObject");
    personBasicAttribute.add("inetOrgPerson");
    personBasicAttribute.add("organizationalPerson");
    personBasicAttribute.add("person");
    personBasicAttribute.add("posixAccount");

    Attributes personAttributes = new BasicAttributes();
    personAttributes.put(personBasicAttribute);
    personAttributes.put("cn", posixUser.getCommonName());
    personAttributes.put("sn", posixUser.getSurName());
    personAttributes.put("uid", posixUser.getUid());
    personAttributes.put("uidNumber", String.valueOf(posixUser.getUidNumber()));
    personAttributes.put("gidNumber", String.valueOf(posixUser.getGidNumber()));
    personAttributes.put("homeDirectory", posixUser.getHomeDirectory());

    if (posixUser.getUniqueIdentifier() != null) {
        personAttributes.put("uniqueIdentifier", posixUser.getUniqueIdentifier());
    }
    if (posixUser.getDescription() != null) {
        personAttributes.put("description", posixUser.getDescription());
    }
    if (posixUser.getGecos() != null) {
        personAttributes.put("gecos", posixUser.getGecos());
    }
    if (posixUser.getLoginShell() != null) {
        personAttributes.put("loginShell", posixUser.getLoginShell());
    }
    if (posixUser.getUserPassword() != null) {
        personAttributes.put("userPassword", posixUser.getUserPassword());
    }
    if (posixUser.getGivenName() != null) {
        personAttributes.put("givenName", posixUser.getGivenName());
    }
    if (posixUser.getMail() != null) {
        personAttributes.put("mail", posixUser.getMail());
    }

    LdapName newUserDn = LdapUtils.emptyLdapName();
    try {
        newUserDn = new LdapName(userBase);
        newUserDn.add("uid=" + posixUser.getUid());
        log.debug("Insert {}", newUserDn.toString());
        ldapTemplate.bind(newUserDn, null, personAttributes);

        return posixUser;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    } catch (NameAlreadyBoundException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return null;
}