Example usage for javax.naming.directory Attribute size

List of usage examples for javax.naming.directory Attribute size

Introduction

In this page you can find the example usage for javax.naming.directory Attribute size.

Prototype

int size();

Source Link

Document

Retrieves the number of values in this attribute.

Usage

From source file:org.jasig.portlet.contacts.adapters.impl.ldap.ConfigurableContactAttributesMapper.java

/**
 * Sets the object's property from the LDAP attribute value or default property value.
 * @param obj object to set property on//from   ww w . ja  v  a 2  s  .  c  o m
 * @param propertyName property name to set
 * @param propertyNameToLDAPNameMap Map of object property names to ldap attribute names
 * @param attrs LDAP attributes
 * @return Populated object, or null if the LDAP attributes do not contain values that create a
 *         reasonably useful object of the requested type
 */
private <T> void setProperty(T obj, String propertyName, Map<String, ?> propertyNameToLDAPNameMap,
        Attributes attrs) {
    String method = "set" + StringUtils.capitalize(propertyName);
    String ldapAttributeName = (String) propertyNameToLDAPNameMap.get(propertyName);
    try {
        if (StringUtils.hasLength(ldapAttributeName)) {
            if (ldapAttributeName.startsWith(defaultPrefix)) {
                obj.getClass().getMethod(method, String.class).invoke(obj,
                        ldapAttributeName.substring(defaultPrefix.length()));
            } else {
                Attribute attr = attrs.get(ldapAttributeName);
                obj.getClass().getMethod(method, String.class).invoke(obj, getValue(attr));
                if (attr != null && attr.size() > 1) {
                    logger.warn("Found multiple values for LDAP attribute " + ldapAttributeName
                            + attrs.get("cn") != null ? ", cn=" + config.get("cn") : "");
                }
            }
        }
    } catch (Exception ex) {
        logger.error("Exception setting property for " + obj.getClass().getCanonicalName() + "." + method
                + ", LDAP attribute " + ldapAttributeName, ex);
    }
}

From source file:ldap.UserAccountImpl.java

public String toString() {
    StringBuffer buffer = new StringBuffer();
    String name = null;/*from   w  w  w. j  a v a 2  s  .  co m*/
    try {
        NamingEnumeration attList = getAll();
        while (attList.hasMore()) {
            Attribute att = (Attribute) attList.next();
            //if (att.getID().equals(Config.USER_NAMING_ATT))
            if (att.getID().equals(LdapConstants.ldapAttrUid))
                name = att.get().toString() + "\n";

            buffer.append("    ").append(att.getID()).append(": ");

            if (att.size() == 1)
                buffer.append(att.get().toString()).append("\n");
            else {
                NamingEnumeration values = att.getAll();
                buffer.append("\n");
                while (values.hasMore())
                    buffer.append("        ").append(values.next()).append("\n");
            }
        }
        if (name != null)
            buffer.insert(0, name);
    } catch (NamingException e) {
        return "Unexpected Internal Error dumping UserAccount to text.\nError was: " + e.getMessage();
    }
    return buffer.toString();
}

From source file:nl.nn.adapterframework.webcontrol.LoginFilter.java

private boolean isMemberOf(DirContext ctx, String dnUser, String dnGroup) throws NamingException {
    DirContext lookedContext = (DirContext) (ctx.lookup(dnGroup));
    Attribute attrs = lookedContext.getAttributes("").get("member");
    for (int i = 0; i < attrs.size(); i++) {
        String foundMember = (String) attrs.get(i);
        if (foundMember.equalsIgnoreCase(dnUser)) {
            return true;
        }/*from  ww w  . j  ava2 s .c  o m*/
    }
    return false;
}

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

/**
 * Reads the given {@link Attribute} and recurses into RDN attributes, fills
 * the given vectors.//from  w  w w .  j a  v  a 2  s  . c  o  m
 * 
 * @param attr
 *            The {@link Attribute} to read.
 * @param oids
 *            The vector of OID.
 * @param values
 *            The vector of value.
 * @param added
 *            The added status vector.
 * @throws NamingException
 *             if a naming error occurs.
 */
private void readAttr(Attribute attr, Vector<DERObjectIdentifier> oids, Vector<Object> values,
        Vector<Boolean> added) throws NamingException {
    // Recursively looking into each attribute
    LOG.debug("Attribute: " + attr);
    for (int i = 0; i < attr.size(); i++) {
        if (attr.get(i) instanceof Attribute) {
            Attribute rdnAttr = (Attribute) attr.get(i);
            LOG.debug("Attribute RDN: " + rdnAttr);
            readAttr(rdnAttr, oids, values, added);
        } else { // Get back the OID from name
            DERObjectIdentifier oid = (DERObjectIdentifier) X509Name.DefaultLookUp
                    .get(Strings.toLowerCase(attr.getID()));
            oids.add(oid);
            Object attrValue = attr.get(i);
            LOG.debug("Attribute value: " + attrValue);
            values.add(attrValue);
            added.add(start_);
            start_ = true;

        }
    }

}

From source file:nl.nn.adapterframework.ldap.LdapFindMemberPipe.java

private boolean findMember(String host, int port, String dnSearchIn, boolean useSsl, String dnFind,
        boolean recursiveSearch) throws NamingException {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    String provUrl = retrieveUrl(host, port, dnSearchIn, useSsl);
    env.put(Context.PROVIDER_URL, provUrl);
    if (StringUtils.isNotEmpty(cf.getUsername())) {
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, cf.getUsername());
        env.put(Context.SECURITY_CREDENTIALS, cf.getPassword());
    } else {//from www  .j  a  v  a 2 s  .  com
        env.put(Context.SECURITY_AUTHENTICATION, "none");
    }
    DirContext ctx = null;
    try {
        try {
            ctx = new InitialDirContext(env);
        } catch (CommunicationException e) {
            log.info("Cannot create constructor for DirContext (" + e.getMessage()
                    + "], will try again with dummy SocketFactory");
            env.put("java.naming.ldap.factory.socket", DummySSLSocketFactory.class.getName());
            ctx = new InitialLdapContext(env, null);
        }
        Attribute attrs = ctx.getAttributes("").get("member");
        if (attrs != null) {
            boolean found = false;
            for (int i = 0; i < attrs.size() && !found; i++) {
                String dnFound = (String) attrs.get(i);
                if (dnFound.equalsIgnoreCase(dnFind)) {
                    found = true;
                } else {
                    if (recursiveSearch) {
                        found = findMember(host, port, dnFound, useSsl, dnFind, recursiveSearch);
                    }
                }
            }
            return found;
        }
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                log.warn("Exception closing DirContext", e);
            }
        }
    }
    return false;
}

From source file:org.craftercms.studio.impl.v1.service.security.DbWithLdapExtensionSecurityProvider.java

private void extractGroupsFromAttribute(User user, String groupNameAttribName, Attribute groupNameAttrib,
        SiteFeed siteFeed) throws NamingException {
    if (groupNameAttrib != null && groupNameAttrib.size() > 0) {
        NamingEnumeration groupAttribValues = groupNameAttrib.getAll();
        while (groupAttribValues.hasMore()) {
            Object groupNameObj = groupAttribValues.next();
            if (groupNameObj != null) {
                String groupName = extractGroupNameFromAttributeValue(groupNameObj.toString());
                if (StringUtils.isNotEmpty(groupName)) {
                    addGroupToUser(user, groupName, siteFeed);
                }//from www . j a  va  2 s.  c o  m
            }
        }
    } else {
        logger.debug("No LDAP attribute " + groupNameAttribName + " found for username " + user.getUsername());
    }
}

From source file:org.lsc.beans.LscBean.java

/**
 * Set an attribute. API CHANGE: Do nothing if attribute is empty
 * //from   ww  w  . ja  v a2s  . co m
 * @param attr
 *            the attribute to set
 */
public final void setAttribute(final Attribute attr) {
    if (attr != null && attr.size() > 0) {
        // convert the Attribute into a Set of values
        try {
            setAttribute(attr.getID(), SetUtils.attributeToSet(attr));
        } catch (NamingException e) {
            LOGGER.error("Error storing the attribute {}: {}", attr.getID(), e.toString());
            LOGGER.debug(e.toString(), e);
        }
    }
}

From source file:org.eclipse.skalli.core.user.ldap.LDAPClient.java

private String getStringValue(Attributes attributes, LDAPAttributeNames attributeName) throws NamingException {
    String ret = null;/*from w ww.ja va2s .com*/
    Attribute attribute = attributes.get(attributeName.getLdapKey());
    if (attribute != null) {
        for (int i = 0; i < attribute.size(); i++) {
            ret = (String) attribute.get(i);
        }
    }
    return ret;
}

From source file:ca.tnt.ldaputils.impl.LdapEntry.java

public String getStringValue(final String attribute) {
    final Attribute ldapAttribute;
    try {// w  ww.j  a v  a  2s. com
        ldapAttribute = attributes.get(attribute);
        if (ldapAttribute != null && ldapAttribute.size() != 0)
            return (String) ldapAttribute.get(0);
        else {
            return null;
        }
    } catch (NamingException e) {
        throw new LdapNamingException(e);
    }

}

From source file:ca.tnt.ldaputils.impl.LdapEntry.java

public List getAttributeValues(final String attribute) {
    final Attribute ldapAttribute;
    List values = null;//w ww  . ja va 2 s  . c  om

    ldapAttribute = attributes.get(attribute);
    try {
        if (ldapAttribute != null && ldapAttribute.size() != 0) {
            values = Collections.list(ldapAttribute.getAll());
        }
    } catch (NamingException e) {
        throw new LdapNamingException(e);
    }

    return values;
}