List of usage examples for javax.naming.directory SearchResult getNameInNamespace
public String getNameInNamespace()
From source file:RetrievingLdapName.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {//w ww .j av a 2 s. c o m // Create initial context DirContext ctx = new InitialDirContext(env); NamingEnumeration answer = ctx.search("ou=People", null); // Print the answer while (answer.hasMore()) { SearchResult sr = (SearchResult) answer.next(); String name = sr.getNameInNamespace(); System.out.println(name); LdapName dn = new LdapName(name); // do something with the dn } // Close the context when we're done ctx.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:FullName.java
public static void printSearchEnumeration(NamingEnumeration retEnum) { try {// w w w . ja va 2 s .c o m while (retEnum.hasMore()) { SearchResult sr = (SearchResult) retEnum.next(); System.out.println(">>" + sr.getNameInNamespace()); } } catch (NamingException e) { e.printStackTrace(); } }
From source file:org.ballerinalang.auth.ldap.util.LdapUtils.java
/** * Searches the corresponding name for a given username from LDAP. * * @param userName Given username * @param searchBase LDAP search base * @param searchFilter LDAP search filter * @param dirContext Directory naming context * @return Associated name for the given username * @throws UserStoreException if there is any exception occurs during the process * @throws NamingException if there is any exception occurs during the process *//*from w w w . j ava 2 s . c o m*/ public static String getNameInSpaceForUserName(String userName, String searchBase, String searchFilter, DirContext dirContext) throws UserStoreException, NamingException { if (userName == null) { throw new UserStoreException("userName value is null."); } String userDN = null; NamingEnumeration<SearchResult> answer = null; try { SearchControls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); String[] searchBases = searchBase.split("#"); for (String base : searchBases) { answer = dirContext.search(escapeDNForSearch(base), searchFilter, searchCtls); if (!(answer.hasMore())) { continue; } SearchResult userObj = answer.next(); if (userObj != null) { //no need to decode since , if decoded the whole string, can't be encoded again //eg CN=Hello\,Ok=test\,test, OU=Industry userDN = userObj.getNameInNamespace(); break; } } if (LOG.isDebugEnabled()) { LOG.debug("Name in space for " + userName + " is " + userDN); } } finally { LdapUtils.closeNamingEnumeration(answer); } return userDN; }
From source file:org.apache.directory.studio.ldapbrowser.core.jobs.ExportDsmlRunnable.java
/** * Converts the given {@link SearchResult} to a {@link SearchResultEntryDsml}. * * @param searchResult the search result * @return the associated search result entry DSML * @throws org.apache.directory.api.ldap.model.exception.LdapException */// w w w .j a v a 2 s . c o m private static DsmlDecorator<? extends Response> convertSearchResultToDsml(SearchResult searchResult) throws LdapException { Entry entry = AttributeUtils.toEntry(searchResult.getAttributes(), new Dn(searchResult.getNameInNamespace())); if (isReferral(entry)) { // The search result is a referral SearchResultReferenceDsml srr = new SearchResultReferenceDsml(codec); // Getting the 'ref' attribute Attribute refAttribute = entry.get(ExportDsmlRunnable.REF_ATTRIBUTETYPE_NAME); if (refAttribute == null) { // If we did not get it by its name, let's get it by its OID refAttribute = entry.get(ExportDsmlRunnable.REF_ATTRIBUTETYPE_OID); } // Adding references if (refAttribute != null) { for (Value value : refAttribute) { srr.addSearchResultReference(new LdapUrl((String) value.getValue())); } } return srr; } else { // The search result is NOT a referral SearchResultEntryDsml sre = new SearchResultEntryDsml(codec); sre.setEntry(entry); return sre; } }
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; if (answer.hasMoreElements()) { searchResult = (SearchResult) answer.next(); } else {/*from ww w . j av a2 s. co m*/ 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:it.infn.ct.security.utilities.LDAPUtils.java
public static String getOrgDN(String organisation, String countryCode) { NamingEnumeration results = null; DirContext ctx = null;/*w w w . j av a 2s. c o m*/ String dn = null; try { ctx = getContext(); SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); String retAttrs[] = { "dn" }; controls.setReturningAttributes(retAttrs); ResourceBundle rb = ResourceBundle.getBundle("ldap"); results = ctx.search("c=" + countryCode + "," + rb.getString("organisationsRoot"), "(&(objectclass=organization)(o=" + organisation + "))", controls); if (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); dn = searchResult.getNameInNamespace(); } } catch (NameNotFoundException ex) { _log.error(ex); } 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 dn; }
From source file:org.lsc.beans.LscBean.java
/** * Set a bean from an LDAP entry/*from w w w.j av a2 s.c om*/ * * @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:com.adito.activedirectory.AbstractPagedResultMapper.java
public final void processSearchResultException(SearchResult searchResult, Exception e) { String dn = searchResult.getNameInNamespace(); logger.error("Problem retrieving principal " + dn, e); }
From source file:org.archone.ad.dao.CommonDao.java
public List<ShoadUser> listUsers(String domain) throws NamingException { List<ShoadUser> users = new LinkedList<ShoadUser>(); NamingEnumeration<SearchResult> searchResults = dirContext.search(nameConvertor.getUsersBaseDn(domain), "(uid=*)", defaultSearchControls); while (searchResults.hasMore()) { SearchResult sr = searchResults.next(); ShoadUser shoadUser = (ShoadUser) this.dirContext.lookup(sr.getNameInNamespace()); users.add(shoadUser);//from w w w .ja v a 2 s . c o m } return users; }
From source file:it.infn.ct.security.utilities.LDAPUtils.java
public static List<Organization> getOrgList(String country) { List<Organization> OrgList = new ArrayList<Organization>(); NamingEnumeration resultCountries = null; DirContext ctx = null;// w w w. j a v a 2 s . c o m try { ctx = getContext(); SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); ResourceBundle rb = ResourceBundle.getBundle("ldap"); String filter; if (country == null) { filter = "(objectclass=country)"; } else { filter = "(&(objectclass=country)(c=" + country + "))"; } resultCountries = ctx.search(rb.getString("organisationsRoot"), filter, controls); while (resultCountries.hasMore()) { SearchResult searchResult = (SearchResult) resultCountries.next(); Attributes attributes = searchResult.getAttributes(); String countryCode = (String) attributes.get("c").get(); String countryName = (String) attributes.get("co").get(); NamingEnumeration resultsOrgs = ctx.search( "c=" + countryCode + "," + rb.getString("organisationsRoot"), "(objectclass=organization)", controls); while (resultsOrgs.hasMore()) { SearchResult srOrg = (SearchResult) resultsOrgs.next(); Attributes orgAttrs = srOrg.getAttributes(); String description = ""; if ((orgAttrs.get("description")) != null) { description = (String) orgAttrs.get("description").get(); } OrgList.add(new Organization((String) orgAttrs.get("o").get(), countryName, countryCode, description, srOrg.getNameInNamespace())); } resultsOrgs.close(); } } catch (NameNotFoundException ex) { _log.error(ex); } catch (NamingException e) { throw new RuntimeException(e); } finally { if (resultCountries != null) { try { resultCountries.close(); } catch (Exception e) { // Never mind this. } } if (ctx != null) { try { ctx.close(); } catch (Exception e) { // Never mind this. } } } Collections.sort(OrgList, new Comparator<Organization>() { public int compare(Organization o1, Organization o2) { return o1.getKey().compareTo(o2.getKey()); } }); return OrgList; }