List of usage examples for javax.naming NamingEnumeration next
public T next() throws NamingException;
From source file:org.springframework.ldap.test.unboundid.LdapTestUtils.java
/** * Clear the directory sub-tree starting with the node represented by the * supplied distinguished name./*from ww w .j av a 2s. co m*/ * * @param ctx The DirContext to use for cleaning the tree. * @param name the distinguished name of the root node. * @throws NamingException if anything goes wrong removing the sub-tree. */ public static void clearSubContexts(DirContext ctx, Name name) throws NamingException { NamingEnumeration<?> enumeration = null; try { enumeration = ctx.listBindings(name); while (enumeration.hasMore()) { Binding element = (Binding) enumeration.next(); Name childName = LdapUtils.newLdapName(element.getName()); childName = LdapUtils.prepend(childName, name); try { ctx.unbind(childName); } catch (ContextNotEmptyException e) { clearSubContexts(ctx, childName); ctx.unbind(childName); } } } catch (NamingException e) { LOGGER.debug("Error cleaning sub-contexts", e); } finally { try { enumeration.close(); } catch (Exception e) { // Never mind this } } }
From source file:org.pentaho.di.trans.steps.mailvalidator.MailValidation.java
private static ArrayList<String> getMX(String hostName) throws NamingException { // Perform a DNS lookup for MX records in the domain Hashtable<String, String> env = new Hashtable<String, String>(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); DirContext ictx = new InitialDirContext(env); Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" }); Attribute attr = attrs.get("MX"); // if we don't have an MX record, try the machine itself if ((attr == null) || (attr.size() == 0)) { attrs = ictx.getAttributes(hostName, new String[] { "A" }); attr = attrs.get("A"); if (attr == null) { throw new NamingException(BaseMessages.getString(PKG, "MailValidator.NoMatchName", hostName)); }//from w w w . ja v a2 s.c o m } // Huzzah! we have machines to try. Return them as an array list // NOTE: We SHOULD take the preference into account to be absolutely // correct. This is left as an exercise for anyone who cares. ArrayList<String> res = new ArrayList<String>(); NamingEnumeration<?> en = attr.getAll(); while (en.hasMore()) { String x = (String) en.next(); String[] f = x.split(" "); if (f[1].endsWith(".")) { f[1] = f[1].substring(0, (f[1].length() - 1)); } res.add(f[1]); } return res; }
From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java
public static final Attributes getAttributes(NamingEnumeration<SearchResult> result) throws NamingException { if (result == null) return null; if (!result.hasMore()) return null; SearchResult rs = (SearchResult) result.next(); return rs.getAttributes(); }
From source file:org.projectforge.business.ldap.LdapUtils.java
public static String[] getAttributeStringValues(final Attributes attributes, final String attrId) throws NamingException { final Attribute attr = attributes.get(attrId); if (attr == null) { return null; }//from www. j a va 2 s.c o m final NamingEnumeration<?> enumeration = attr.getAll(); final List<String> list = new ArrayList<String>(); while (enumeration.hasMore() == true) { final Object attrValue = enumeration.next(); if (attrValue == null) { list.add(null); } list.add(String.valueOf(attrValue)); } return list.toArray(new String[list.size()]); }
From source file:com.ktds.ldap.populator.LdapTestUtils.java
/** * Clear the directory sub-tree starting with the node represented by the * supplied distinguished name./*from w ww.j a v a 2s. c o m*/ * * @param ctx The DirContext to use for cleaning the tree. * @param name the distinguished name of the root node. * @throws NamingException if anything goes wrong removing the sub-tree. */ public static void clearSubContexts(DirContext ctx, Name name) throws NamingException { NamingEnumeration enumeration = null; try { enumeration = ctx.listBindings(name); while (enumeration.hasMore()) { Binding element = (Binding) enumeration.next(); Name childName = LdapUtils.newLdapName(element.getName()); childName = LdapUtils.prepend(childName, name); try { ctx.unbind(childName); } catch (ContextNotEmptyException e) { clearSubContexts(ctx, childName); ctx.unbind(childName); } } } catch (NamingException e) { LOGGER.debug("Error cleaning sub-contexts", e); } finally { try { enumeration.close(); } catch (Exception e) { // Never mind this } } }
From source file:org.geoserver.security.ldap.LDAPTestUtils.java
/** * Clear the directory sub-tree starting with the node represented by the * supplied distinguished name./*from w w w. j av a2 s . c o m*/ * * @param ctx The DirContext to use for cleaning the tree. * @param name the distinguished name of the root node. * @throws NamingException if anything goes wrong removing the sub-tree. */ public static void clearSubContexts(DirContext ctx, Name name) throws NamingException { NamingEnumeration enumeration = null; try { enumeration = ctx.listBindings(name); while (enumeration.hasMore()) { Binding element = (Binding) enumeration.next(); DistinguishedName childName = new DistinguishedName(element.getName()); childName.prepend((DistinguishedName) name); try { ctx.destroySubcontext(childName); } catch (ContextNotEmptyException e) { clearSubContexts(ctx, childName); ctx.destroySubcontext(childName); } } } catch (NamingException e) { e.printStackTrace(); } finally { try { enumeration.close(); } catch (Exception e) { // Never mind this } } }
From source file:org.wso2.carbon.appfactory.userstore.internal.OTLDAPUtil.java
public static String getUserIdFromEmail(String email, LDAPConnectionContext connectionSource, String userSearchBase) throws UserStoreException { // if it is not an email, just return it as the uid. if (!email.contains("@")) { return email; }//from ww w.j av a2s .co m // check from cache String userId = otUserIdCache.getValueFromCache(email); if (userId != null && !userId.isEmpty()) { return userId; } // check from ldap and update the cache StringBuffer buff = new StringBuffer(); buff.append("(&(objectClass=inetOrgPerson)(mail=").append(email).append("))"); if (log.isDebugEnabled()) { log.debug("Searching for " + buff.toString()); } DirContext dirContext = connectionSource.getContext(); NamingEnumeration<SearchResult> answer = null; try { String name = null; answer = searchForUser(buff.toString(), null, 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) { name = userObj.getName(); if (name != null) { name = name.replaceFirst("uid=", ""); } } otUserIdCache.addToCache(email, name); return name; } catch (Exception e) { log.error(e.getMessage(), e); throw new UserStoreException(e.getMessage(), e); } finally { JNDIUtil.closeNamingEnumeration(answer); JNDIUtil.closeContext(dirContext); } }
From source file:no.smint.anthropos.ldap.LDAP.java
public static String getDn(String uid) throws NamingException { Hashtable<String, Object> env = config(); DirContext ctx = new InitialDirContext(env); SearchControls ctls = new SearchControls(); String filter = ("uid=" + uid); NamingEnumeration answer = ctx.search(name, filter, ctls); SearchResult searchResult;//from w w w .ja v a2s . c o m if (answer.hasMoreElements()) { searchResult = (SearchResult) answer.next(); } else { System.out.println("Found no user by that name"); return null; } if (answer.hasMoreElements()) { System.err.println("Matched mutliple users for the uid" + uid); return null; } ctx.close(); return searchResult.getNameInNamespace(); }
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."); }/*from w ww. ja v a 2s . 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 w w . j a v a 2s . co 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); } }