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