use of com.zimbra.cs.ldap.ZSearchControls 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.ZSearchControls in project zm-mailbox by Zimbra.
the class ZLdapHelper method searchForEntry.
@Override
@TODOEXCEPTIONMAPPING
public ZSearchResultEntry searchForEntry(String base, ZLdapFilter filter, ZLdapContext initZlc, boolean useMaster, String[] returnAttrs) throws LdapMultipleEntriesMatchedException, ServiceException {
ZLdapContext zlc = initZlc;
try {
if (zlc == null) {
zlc = LdapClient.getContext(LdapServerType.get(useMaster), LdapUsage.SEARCH);
}
ZSearchControls sc = (returnAttrs == null) ? ZSearchControls.SEARCH_CTLS_SUBTREE() : ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
ZSearchResultEnumeration ne = zlc.searchDir(base, filter, sc);
if (ne.hasMore()) {
ZSearchResultEntry sr = ne.next();
if (ne.hasMore()) {
String dups = LdapUtil.formatMultipleMatchedEntries(sr, ne);
throw LdapException.MULTIPLE_ENTRIES_MATCHED(base, filter.toFilterString(), dups);
}
ne.close();
return sr;
}
/* all callsites with the following @TODOEXCEPTIONMAPPING pattern can have ease of mind now and remove the
* TODOEXCEPTIONMAPPING annotation
*
} catch (NameNotFoundException e) {
return null;
} catch (InvalidNameException e) {
return null;
} catch (NamingException e) {
throw ServiceException.FAILURE("unable to lookup account via query: "+query+" message: "+e.getMessage(), e);
*/
} finally {
if (initZlc == null)
LdapClient.closeContext(zlc);
}
return null;
}
use of com.zimbra.cs.ldap.ZSearchControls 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;
}
use of com.zimbra.cs.ldap.ZSearchControls 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.ZSearchControls 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);
}
Aggregations