Example usage for javax.naming.ldap Rdn toAttributes

List of usage examples for javax.naming.ldap Rdn toAttributes

Introduction

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

Prototype

public Attributes toAttributes() 

Source Link

Document

Retrieves the javax.naming.directory.Attributes Attributes view of the type/value mappings contained in this Rdn.

Usage

From source file:RdnGetters.java

public static void main(String args[]) throws Exception {
    Attributes attrs = new BasicAttributes();
    attrs.put("o", "Yellow");
    attrs.put("cn", "Mango");

    byte[] mangoJuice = new byte[6];
    for (int i = 0; i < mangoJuice.length; i++) {
        mangoJuice[i] = (byte) i;
    }/*  w  w w . j  a v  a2 s  .c  o m*/
    attrs.put("ou", mangoJuice);
    Rdn rdn = new Rdn(attrs);

    System.out.println();
    System.out.println("size:" + rdn.size());
    System.out.println("getType(): " + rdn.getType());
    System.out.println("getValue(): " + rdn.getValue());

    // test toAttributes
    System.out.println();
    System.out.println("toAttributes(): " + rdn.toAttributes());
}

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  2  s .  co 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:edu.vt.middleware.ldap.ssl.DefaultHostnameVerifier.java

/**
 * Returns the CNs from the supplied certificate.
 *
 * @param  cert  to get CNs from//from  www .  ja  va  2s .co 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()]);
}

From source file:com.evolveum.midpoint.model.common.expression.functions.BasicExpressionFunctions.java

public String determineLdapSingleAttributeValue(String dn, String attributeName, Collection<?> values)
        throws NamingException {
    if (values == null || values.isEmpty()) {
        return null;
    }//w  w  w .ja  v a 2s.  c om

    Collection<String> stringValues = null;
    // Determine item type, try to convert to strings
    Object firstElement = values.iterator().next();
    if (firstElement instanceof String) {
        stringValues = (Collection) values;
    } else if (firstElement instanceof Element) {
        stringValues = new ArrayList<String>(values.size());
        for (Object value : values) {
            Element element = (Element) value;
            stringValues.add(element.getTextContent());
        }
    } else {
        throw new IllegalArgumentException("Unexpected value type " + firstElement.getClass());
    }

    if (stringValues.size() == 1) {
        return stringValues.iterator().next();
    }

    if (StringUtils.isBlank(dn)) {
        throw new IllegalArgumentException(
                "No dn argument specified, cannot determine which of " + values.size() + " values to use");
    }

    LdapName parsedDn = new LdapName(dn);
    for (int i = 0; i < parsedDn.size(); i++) {
        Rdn rdn = parsedDn.getRdn(i);
        Attributes rdnAttributes = rdn.toAttributes();
        NamingEnumeration<String> rdnIDs = rdnAttributes.getIDs();
        while (rdnIDs.hasMore()) {
            String rdnID = rdnIDs.next();
            Attribute attribute = rdnAttributes.get(rdnID);
            if (attributeName.equals(attribute.getID())) {
                for (int j = 0; j < attribute.size(); j++) {
                    Object value = attribute.get(j);
                    if (stringValues.contains(value)) {
                        return (String) value;
                    }
                }
            }
        }
    }

    // Fallback. No values in DN. Just return the first alphabetically-wise value.
    return Collections.min(stringValues);
}

From source file:org.glite.slcs.pki.bouncycastle.X509PrincipalUtil.java

/**
 * Reads the LdapName {@link Rdn} component and fills the given vectors.
 * /*w  w w. j a va2 s.  co  m*/
 * @param rdn
 *            The {@link Rdn} to read.
 * @param oids
 *            The vector of OID.
 * @param values
 *            The vector of value.
 * @param added
 *            The added status vector.
 * @throws NamingException
 *             if an error occurs.
 */
private void readRdn(Rdn rdn, Vector<DERObjectIdentifier> oids, Vector<Object> values, Vector<Boolean> added)
        throws NamingException {
    LOG.debug("RDN: " + rdn);
    Enumeration<? extends Attribute> attrs = rdn.toAttributes().getAll();
    do {
        if (attrs.hasMoreElements()) {
            Attribute attr = attrs.nextElement();
            readAttr(attr, oids, values, added);
            start_ = true;
        }
    } while (attrs.hasMoreElements());
}

From source file:org.sipfoundry.sipxconfig.bulk.ldap.UserMapper.java

public Collection<String> getGroupNames(SearchResult sr) throws NamingException {
    Set<String> groupNames = new HashSet<String>();
    // group names in the current entry
    Attributes attrs = sr.getAttributes();
    Set<String> entryGroups = replaceWhitespace(getValues(attrs, Index.USER_GROUP));
    if (entryGroups != null) {
        groupNames.addAll(entryGroups);/*from   w w w  .jav a  2s  .  c o  m*/
    }

    // group names found in distinguished name
    if (sr.isRelative()) {
        String name = sr.getName();
        LdapName ldapName = new LdapName(name);
        List<Rdn> rdns = ldapName.getRdns();
        for (Rdn rdn : rdns) {
            Attributes rdnsAttributes = rdn.toAttributes();
            Set<String> rdnsGroups = replaceWhitespace(getValues(rdnsAttributes, Index.USER_GROUP));
            if (rdnsGroups != null) {
                groupNames.addAll(rdnsGroups);
            }

        }
    }
    //only if there is no already defined group, add the default user group
    if (groupNames.isEmpty()) {
        String defaultGroupName = getAttrMap().getDefaultGroupName();
        if (defaultGroupName != null) {
            groupNames.add(defaultGroupName);
        }
    }
    return groupNames;
}