List of usage examples for javax.naming.directory SearchResult getAttributes
public Attributes getAttributes()
From source file:org.apache.cloudstack.ldap.OpenLdapUserManagerImpl.java
protected LdapUser createUser(final SearchResult result) throws NamingException { final Attributes attributes = result.getAttributes(); final String username = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getUsernameAttribute()); final String email = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getEmailAttribute()); final String firstname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getFirstnameAttribute()); final String lastname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getLastnameAttribute()); final String principal = result.getNameInNamespace(); String domain = principal.replace( "cn=" + LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getCommonNameAttribute()) + ",", "");// w w w .j av a2 s .c om domain = domain.replace("," + _ldapConfiguration.getBaseDn(), ""); domain = domain.replace("ou=", ""); boolean disabled = isUserDisabled(result); return new LdapUser(username, email, firstname, lastname, principal, domain, disabled); }
From source file:org.apache.archiva.redback.common.ldap.role.TestLdapRoleMapper.java
private void assertExist(DirContext context, String dn, String attribute, String value) throws NamingException { SearchControls ctls = new SearchControls(); ctls.setDerefLinkFlag(true);//from w ww .j a v a 2s . c o m ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE); ctls.setReturningAttributes(new String[] { "*" }); BasicAttributes matchingAttributes = new BasicAttributes(); matchingAttributes.put(attribute, value); BasicAttribute objectClass = new BasicAttribute("objectClass"); objectClass.add("inetOrgPerson"); matchingAttributes.put(objectClass); NamingEnumeration<SearchResult> results = context.search(suffix, matchingAttributes); assertTrue(results.hasMoreElements()); SearchResult result = results.nextElement(); Attributes attrs = result.getAttributes(); Attribute testAttr = attrs.get(attribute); assertEquals(value, testAttr.get()); }
From source file:net.officefloor.plugin.jndi.ldap.CredentialStoreTest.java
/** * Ensure able to obtain credentials./*from w ww . j a v a2 s .com*/ */ public void testObtainCredentials() throws Exception { final Charset ASCII = Charset.forName("ASCII"); // Calculate the expected credential String expectedRaw = "daniel:officefloor:password"; MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(expectedRaw.getBytes(ASCII)); byte[] expectedBytes = digest.digest(); String expectedCredentials = Base64.encodeBase64String(expectedBytes).trim(); // Obtain the context DirContext context = this.ldap.getDirContext(); // Obtain the People context DirContext people = (DirContext) context.lookup("ou=People,dc=officefloor,dc=net"); assertNotNull("Should have People context", people); // Search for person NamingEnumeration<SearchResult> results = people.search("", "(&(objectClass=inetOrgPerson)(uid=daniel))", null); assertTrue("Expecting to find daniel entry", results.hasMore()); SearchResult result = results.next(); assertFalse("Should only have the daniel entry", results.hasMore()); // Obtain the digest MD5 credentials for Daniel String digestMd5Credential = null; Attributes attributes = result.getAttributes(); Attribute passwordAttribute = attributes.get("userPassword"); for (NamingEnumeration<?> enumeration = passwordAttribute.getAll(); enumeration.hasMore();) { byte[] credentials = (byte[]) enumeration.next(); String text = new String(credentials, ASCII); // Determine if MD5 credential if (text.toUpperCase().startsWith("{MD5}")) { // Found MD5 credential digestMd5Credential = text.substring("{MD5}".length()); } } assertNotNull("Must have digest MD5 credential", digestMd5Credential); // Ensure correct credentials assertEquals("Incorrect DIGEST MD5 credentials", expectedCredentials, digestMd5Credential); }
From source file:org.pepstock.jem.gwt.server.security.ExtendedJndiLdapRealm.java
/** * Extract from LDAP all configured attributes. * /*from ww w . j ava 2s . c o m*/ * @param id user id * @param environment LDAP environment * @return list of principal attributes */ public List<PrincipalAttribute> search(String id, Hashtable<String, String> environment) { // checks if attributes are set if (attributes != null && attributes.length > 0) { ctls.setReturningAttributes(attributes); } // if no attributes, uses UID by default if (ctls.getReturningAttributes() == null) { ctls.setReturningAttributes(new String[] { UID }); } // uses useDN for searching String userDn = super.getUserDnTemplate(); String ldapUserContext = StringUtils.substringAfter(userDn, ","); try { // gets initial context InitialDirContext ctx = new InitialDirContext(environment); // creates search string String filter = MessageFormat.format("(uid={0})", new Object[] { id }); // searchs! Object obj = ctx.search(ldapUserContext, filter, ctls); // scans all attributes and load into a Principal Attribute @SuppressWarnings("rawtypes") NamingEnumeration userEnum = (NamingEnumeration) obj; if (userEnum != null && userEnum.hasMore()) { SearchResult result = (SearchResult) userEnum.next(); return loadAttributes(id, result.getAttributes()); } } catch (NamingException ne) { LogAppl.getInstance().emit(UserInterfaceMessage.JEMG031E, ne, id); } return new ArrayList<PrincipalAttribute>(); }
From source file:alpine.auth.LdapConnectionWrapper.java
/** * Retrieves an attribute by its name for the specified search result. * @param result the search result of the entry to obtain the attribute value for * @param attributeName the name of the attribute to return * @return the value of the attribute, or null if not found * @throws NamingException if an exception is thrown * @since 1.4.0/* ww w . j a va 2 s .c om*/ */ public String getAttribute(SearchResult result, String attributeName) throws NamingException { return getAttribute(result.getAttributes(), attributeName); }
From source file:org.mule.module.ldap.api.jndi.SimpleLDAPResultSet.java
/** * /*from w w w . j ava2 s . c om*/ * @return * @throws LDAPException * @see org.mule.module.ldap.api.LDAPResultSet#next() */ @Override public LDAPEntry next() throws LDAPException { SearchResult searchResult = (SearchResult) this.entries.nextElement(); String entryDn; if (searchResult != null) { entryDn = searchResult.getName(); if (searchResult.isRelative()) { entryDn += "," + baseDn; } return LDAPJNDIUtils.buildEntry(entryDn, searchResult.getAttributes()); } else { throw new NoSuchElementException(); } }
From source file:org.sipfoundry.sipxconfig.bulk.ldap.LdapRowInserterTest.java
public void testCheckRowData() throws Exception { IMocksControl control = org.easymock.classextension.EasyMock.createNiceControl(); UserMapper userMapper = control.createMock(UserMapper.class); SearchResult searchResult = control.createMock(SearchResult.class); Attributes attributes = control.createMock(Attributes.class); Attribute attribute = control.createMock(Attribute.class); searchResult.getAttributes(); control.andReturn(attributes);// w w w. j a v a 2 s. c o m attributes.get("identity"); control.andReturn(null); control.replay(); m_rowInserter.setUserMapper(userMapper); assertEquals(RowStatus.FAILURE, m_rowInserter.checkRowData(searchResult).getRowStatus()); control.reset(); searchResult.getAttributes(); control.andReturn(attributes); attributes.get("identity"); control.andReturn(attribute); userMapper.getUserName(attributes); control.andReturn("@McQueen"); userMapper.getGroupNames(searchResult); control.andReturn(Collections.singleton(SALES)); control.replay(); m_rowInserter.setUserMapper(userMapper); assertEquals(RowStatus.FAILURE, m_rowInserter.checkRowData(searchResult).getRowStatus()); control.reset(); searchResult.getAttributes(); control.andReturn(attributes); attributes.get("identity"); control.andReturn(attribute); userMapper.getUserName(attributes); control.andReturn("McQueen"); userMapper.getGroupNames(searchResult); control.andReturn(Collections.singleton(SALES)); control.replay(); m_rowInserter.setUserMapper(userMapper); assertEquals(RowStatus.SUCCESS, m_rowInserter.checkRowData(searchResult).getRowStatus()); }
From source file:org.gbif.portal.registration.LDAPUtils.java
/** * Gets the common, phone and email for the * @param uid To use for searching in LDAP * @return An array containing the 3 strings * @throws NamingException On error//from w w w. ja v a2 s .c o m */ @SuppressWarnings("unchecked") public List<UserLogin> getUsernamePasswordForEmail(String email) throws NamingException { DirContext ctx = getUserContext(); NamingEnumeration searchResults = ctx.search("", "mail=" + email, null, new SearchControls()); List<UserLogin> uls = new ArrayList<UserLogin>(); while (searchResults.hasMore()) { SearchResult sr = (SearchResult) searchResults.next(); Attributes attributes = sr.getAttributes(); debugAttributes(attributes); UserLogin ul = new UserLogin(); ul.setSurname((String) attributes.get("sn").get()); ul.setFirstName((String) attributes.get("givenName").get()); ul.setEmail((String) attributes.get("mail").get()); ul.setUsername((String) attributes.get("uid").get()); uls.add(ul); } return uls; }
From source file:org.springframework.ldap.samples.article.dao.TraditionalPersonDaoImpl.java
public List getAllPersonNames() { DirContext ctx = createAnonymousContext(); LinkedList list = new LinkedList(); NamingEnumeration results = null; try {/*from w ww. java2 s. com*/ SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); results = ctx.search("", "(objectclass=person)", controls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); Attributes attributes = searchResult.getAttributes(); Attribute attr = attributes.get("cn"); String cn = (String) attr.get(); list.add(cn); } } catch (NamingException e) { throw new RuntimeException(e); } finally { if (results != null) { try { results.close(); } catch (Exception e) { // Never mind this. } } if (ctx != null) { try { ctx.close(); } catch (Exception e) { // Never mind this. } } } return list; }
From source file:org.springframework.ldap.demo.dao.PersonDaoImpl.java
public List<String> getAllPersonNames() { DirContext ctx = createAnonymousContext(); LinkedList<String> list = new LinkedList<String>(); NamingEnumeration<?> results = null; try {/*from w ww. j a va2 s. c o m*/ SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); results = ctx.search("", "(objectclass=person)", controls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); Attributes attributes = searchResult.getAttributes(); Attribute attr = attributes.get("cn"); String cn = (String) attr.get(); list.add(cn); } } catch (NamingException e) { throw new RuntimeException(e); } finally { if (results != null) { try { results.close(); } catch (Exception e) { // Never mind this. } } if (ctx != null) { try { ctx.close(); } catch (Exception e) { // Never mind this. } } } return list; }