use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.
the class BySearchResultEntrySearcher method doSearch.
public void doSearch(ZLdapFilter filter, Set<ObjectType> types) throws ServiceException {
String[] bases = prov.getSearchBases(domain, types);
for (String base : bases) {
try {
ZSearchControls ctrl = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
ZSearchResultEnumeration results = prov.getHelper().searchDir(base, filter, ctrl, zlc, LdapServerType.REPLICA);
while (results.hasMore()) {
ZSearchResultEntry sr = results.next();
visitor.processSearchEntry(sr);
}
results.close();
} catch (ServiceException e) {
ZimbraLog.search.debug("Unexpected exception whilst searching", e);
}
}
}
use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.
the class LdapExample method search.
public void search() throws Exception {
String base = "cn=servers,cn=zimbra";
String filter = "(objectClass=zimbraServer)";
String[] returnAttrs = new String[] { "objectClass", "cn" };
ZLdapFilter zFilter = ZLdapFilterFactory.getInstance().fromFilterString(FilterId.ZMCONFIGD, filter);
ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
GenericLdapConfig ldapConfig = getLdapConfig();
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(ldapConfig, LdapUsage.SEARCH);
ZSearchResultEnumeration ne = zlc.searchDir(base, zFilter, searchControls);
while (ne.hasMore()) {
ZSearchResultEntry entry = ne.next();
String dn = entry.getDN();
ZAttributes attrs = entry.getAttributes();
String cn = attrs.getAttrString("cn");
String[] objectClasses = attrs.getMultiAttrString("objectClass");
System.out.println("dn = " + dn);
System.out.println("cn = " + cn);
for (String objectClass : objectClasses) {
System.out.println("objetClass = " + objectClass);
}
}
ne.close();
} catch (LdapSizeLimitExceededException e) {
e.printStackTrace();
throw e;
} finally {
// Note: this is important!!
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.
the class TestLdapHelper method searchDir.
@Test
public void searchDir() throws Exception {
LdapDIT dit = prov.getDIT();
String base = dit.configBranchBaseDN();
ZLdapFilter filter = filterFactory.anyEntry();
String[] returnAttrs = new String[] { "objectClass" };
ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_ONELEVEL, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
ZSearchResultEnumeration ne = ldapHelper.searchDir(base, filter, searchControls);
Set<String> expected = new HashSet<String>();
expected.add(dit.adminBaseDN());
expected.add(dit.appAdminBaseDN());
expected.add(dit.zimletBaseDN());
expected.add(dit.cosBaseDN());
expected.add(dit.globalDynamicGroupBaseDN());
expected.add(dit.serverBaseDN());
expected.add(dit.xmppcomponentBaseDN());
expected.add(dit.globalGrantDN());
expected.add(dit.configDN());
expected.add(dit.shareLocatorBaseDN());
expected.add(dit.ucServiceBaseDN());
int numFound = 0;
while (ne.hasMore()) {
ZSearchResultEntry sr = ne.next();
assertTrue(expected.contains(sr.getDN()));
numFound++;
}
ne.close();
assertEquals(expected.size(), numFound);
}
use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.
the class LdapProvisioning method isEmptyOu.
/**
* @param habOrgUnitName organizational unit name
* @param domainDn the domain distinguishedd name
* @return true if the ou has groups or false if empty
* @throws ServiceException
*/
private static boolean isEmptyOu(String habOrgUnitName, String domainDn) throws ServiceException {
ZLdapContext zlc = null;
boolean empty = false;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_OU);
String baseDN = createOuDn(habOrgUnitName, domainDn);
String filter = "(objectClass=zimbraDistributionList)";
String[] returnAttrs = new String[] { "cn" };
ZLdapFilter zFilter = ZLdapFilterFactory.getInstance().fromFilterString(FilterId.ALL_DISTRIBUTION_LISTS, filter);
ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
ZSearchResultEnumeration ne = zlc.searchDir(baseDN, zFilter, searchControls);
empty = !ne.hasMore();
} catch (ServiceException e) {
throw ServiceException.FAILURE(String.format("Unable to delete HAB org unit: %s for domain=%s", habOrgUnitName, domainDn), e);
} finally {
LdapClient.closeContext(zlc);
}
return empty;
}
use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.
the class LdapProvisioning method getAllHabOrgUnitInADomain.
/**
* @param domain domain for which HAB org unit list is requested
* @return HAB org unit list under the given domain
* @throws ServiceException
*/
public Set<String> getAllHabOrgUnitInADomain(Domain domain) throws ServiceException {
ZLdapContext zlc = null;
Set<String> habList = new HashSet<String>();
try {
String domainDn = ((LdapEntry) domain).getDN();
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_OU);
String filter = "(objectClass=organizationalUnit)";
String[] returnAttrs = new String[] { "ou" };
ZLdapFilter zFilter = ZLdapFilterFactory.getInstance().fromFilterString(FilterId.ANY_ENTRY, filter);
ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
ZSearchResultEnumeration ne = zlc.searchDir(domainDn, zFilter, searchControls);
while (ne.hasMore()) {
habList.add(ne.next().getAttributes().getAttrString("ou"));
}
ZimbraLog.misc.debug("The HAB orgunits under:%s, are: (%s)", domain.getName(), habList);
} catch (ServiceException e) {
throw ServiceException.FAILURE(String.format("Unable to delete HAB org unit: %s for domain=%s", domain.getName(), domain.getName()), e);
} finally {
LdapClient.closeContext(zlc);
}
return habList;
}
Aggregations