Search in sources :

Example 1 with ZSearchResultEnumeration

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);
        }
    }
}
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 2 with ZSearchResultEnumeration

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);
    }
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) ZAttributes(com.zimbra.cs.ldap.ZAttributes) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 3 with ZSearchResultEnumeration

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);
}
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)

Example 4 with ZSearchResultEnumeration

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;
}
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 5 with ZSearchResultEnumeration

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;
}
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)

Aggregations

ZSearchResultEnumeration (com.zimbra.cs.ldap.ZSearchResultEnumeration)31 ZSearchResultEntry (com.zimbra.cs.ldap.ZSearchResultEntry)27 ServiceException (com.zimbra.common.service.ServiceException)19 AccountServiceException (com.zimbra.cs.account.AccountServiceException)18 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)18 ArrayList (java.util.ArrayList)15 ZSearchControls (com.zimbra.cs.ldap.ZSearchControls)14 ZLdapFilter (com.zimbra.cs.ldap.ZLdapFilter)13 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)6 LdapSizeLimitExceededException (com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException)3 Account (com.zimbra.cs.account.Account)2 Cos (com.zimbra.cs.account.Cos)2 GuestAccount (com.zimbra.cs.account.GuestAccount)2 LdapDIT (com.zimbra.cs.account.ldap.LdapDIT)2 LdapAccount (com.zimbra.cs.account.ldap.entry.LdapAccount)2 LdapCos (com.zimbra.cs.account.ldap.entry.LdapCos)2 LdapMimeType (com.zimbra.cs.account.ldap.entry.LdapMimeType)2 ZAttributes (com.zimbra.cs.ldap.ZAttributes)2 MimeTypeInfo (com.zimbra.cs.mime.MimeTypeInfo)2 HashSet (java.util.HashSet)2