List of usage examples for javax.naming.directory Attribute getID
String getID();
From source file:Main.java
public static void main(String[] argv) throws Exception { String url = "ldap://localhost/o=JNDITutorial"; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, url); DirContext ctx = new InitialDirContext(env); String[] attrIDs = { "sn", "number", "value", "mail" }; Attributes answer = ctx.getAttributes("cn=yourName, ou=People", attrIDs); NamingEnumeration e = answer.getAll(); while (e.hasMore()) { Attribute attr = (Attribute) e.next(); System.out.println(attr.getID()); }/* www.j av a 2 s. c om*/ }
From source file:AttributeExample.java
public static void main(String[] argc) throws Exception { Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample"); DirContext dctx = new InitialDirContext(env); Attributes attrs = new BasicAttributes(true); attrs.put(new BasicAttribute("email")); attrs.put(new BasicAttribute("website", "www.pri.com")); NamingEnumeration result = dctx.search("ou=People", attrs); while (result.hasMore()) { SearchResult sr = (SearchResult) result.next(); System.out.println("Result = " + sr.getName()); Attributes srchAttrs = sr.getAttributes(); NamingEnumeration attributes = srchAttrs.getAll(); while (attributes.hasMore()) { Attribute attr = (Attribute) attributes.next(); System.out.println("Attribute: " + attr.getID()); NamingEnumeration values = attr.getAll(); while (values.hasMore()) { Object value = values.next(); System.out.println("Value = " + value); }//from w w w. j av a 2s . co m } } }
From source file:ModAttrs.java
static void printAttrs(Attributes attrs) { if (attrs == null) { System.out.println("No attributes"); } else {//from www . j a v a 2 s .c o m /* Print each attribute */ try { for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) { Attribute attr = (Attribute) ae.next(); System.out.println("attribute: " + attr.getID()); /* print each value */ for (NamingEnumeration e = attr.getAll(); e.hasMore(); System.out.println("value: " + e.next())) ; } } catch (NamingException e) { e.printStackTrace(); } } }
From source file:org.apache.cxf.sts.claims.LdapUtils.java
public static Map<String, Attribute> getAttributesOfEntry(LdapTemplate ldapTemplate, String baseDN, String objectClass, String filterAttributeName, String filterAttributeValue, String[] searchAttributes) { Map<String, Attribute> ldapAttributes = null; AttributesMapper mapper = new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { Map<String, Attribute> map = new HashMap<String, Attribute>(); NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll(); while (attrEnum.hasMore()) { Attribute att = attrEnum.next(); map.put(att.getID(), att); }// www. j av a 2 s .co m return map; } }; List<?> result = null; AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", objectClass)) .and(new EqualsFilter(filterAttributeName, filterAttributeValue)); result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(), SearchControls.SUBTREE_SCOPE, searchAttributes, mapper); if (result != null && result.size() > 0) { ldapAttributes = CastUtils.cast((Map<?, ?>) result.get(0)); } return ldapAttributes; }
From source file:security.AuthenticationManager.java
public static Map<String, String> getUserAttributes(DirContext ctx, String searchBase, String userName, String principalDomain, String... attributeNames) throws NamingException { if (StringUtils.isBlank(userName)) { throw new IllegalArgumentException("Username and password can not be blank."); }/* w w w . j a va2s.c o m*/ if (attributeNames.length == 0) { return Collections.emptyMap(); } Attributes matchAttr = new BasicAttributes(true); BasicAttribute basicAttr = new BasicAttribute("userPrincipalName", userName + principalDomain); matchAttr.put(basicAttr); NamingEnumeration<? extends SearchResult> searchResult = ctx.search(searchBase, matchAttr, attributeNames); if (ctx != null) { ctx.close(); } Map<String, String> result = new HashMap<>(); if (searchResult.hasMore()) { NamingEnumeration<? extends Attribute> attributes = searchResult.next().getAttributes().getAll(); while (attributes.hasMore()) { Attribute attr = attributes.next(); String attrId = attr.getID(); String attrValue = (String) attr.get(); result.put(attrId, attrValue); } } return result; }
From source file:org.wso2.carbon.appfactory.userstore.internal.OTLDAPUtil.java
public static String getEmailFromUserId(String uid, LDAPConnectionContext connectionSource, String userSearchBase) throws UserStoreException { // check from cache String email = otEmailCache.getValueFromCache(uid); if (email != null && !email.isEmpty()) { return email; }//from w ww . j a v a2 s.c o m // check from ldap and update the cache StringBuffer buff = new StringBuffer(); buff.append("(&(objectClass=inetOrgPerson)(uid=").append(uid).append("))"); if (log.isDebugEnabled()) { log.debug("Searching for " + buff.toString()); } DirContext dirContext = connectionSource.getContext(); NamingEnumeration<SearchResult> answer = null; try { String[] returnedAttributes = { "mail" }; answer = searchForUser(buff.toString(), returnedAttributes, dirContext, userSearchBase); int count = 0; SearchResult userObj = null; while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); if (count > 0) { log.error("More than one user exist for the same name"); } count++; userObj = sr; } if (userObj != null) { Attributes attributes = userObj.getAttributes(); Attribute mailAttribute = attributes.get("mail"); if (mailAttribute != null) { email = mailAttribute.getID(); } } otEmailCache.addToCache(uid, email); return email; } catch (Exception e) { log.error(e.getMessage(), e); throw new UserStoreException(e.getMessage(), e); } finally { JNDIUtil.closeNamingEnumeration(answer); JNDIUtil.closeContext(dirContext); } }
From source file:org.springframework.ldap.support.LdapUtils.java
/** * Iterate through all the values of the specified Attribute calling back to * the specified callbackHandler.//from w w w . j a v a2 s . com * @param attribute the Attribute to work with; not <code>null</code>. * @param callbackHandler the callbackHandler; not <code>null</code>. * @since 1.3 */ public static void iterateAttributeValues(Attribute attribute, AttributeValueCallbackHandler callbackHandler) { Assert.notNull(attribute, "Attribute must not be null"); Assert.notNull(callbackHandler, "callbackHandler must not be null"); for (int i = 0; i < attribute.size(); i++) { try { callbackHandler.handleAttributeValue(attribute.getID(), attribute.get(i), i); } catch (javax.naming.NamingException e) { throw LdapUtils.convertLdapException(e); } } }
From source file:cyrille.jndi.LdapTest.java
@Test public void test() throws Exception { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"); env.put(Context.SECURITY_CREDENTIALS, "secret"); DirContext dirContext = new InitialDirContext(env); Attributes attributes = dirContext.getAttributes("uid=aeinstein,ou=Users,dc=example,dc=com"); for (NamingEnumeration<Attribute> attributesEnumeration = (NamingEnumeration<Attribute>) attributes .getAll(); attributesEnumeration.hasMore();) { Attribute attribute = attributesEnumeration.next(); System.out.print(attribute.getID() + "="); for (NamingEnumeration<?> attributeValues = attribute.getAll(); attributeValues.hasMore();) { Object value = attributeValues.next(); if (value instanceof byte[] && "userpassword".equals(attribute.getID())) { byte[] bytes = (byte[]) value; System.out.print(new String(bytes) + ", "); } else { System.out.print(value + ", "); }/*from ww w. j a va2 s . c o m*/ } System.out.println(); } }
From source file:ldap.UserAccountImpl.java
public String toString() { StringBuffer buffer = new StringBuffer(); String name = null;//from ww w . jav a 2s . 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:org.iplantc.persondir.support.ldap.AttributesMapperImpl.java
@Override public Object mapFromAttributes(Attributes attributes) throws NamingException { final int attributeCount = attributes.size(); final Map<String, Object> mapOfAttrValues = this.createAttributeMap(attributeCount); for (final NamingEnumeration<? extends Attribute> attributesEnum = attributes.getAll(); attributesEnum .hasMore();) {/* w ww .ja va2 s. c om*/ final Attribute attribute = attributesEnum.next(); if (!this.ignoreNull || attribute.size() > 0) { final String attrName = attribute.getID(); final String key = this.getAttributeKey(attrName); final NamingEnumeration<?> valuesEnum = attribute.getAll(); final List<?> values = this.getAttributeValues(valuesEnum); mapOfAttrValues.put(key, values); } } return mapOfAttrValues; }