Example usage for javax.naming.directory Attribute get

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

Introduction

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

Prototype

Object get() throws NamingException;

Source Link

Document

Retrieves one of this attribute's values.

Usage

From source file:nl.surfnet.coin.ldap.LdapClientImpl.java

/**
 * Save get of an Attribute value, may return null
 * @param attrID the attribute id/*from  ww w.j a  v  a2 s .  co  m*/
 * @param attributes the attributes holder to get it from
 * @return the stringified attribute or null.
 */
private String getAttribute(String attrID, Attributes attributes) {
    Attribute attribute = attributes.get(attrID);
    try {
        return attribute != null ? (String) attribute.get() : null;
    } catch (NamingException e) {
        // ignore this as we can't recover
        return null;
    }
}

From source file:ldap.UserAccountImpl.java

public String toString() {
    StringBuffer buffer = new StringBuffer();
    String name = null;/*  ww  w.  j ava  2s  .  c  o  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:org.apache.lens.server.user.LDAPBackedDatabaseUserConfigLoader.java

/**
 * Gets the attributes./*w  w  w.  jav  a 2 s . c  om*/
 *
 * @param user the user
 * @return the attributes
 * @throws NamingException the naming exception
 */
public String[] getAttributes(String user) throws NamingException {
    String[] attributes = new String[ldapFields.length];
    SearchResult sr = findAccountByAccountName(user);
    for (int i = 0; i < attributes.length; i++) {
        Attribute attr = sr.getAttributes().get(ldapFields[i]);
        attributes[i] = (attr == null ? null : attr.get().toString());
    }
    return attributes;
}

From source file:org.ow2.proactive.addons.ldap_query.LDAPClient.java

public String searchQueryLDAP() {
    NamingEnumeration results = null;
    ObjectMapper mapper = new ObjectMapper();
    Response response;/*  ww w. ja v a 2 s  .  c  o m*/
    String resultOutput = new String();
    List<Map<String, String>> attributesList = new LinkedList<>();

    String[] attributesToReturn = splitAttributes(allLDAPClientParameters.get(ARG_SELECTED_ATTRIBUTES));
    try {
        ldapConnection = LDAPConnectionUtility.connect(allLDAPClientParameters.get(ARG_URL),
                allLDAPClientParameters.get(ARG_DN_BASE), allLDAPClientParameters.get(ARG_USERNAME),
                allLDAPClientParameters.get(ARG_PASSWORD));
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        if (attributesToReturn.length > 0) {
            controls.setReturningAttributes(attributesToReturn);
        }
        results = ldapConnection.search(
                getFullLdapSearchBase(allLDAPClientParameters.get(ARG_DN_BASE),
                        allLDAPClientParameters.get(ARG_SEARCH_BASE)),
                allLDAPClientParameters.get(ARG_SEARCH_FILTER), controls);

        // Iterate through all attributes in the result of search query
        while (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();

            if (attributes != null && attributes.size() > 0) {
                NamingEnumeration ae = attributes.getAll();
                Map<String, String> attributesMap = new HashMap<>();
                while (ae.hasMore()) {
                    Attribute attribute = (Attribute) ae.next();
                    attributesMap.put(attribute.getID(), attribute.get().toString());
                }
                attributesList.add(attributesMap);
            }
        }
        response = new LDAPResponse("Ok", attributesList);
    } catch (Exception e) {
        response = new ErrorResponse("Error", e.toString());
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (ldapConnection != null) {
            try {
                ldapConnection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    try {
        resultOutput = mapper.writeValueAsString(response);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return resultOutput;
}

From source file:org.jasig.ssp.service.impl.LdapPersonAttributesService.java

private String extractProperty(final Attributes attrs, final String property) {
    final Attribute attrib = attrs.get(property);
    if (null == attrib) {
        return null;
    } else {/*from ww w.j a  v a  2  s.c o  m*/
        Object val;
        try {
            val = attrib.get();
        } catch (NamingException e) {
            return null;
        }

        try {
            return (String) val;
        } catch (ClassCastException cce) {
            return val.toString();
        }
    }
}

From source file:org.beangle.security.ldap.connect.SimpleLdapUserStore.java

public String getPassword(String uid) {
    Set<Attribute> passwords = getAttributes(uid, "userPassword");
    if (passwords.isEmpty())
        return null;
    for (Attribute attr : passwords) {
        byte encPassword[];
        try {//from  w ww .j a v  a  2 s.c  o m
            encPassword = (byte[]) attr.get();
        } catch (NamingException e) {
            logger.error("get password of " + uid + "error", e);
            return null;
        }
        return new String(encPassword);
    }
    return null;
}

From source file:com.photon.phresco.ldap.impl.LDAPManagerImpl.java

private String getMailId(Attributes attrs) throws NamingException {
    if (isDebugEnabled) {
        S_LOGGER.debug("Entering Method LDAPManagerImpl.getMailId(Attributes attrs");
    }//w w w  .  j a va  2s .co m
    String mailId = null;
    Attribute attribute = attrs.get(ldapConfig.getMailIdAttribute());
    if (attribute != null) {
        mailId = (String) attribute.get();
    }
    return mailId;
}

From source file:org.hyperic.hq.plugin.openldap.OpenLDAPMeasurementPlugin.java

private MetricValue getMetric(Metric metric, String tree, String attr)
        throws MetricNotFoundException, NamingException {
    NamingEnumeration enumer = null;
    try {/*w w w. ja v  a 2 s.  co  m*/
        String[] a = { attr };
        SearchControls cons = new SearchControls();
        cons.setSearchScope(SearchControls.OBJECT_SCOPE);
        cons.setReturningAttributes(a);
        enumer = getDirContext(metric.getProperties()).search(tree, "(&(objectClass=*))", cons);
        while (enumer.hasMore()) {
            SearchResult searchresult = (SearchResult) enumer.next();
            Attributes attrs = searchresult.getAttributes();
            Attribute val;
            if (null != (val = attrs.get(attr))) {
                return new MetricValue(new Double(val.get().toString()), System.currentTimeMillis());
            }
        }
        throw new MetricNotFoundException("");
    } finally {
        if (enumer != null) {
            enumer.close();
        }
    }
}

From source file:py.una.pol.karaku.security.KarakuUserService.java

private List<KarakuPermission> loadAuthoritiesByDn(String uid) {

    List<KarakuPermission> listaRoles = new ArrayList<KarakuPermission>();

    try {//from   ww w .ja  va  2 s  .  c  om
        DirContext ctx = getInitialDirContext(propertiesUtil.get(LDAP_ADMIN_KEY),
                propertiesUtil.get(LDAP_ADMIN_PASS_KEY));
        Attributes matchAttrs = new BasicAttributes(true);
        matchAttrs.put(new BasicAttribute("member", getRealUsername(uid)));
        NamingEnumeration<SearchResult> answer = ctx.search("ou=permissions", matchAttrs);

        while (answer.hasMore()) {
            SearchResult searchResult = answer.next();
            Attributes attributes = searchResult.getAttributes();
            Attribute attr = attributes.get("cn");
            String rol = (String) attr.get();
            KarakuPermission grantedAuthority = new KarakuPermission(rol);
            listaRoles.add(grantedAuthority);
        }

        return listaRoles;
    } catch (NamingException e) {
        LOG.warn("Can't create Ldap Context", e);
        return Collections.emptyList();
    }
}

From source file:com.photon.phresco.ldap.impl.LDAPManagerImpl.java

private String getDisplayName(Attributes attrs) throws NamingException {
    if (isDebugEnabled) {
        S_LOGGER.debug("Entering Method LDAPManagerImpl.getDisplayName(Attributes attrs)");
    }//w w  w.j a  va 2  s  .  co  m
    String displayName = null;
    Attribute attribute = attrs.get(ldapConfig.getDisplayNameAttribute());
    if (attribute != null) {
        displayName = (String) attribute.get();
    }
    return displayName;
}