List of usage examples for javax.naming.directory SearchResult getName
public String getName()
From source file:org.jkcsoft.java.util.JndiHelper.java
public static void logLdap(Log plog, int level, int nth, Object dirEntry) throws NamingException { try {/*from w w w.ja va 2 s .c om*/ if (dirEntry instanceof NamingEnumeration) { NamingEnumeration nameEnum = (NamingEnumeration) dirEntry; JndiHelper.logLevel(plog, level, nth, "Naming Enumeration: " + nameEnum); try { int nthThis = 0; List nameList = new Vector(Collections.list(nameEnum)); Collections.sort(nameList, new Comparator() { public int compare(Object o1, Object o2) { if (o1 instanceof Attribute) { return String.CASE_INSENSITIVE_ORDER.compare(((Attribute) o1).getID(), ((Attribute) o2).getID()); } return 0; } }); Iterator nameIter = nameList.iterator(); while (nameIter.hasNext()) { logLdap(plog, level + 1, nthThis++, nameIter.next()); } } catch (NamingException ex) { plog.error("Exception iterating thru NamingEnumeration: " + ex.getMessage()); } } else if (dirEntry instanceof Attribute) { Attribute dirAttr = (Attribute) dirEntry; JndiHelper.logLevel(plog, level, nth, "Attribute: [" + dirAttr + "]"); } else if (dirEntry instanceof DirContext) { DirContext lctx = (DirContext) dirEntry; JndiHelper.logLevel(plog, level, nth, "LDAP Context: DN [" + lctx.getNameInNamespace() + "]" + " Attributes ==>"); logLdap(plog, level, nth, lctx.getAttributes("").getAll()); } else if (dirEntry instanceof SearchResult) { SearchResult sr = (SearchResult) dirEntry; JndiHelper.logLevel(plog, level, nth, "SearchResult: ClassName of Bound Object [" + sr.getClassName() + "]" + " Name: [" + sr.getName() + "]" + " Bound Object ==>"); // sr.s logLdap(plog, level, nth, sr.getObject()); logLdap(plog, level, nth, sr.getAttributes().getAll()); } else { JndiHelper.logLevel(plog, level, nth, "(?) class of entry: [" + dirEntry + "]"); } nth++; } catch (NamingException e1) { plog.error("Naming Exception (will try to continue): " + e1.getMessage()); } }
From source file:org.josso.gateway.identity.service.store.ldap.LDAPIdentityStore.java
/** * Fetches the supplied user DN./*from w ww . j a v a 2s. c om*/ * * @param uid the user id * @return the user DN for the supplied uid * @throws NamingException LDAP error obtaining user information. * @throws IOException */ protected String selectUserDN(String uid) throws NamingException, IOException { String dn = null; InitialLdapContext ctx = createLdapInitialContext(false); StartTlsResponse tls = null; if (getEnableStartTls()) { tls = startTls(ctx); } String principalUidAttrName = this.getPrincipalUidAttributeID(); String usersCtxDN = this.getUsersCtxDN(); try { // NamingEnumeration answer = ctx.search(usersCtxDN, matchAttrs, principalAttr); // This gives more control over search behavior : NamingEnumeration answer = ctx.search(usersCtxDN, "(&(" + principalUidAttrName + "=" + uid + "))", getSearchControls()); while (answer.hasMore()) { SearchResult sr = (SearchResult) answer.next(); Attributes attrs = sr.getAttributes(); Attribute uidAttr = attrs.get(principalUidAttrName); if (uidAttr == null) { logger.warn("Invalid user uid attribute '" + principalUidAttrName + "'"); continue; } String uidValue = uidAttr.get().toString(); if (uidValue != null) { dn = sr.getName() + "," + usersCtxDN; if (logger.isDebugEnabled()) logger.debug("Found user '" + principalUidAttrName + "=" + uidValue + "' for user '" + uid + "' DN=" + dn); } else { if (logger.isDebugEnabled()) logger.debug("User not found for user '" + uid + "'"); } } } catch (NamingException e) { if (logger.isDebugEnabled()) logger.debug("Failed to locate user", e); } finally { // Close the context to release the connection if (tls != null) { tls.close(); } ctx.close(); } return dn; }
From source file:org.jsecurity.realm.activedirectory.ActiveDirectoryRealm.java
private Set<String> getRoleNamesForUser(String username, LdapContext ldapContext) throws NamingException { Set<String> roleNames; roleNames = new LinkedHashSet<String>(); SearchControls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); String userPrincipalName = username; if (principalSuffix != null) { userPrincipalName += principalSuffix; }//from w ww .ja va 2 s . co m String searchFilter = "(&(objectClass=*)(userPrincipalName=" + userPrincipalName + "))"; NamingEnumeration answer = ldapContext.search(searchBase, searchFilter, searchCtls); while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); if (log.isDebugEnabled()) { log.debug("Retrieving group names for user [" + sr.getName() + "]"); } Attributes attrs = sr.getAttributes(); if (attrs != null) { NamingEnumeration ae = attrs.getAll(); while (ae.hasMore()) { Attribute attr = (Attribute) ae.next(); if (attr.getID().equals("memberOf")) { Collection<String> groupNames = LdapUtils.getAllAttributeValues(attr); if (log.isDebugEnabled()) { log.debug("Groups found for user [" + username + "]: " + groupNames); } Collection<String> rolesForGroups = getRoleNamesForGroups(groupNames); roleNames.addAll(rolesForGroups); } } } } return roleNames; }
From source file:org.kitodo.production.services.data.LdapServerService.java
/** * Check if User already exists on system. * * @param user/*from ww w. j a v a 2 s.c o m*/ * The User. * @return result as boolean */ public boolean isUserAlreadyExists(User user) { Hashtable<String, String> ldapEnvironment = initializeWithLdapConnectionSettings( user.getLdapGroup().getLdapServer()); DirContext ctx; boolean result = false; try { ctx = new InitialDirContext(ldapEnvironment); Attributes matchAttrs = new BasicAttributes(true); NamingEnumeration<SearchResult> answer = ctx.search(buildUserDN(user), matchAttrs); result = answer.hasMoreElements(); while (answer.hasMore()) { SearchResult sr = answer.next(); logger.debug(">>>{}", sr.getName()); Attributes attrs = sr.getAttributes(); String givenName = getStringForAttribute(attrs, "givenName"); String surName = getStringForAttribute(attrs, "sn"); String mail = getStringForAttribute(attrs, "mail"); String cn = getStringForAttribute(attrs, "cn"); String homeDirectory = getStringForAttribute(attrs, "homeDirectory"); logger.debug(givenName); logger.debug(surName); logger.debug(mail); logger.debug(cn); logger.debug(homeDirectory); } ctx.close(); } catch (NamingException e) { logger.error(e.getMessage(), e); } return result; }
From source file:org.lsc.beans.LscBean.java
/** * Set a bean from an LDAP entry// w w w. j av a 2 s . c o m * * @param entry * the LDAP entry * @param baseDn * the base Dn used to set the right Dn * @param c * class to instantiate * @return the bean * @throws NamingException * thrown if a directory exception is encountered while looking * at the entry */ public static LscBean getInstance(final SearchResult entry, final String baseDn, final Class<?> c) throws NamingException { try { if (entry != null) { LscBean ab = (LscBean) c.newInstance(); String dn = entry.getName(); if ((dn.length() > 0) && (dn.charAt(0) == '"') && (dn.charAt(dn.length() - 1) == '"')) { dn = dn.substring(1, dn.length() - 1); } if (dn.startsWith("ldap://")) { ab.setDistinguishName(entry.getNameInNamespace()); } else { // Manually concat baseDn because getNameInNamespace returns // a differently escaped DN, causing LSC to detect a MODRDN if ((baseDn != null) && (baseDn.length() > 0)) { if (dn.length() > 0) { ab.setDistinguishName(dn + "," + baseDn); } else { ab.setDistinguishName(baseDn); } } else { ab.setDistinguishName(dn); } } NamingEnumeration<?> ne = entry.getAttributes().getAll(); while (ne.hasMore()) { ab.setAttribute((Attribute) ne.next()); } return ab; } else { return null; } } catch (InstantiationException ie) { LOGGER.error(ie.toString()); LOGGER.debug(ie.toString(), ie); } catch (IllegalAccessException iae) { LOGGER.error(iae.toString()); LOGGER.debug(iae.toString(), iae); } return null; }
From source file:org.lsc.jndi.JndiServices.java
private void doDeleteChildrenRecursively(String distinguishName) throws NamingException { SearchControls sc = new SearchControls(); sc.setSearchScope(SearchControls.ONELEVEL_SCOPE); NamingEnumeration<SearchResult> ne = ctx.search(distinguishName, DEFAULT_FILTER, sc); while (ne.hasMore()) { SearchResult sr = (SearchResult) ne.next(); String childrenDn = rewriteBase(sr.getName() + "," + distinguishName); deleteChildrenRecursively(childrenDn); }/*from ww w. ja va 2s . c o m*/ ctx.destroySubcontext(new LdapName(distinguishName)); }
From source file:org.mule.module.ldap.api.jndi.PagedLDAPResultSet.java
/** * @return//from www . j a v a 2 s . co m * @throws LDAPException * @see org.mule.module.ldap.api.LDAPResultSet#next() */ @Override public LDAPEntry next() throws LDAPException { if (hasNext()) // Force navigating to next page { 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()); } } throw new NoSuchElementException(); }
From source file:org.mule.module.ldap.api.jndi.SimpleLDAPResultSet.java
/** * //ww w . jav a 2s. co m * @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.nuxeo.ecm.directory.ldap.ExternalLDAPDirectoryFeature.java
protected void destroyRecursively(String dn, DirContext ctx, int limit) throws NamingException { if (limit == 0) { log.warn("Reach recursion limit, stopping deletion at" + dn); return;/*from www . ja v a 2s. c o m*/ } SearchControls scts = new SearchControls(); scts.setSearchScope(SearchControls.ONELEVEL_SCOPE); NamingEnumeration<SearchResult> children = ctx.search(dn, "(objectClass=*)", scts); try { while (children.hasMore()) { SearchResult child = children.next(); String subDn = child.getName(); subDn = subDn + ',' + dn; destroyRecursively(subDn, ctx, limit); } } catch (SizeLimitExceededException e) { log.warn("SizeLimitExceededException: trying again on partial results " + dn); if (limit == -1) { limit = 100; } destroyRecursively(dn, ctx, limit - 1); } ctx.destroySubcontext(dn); }
From source file:org.nuxeo.ecm.directory.ldap.LDAPDirectoryTestCase.java
protected void destroyRecursively(String dn, DirContext ctx, int limit) throws NamingException { if (limit == 0) { log.warn("Reach recursion limit, stopping deletion at" + dn); return;/* w w w.j a v a 2 s. c o m*/ } SearchControls scts = new SearchControls(); scts.setSearchScope(SearchControls.ONELEVEL_SCOPE); String providerUrl = (String) ctx.getEnvironment().get(Context.PROVIDER_URL); NamingEnumeration<SearchResult> children = ctx.search(dn, "(objectClass=*)", scts); try { while (children.hasMore()) { SearchResult child = children.next(); String subDn = child.getName(); if (!USE_EXTERNAL_TEST_LDAP_SERVER && subDn.endsWith(providerUrl)) { subDn = subDn.substring(0, subDn.length() - providerUrl.length() - 1); } else { subDn = subDn + ',' + dn; } destroyRecursively(subDn, ctx, limit); } } catch (SizeLimitExceededException e) { log.warn("SizeLimitExceededException: trying again on partial results " + dn); if (limit == -1) { limit = 100; } destroyRecursively(dn, ctx, limit - 1); } ctx.destroySubcontext(dn); }