Search in sources :

Example 6 with ZSearchResultEnumeration

use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.

the class LdapProvisioning method hasSubordinates.

private boolean hasSubordinates(ZLdapContext zlc, String dn) throws ServiceException {
    boolean hasSubordinates = false;
    ZSearchResultEnumeration ne = null;
    try {
        ne = helper.searchDir(dn, filterFactory.hasSubordinates(), ZSearchControls.SEARCH_CTLS_SUBTREE(), zlc, LdapServerType.MASTER);
        hasSubordinates = ne.hasMore();
    } finally {
        if (ne != null) {
            ne.close();
        }
    }
    return hasSubordinates;
}
Also used : ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration)

Example 7 with ZSearchResultEnumeration

use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.

the class LdapProvisioning method getAllServers.

@Override
public List<Server> getAllServers(String service) throws ServiceException {
    List<Server> result = new ArrayList<Server>();
    ZLdapFilter filter;
    if (service != null) {
        filter = filterFactory.serverByService(service);
    } else {
        filter = filterFactory.allServers();
    }
    try {
        Map<String, Object> serverDefaults = getConfig().getServerDefaults();
        ZSearchResultEnumeration ne = helper.searchDir(mDIT.serverBaseDN(), filter, ZSearchControls.SEARCH_CTLS_SUBTREE());
        while (ne.hasMore()) {
            ZSearchResultEntry sr = ne.next();
            LdapServer s = new LdapServer(sr.getDN(), sr.getAttributes(), serverDefaults, this);
            result.add(s);
        }
        ne.close();
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to list all servers", e);
    }
    if (result.size() > 0)
        serverCache.put(result, true);
    Collections.sort(result);
    return result;
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) InMemoryLdapServer(com.zimbra.cs.ldap.unboundid.InMemoryLdapServer) LdapServer(com.zimbra.cs.account.ldap.entry.LdapServer) InMemoryLdapServer(com.zimbra.cs.ldap.unboundid.InMemoryLdapServer) LdapServer(com.zimbra.cs.account.ldap.entry.LdapServer) Server(com.zimbra.cs.account.Server) 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) ArrayList(java.util.ArrayList) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 8 with ZSearchResultEnumeration

use of com.zimbra.cs.ldap.ZSearchResultEnumeration in project zm-mailbox by Zimbra.

the class LdapProvisioning method getIdentitiesByQuery.

private List<Identity> getIdentitiesByQuery(LdapEntry entry, ZLdapFilter filter, ZLdapContext initZlc) throws ServiceException {
    List<Identity> result = new ArrayList<Identity>();
    try {
        String base = entry.getDN();
        ZSearchResultEnumeration ne = helper.searchDir(base, filter, ZSearchControls.SEARCH_CTLS_SUBTREE(), initZlc, LdapServerType.REPLICA);
        while (ne.hasMore()) {
            ZSearchResultEntry sr = ne.next();
            result.add(new LdapIdentity((Account) entry, sr.getDN(), sr.getAttributes(), this));
        }
        ne.close();
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to lookup identity via query: " + filter.toFilterString() + " message: " + e.getMessage(), e);
    }
    return result;
}
Also used : Account(com.zimbra.cs.account.Account) GuestAccount(com.zimbra.cs.account.GuestAccount) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) 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) ArrayList(java.util.ArrayList) LdapIdentity(com.zimbra.cs.account.ldap.entry.LdapIdentity) Identity(com.zimbra.cs.account.Identity) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry) LdapIdentity(com.zimbra.cs.account.ldap.entry.LdapIdentity)

Example 9 with ZSearchResultEnumeration

use of com.zimbra.cs.ldap.ZSearchResultEnumeration 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 10 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)

Aggregations

ZSearchResultEnumeration (com.zimbra.cs.ldap.ZSearchResultEnumeration)27 ZSearchResultEntry (com.zimbra.cs.ldap.ZSearchResultEntry)25 ServiceException (com.zimbra.common.service.ServiceException)15 AccountServiceException (com.zimbra.cs.account.AccountServiceException)14 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)14 ArrayList (java.util.ArrayList)14 ZLdapFilter (com.zimbra.cs.ldap.ZLdapFilter)11 ZSearchControls (com.zimbra.cs.ldap.ZSearchControls)11 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)4 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 MimeTypeInfo (com.zimbra.cs.mime.MimeTypeInfo)2 AlwaysOnCluster (com.zimbra.cs.account.AlwaysOnCluster)1 DataSource (com.zimbra.cs.account.DataSource)1