Search in sources :

Example 1 with ZSearchControls

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;
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration)

Example 2 with ZSearchControls

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;
}
Also used : ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry) TODOEXCEPTIONMAPPING(com.zimbra.cs.ldap.LdapTODO.TODOEXCEPTIONMAPPING)

Example 3 with ZSearchControls

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;
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) LdapEntry(com.zimbra.cs.account.ldap.entry.LdapEntry) HashSet(java.util.HashSet)

Example 4 with ZSearchControls

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);
        }
    }
}
Also used : ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) ServiceException(com.zimbra.common.service.ServiceException) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 5 with ZSearchControls

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);
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) LdapDIT(com.zimbra.cs.account.ldap.LdapDIT) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) HashSet(java.util.HashSet) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Aggregations

ZSearchControls (com.zimbra.cs.ldap.ZSearchControls)15 ZSearchResultEnumeration (com.zimbra.cs.ldap.ZSearchResultEnumeration)14 ZSearchResultEntry (com.zimbra.cs.ldap.ZSearchResultEntry)12 ZLdapFilter (com.zimbra.cs.ldap.ZLdapFilter)10 ServiceException (com.zimbra.common.service.ServiceException)5 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)5 AccountServiceException (com.zimbra.cs.account.AccountServiceException)4 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)4 LdapSizeLimitExceededException (com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException)4 ArrayList (java.util.ArrayList)3 LdapDIT (com.zimbra.cs.account.ldap.LdapDIT)2 HashSet (java.util.HashSet)2 AddressList (com.zimbra.cs.account.AddressList)1 DistributionList (com.zimbra.cs.account.DistributionList)1 LdapDistributionList (com.zimbra.cs.account.ldap.entry.LdapDistributionList)1 LdapEntry (com.zimbra.cs.account.ldap.entry.LdapEntry)1 GenericLdapConfig (com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig)1 TODOEXCEPTIONMAPPING (com.zimbra.cs.ldap.LdapTODO.TODOEXCEPTIONMAPPING)1 ZAttributes (com.zimbra.cs.ldap.ZAttributes)1 TestLdapZLdapFilter (com.zimbra.qa.unittest.prov.ldap.TestLdapZLdapFilter)1