Java Code Examples for javax.naming.ldap.Rdn#toAttributes()

The following examples show how to use javax.naming.ldap.Rdn#toAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonNameCertInfoExtractor.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
public String extractCertInfo(List<Rdn> rdns) throws NamingException {
    for (Rdn rdn : rdns) {
        Attributes attrs = rdn.toAttributes();
        Attribute attr = attrs.get("CN");
        if (attr != null) {
            Object o = attr.get();
            if (o != null) {
                return o.toString();
            }
        }
    }
    return null;
}
 
Example 2
Source File: AbstractCommonHostnameVerifierFix.java    From steady with Apache License 2.0 5 votes vote down vote up
static String[] extractCNs(final String subjectPrincipal) throws SSLException {
    if (subjectPrincipal == null) {
        return null;
    }
    final List<String> cns = new ArrayList<String>();
    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) {
                        cns.add(value.toString());
                    }
                } catch (NamingException ignore) {
                }
            }
        }
    } catch (InvalidNameException e) {
        throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
    }
    return cns.isEmpty() ? null : cns.toArray(new String[ cns.size() ]);
}
 
Example 3
Source File: J_AbstractVerifier_F.java    From steady with Apache License 2.0 5 votes vote down vote up
static String[] extractCNs(final String subjectPrincipal) throws SSLException {
    if (subjectPrincipal == null) {
        return null;
    }
    final List<String> cns = new ArrayList<String>();
    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) {
                        cns.add(value.toString());
                    }
                } catch (NamingException ignore) {
                }
            }
        }
    } catch (InvalidNameException e) {
        throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
    }
    return cns.isEmpty() ? null : cns.toArray(new String[ cns.size() ]);
}
 
Example 4
Source File: AbstractVerifierFix.java    From steady with Apache License 2.0 5 votes vote down vote up
static String[] extractCNs(final String subjectPrincipal) throws SSLException {
    if (subjectPrincipal == null) {
        return null;
    }
    final List<String> cns = new ArrayList<String>();
    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) {
                        cns.add(value.toString());
                    }
                } catch (NamingException ignore) {
                }
            }
        }
    } catch (InvalidNameException e) {
        throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
    }
    return cns.isEmpty() ? null : cns.toArray(new String[ cns.size() ]);
}
 
Example 5
Source File: DefaultHostnameVerifier.java    From cxf with Apache License 2.0 5 votes vote down vote up
static String extractCN(final String subjectPrincipal) throws SSLException {
    if (subjectPrincipal == null) {
        return null;
    }
    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 | NamingException ignore) {
                    //
                }
            }
        }
        return null;
    } catch (InvalidNameException e) {
        throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
    }
}
 
Example 6
Source File: LdapCtx.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 7
Source File: AbstractContextSource.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
static String formatForUrl(LdapName ldapName) {
    StringBuilder sb = new StringBuilder();
    ListIterator<Rdn> it = ldapName.getRdns().listIterator(ldapName.size());
    while (it.hasPrevious()) {
        Rdn component = it.previous();

        Attributes attributes = component.toAttributes();

        // Loop through all attribute of the rdn (usually just one, but more are supported by RFC)
        NamingEnumeration<? extends Attribute> allAttributes = attributes.getAll();
        while(allAttributes.hasMoreElements()) {
            Attribute oneAttribute = allAttributes.nextElement();
            String encodedAttributeName = nameEncodeForUrl(oneAttribute.getID());

            // Loop through all values of the attribute (usually just one, but more are supported by RFC)
            NamingEnumeration <?> allValues;
            try {
                allValues = oneAttribute.getAll();
            } catch (NamingException e) {
                throw new UncategorizedLdapException("Unexpected error occurred formatting base URL", e);
            }

            while(allValues.hasMoreElements()) {
                sb.append(encodedAttributeName).append('=');

                Object oneValue = allValues.nextElement();
                if (oneValue instanceof String) {
                    String oneString = (String) oneValue;
                    sb.append(nameEncodeForUrl(oneString));
                } else {
                    throw new IllegalArgumentException("Binary attributes not supported for base URL");
                }

                if(allValues.hasMoreElements()) {
                    sb.append('+');
                }
            }
            if(allAttributes.hasMoreElements()) {
                sb.append('+');
            }
        }

        if(it.hasPrevious()) {
            sb.append(',');
        }
    }
    return sb.toString();
}
 
Example 8
Source File: LdapCtx.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 9
Source File: LdapCtx.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 10
Source File: LdapCtx.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 11
Source File: LdapCtx.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 12
Source File: LdapCtx.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 13
Source File: LdapCtx.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 14
Source File: LdapCtx.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 15
Source File: LdapCtx.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @return Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 16
Source File: LdapCtx.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 17
Source File: LdapCtx.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 18
Source File: LdapCtx.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}
 
Example 19
Source File: LdapCtx.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds attributes from RDN to attrs if not already present.
 * Note that if attrs already contains an attribute by the same name,
 * or if the distinguished name is empty, then leave attrs unchanged.
 *
 * @param dn The non-null DN of the entry to add
 * @param attrs The non-null attributes of entry to add
 * @param directUpdate Whether attrs can be updated directly
 * @returns Non-null attributes with attributes from the RDN added
 */
private static Attributes addRdnAttributes(String dn, Attributes attrs,
    boolean directUpdate) throws NamingException {

        // Handle the empty name
        if (dn.equals("")) {
            return attrs;
        }

        // Parse string name into list of RDNs
        List<Rdn> rdnList = (new LdapName(dn)).getRdns();

        // Get leaf RDN
        Rdn rdn = rdnList.get(rdnList.size() - 1);
        Attributes nameAttrs = rdn.toAttributes();

        // Add attributes of RDN to attrs if not already there
        NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
        Attribute nameAttr;
        while (enum_.hasMore()) {
            nameAttr = enum_.next();

            // If attrs already has the attribute, don't change or add to it
            if (attrs.get(nameAttr.getID()) ==  null) {

                /**
                 * When attrs.isCaseIgnored() is false, attrs.get() will
                 * return null when the case mis-matches for otherwise
                 * equal attrIDs.
                 * As the attrIDs' case is irrelevant for LDAP, ignore
                 * the case of attrIDs even when attrs.isCaseIgnored() is
                 * false. This is done by explicitly comparing the elements in
                 * the enumeration of IDs with their case ignored.
                 */
                if (!attrs.isCaseIgnored() &&
                        containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
                    continue;
                }

                if (!directUpdate) {
                    attrs = (Attributes)attrs.clone();
                    directUpdate = true;
                }
                attrs.put(nameAttr);
            }
        }

        return attrs;
}