Example usage for javax.naming.ldap LdapName LdapName

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

Introduction

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

Prototype

public LdapName(List<Rdn> rdns) 

Source Link

Document

Constructs an LDAP name given its parsed RDN components.

Usage

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

/**
 * Updates a POSIX user in the LDAP directory.
 * //from   ww w  .j  av  a  2s.  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:fr.mtlx.odm.TestSessionImpl.java

@Test
public void testBind() throws InvalidNameException {
    String dn = "cn=fire,ou=personnes";

    Person entry = new Person();

    entry.setDn(new LdapName(dn));

    entry.setCn("fire");

    entry.setSn("fox");

    validate(entry);//from  w ww .j ava 2  s.co  m

    session.getOperations(Person.class).bind(entry);
}

From source file:com.zimbra.cs.service.authenticator.CertUtil.java

private String getSubjectAttr(String needAttrName, String needAttrOid) {
    String subjectDN = getSubjectDN();

    try {/*  ww  w .  ja v a2s.  co  m*/
        LdapName dn = new LdapName(subjectDN);
        List<Rdn> rdns = dn.getRdns();

        for (Rdn rdn : rdns) {
            String type = rdn.getType();

            boolean isOid = type.contains(".");

            boolean matched = (isOid ? type.equals(needAttrOid) : type.equals(needAttrName));

            if (matched) {
                Object value = rdn.getValue();
                if (value == null) {
                    continue;
                }

                if (isOid) {
                    byte[] bytes = (byte[]) value;
                    ASN1InputStream decoder = null;
                    try {
                        decoder = new ASN1InputStream(bytes);
                        DEREncodable encoded = decoder.readObject();
                        DERIA5String str = DERIA5String.getInstance(encoded);
                        return str.getString();
                    } catch (IOException e) {
                        ZimbraLog.account.warn(LOG_PREFIX + "unable to decode " + type, e);
                    } finally {
                        ByteUtil.closeStream(decoder);
                    }

                } else {
                    return value.toString();
                }
            }
        }
    } catch (InvalidNameException e) {
        ZimbraLog.account.warn(LOG_PREFIX + "Invalid subject dn value" + subjectDN, e);
    }

    return null;
}

From source file:eu.europa.esig.dss.DSSASN1Utils.java

/**
 * This method can be removed the simple IssuerSerial verification can be
 * performed. In fact the hash verification is sufficient.
 *
 * @param generalNames/*from w w  w  .j  a v  a 2s .  c o  m*/
 * @return
 */
public static String getCanonicalizedName(final GeneralNames generalNames) {
    GeneralName[] names = generalNames.getNames();
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    for (GeneralName name : names) {
        String ldapString = String.valueOf(name.getName());
        LOG.debug("ldapString to canonicalize: {} ", ldapString);
        try {
            LdapName ldapName = new LdapName(ldapString);
            List<Rdn> rdns = ldapName.getRdns();
            for (final Rdn rdn : rdns) {
                treeMap.put(rdn.getType().toLowerCase(), String.valueOf(rdn.getValue()).toLowerCase());
            }
        } catch (InvalidNameException e) {
            throw new DSSException(e);
        }
    }
    StringBuilder stringBuilder = new StringBuilder();
    for (Entry<String, String> entry : treeMap.entrySet()) {
        stringBuilder.append(entry.getKey()).append('=').append(entry.getValue()).append('|');
    }
    final String canonicalizedName = stringBuilder.toString();
    LOG.debug("canonicalizedName: {} ", canonicalizedName);
    return canonicalizedName;
}

From source file:com.newrelic.agent.deps.org.apache.http.conn.ssl.DefaultHostnameVerifier.java

static String extractCN(final String subjectPrincipal) throws SSLException {
    if (subjectPrincipal == null) {
        return null;
    }//from w w  w.j av a  2s  .  c o m
    try {
        final LdapName subjectDN = new LdapName(subjectPrincipal);
        final List<Rdn> rdns = subjectDN.getRdns();
        for (int i = rdns.size() - 1; i >= 0; i--) {
            final Rdn rds = rdns.get(i);
            final Attributes attributes = rds.toAttributes();
            final Attribute cn = attributes.get("cn");
            if (cn != null) {
                try {
                    final Object value = cn.get();
                    if (value != null) {
                        return value.toString();
                    }
                } catch (NoSuchElementException ignore) {
                } catch (NamingException ignore) {
                }
            }
        }
        return null;
    } catch (InvalidNameException e) {
        throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
    }
}

From source file:fr.mtlx.odm.TestSessionImpl.java

@Test
public void testModify() throws InvalidNameException, NameNotFoundException {
    Name dn = new LdapName("cn=fire,ou=personnes");

    Person entry = session.getOperations(Person.class).lookup(dn);

    entry.setCn("fire");

    entry.setSn("bird");

    validate(entry);//w ww  . j a v  a 2s .co  m

    session.getOperations(Person.class).modify(entry);
}

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

/**
 * Removes a POSIX user from the specified POSIX group.
 * //from   w  ww  . jav a  2  s . c  o m
 * @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

/**
 * Deletes a POSIX user from the LDAP directory.
 * /*from  w  ww .ja  v a2 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:fr.mtlx.odm.TestSessionImpl.java

@Test
public void testUnbind() throws InvalidNameException, NameNotFoundException {
    Name dn = new LdapName("cn=fire,ou=personnes");

    Person entry = session.getOperations(Person.class).lookup(dn);

    session.getOperations(Person.class).unbind(entry);
}

From source file:edu.vt.middleware.ldap.ssl.DefaultHostnameVerifier.java

/**
 * Returns the CNs from the supplied certificate.
 *
 * @param  cert  to get CNs from/* w w  w. ja va  2 s .  c  o m*/
 *
 * @return  CNs
 */
private String[] getCNs(final X509Certificate cert) {
    final List<String> names = new ArrayList<String>();
    final String subjectPrincipal = cert.getSubjectX500Principal().toString();
    if (subjectPrincipal != null) {
        try {
            final LdapName subjectDn = new LdapName(subjectPrincipal);
            for (Rdn rdn : subjectDn.getRdns()) {
                final Attributes attrs = rdn.toAttributes();
                final NamingEnumeration<String> ids = attrs.getIDs();
                while (ids.hasMore()) {
                    final String id = ids.next();
                    if (id.toLowerCase().equals("cn") || id.toLowerCase().equals("commonname")
                            || id.toLowerCase().equals("2.5.4.3")) {
                        final Object value = attrs.get(id).get();
                        if (value != null) {
                            if (value instanceof String) {
                                names.add((String) value);
                            } else if (value instanceof Attribute) {
                                // for multi value RDNs the first value is used
                                final Object multiValue = ((Attribute) value).get();
                                if (multiValue != null && multiValue instanceof String) {
                                    names.add((String) multiValue);
                                }
                            }
                        }
                    }
                }
            }
        } catch (NamingException e) {
            if (this.logger.isWarnEnabled()) {
                this.logger.warn("Could not get distinguished name from subject " + subjectPrincipal, e);
            }
        }
    }
    return names.toArray(new String[names.size()]);
}