use of com.zimbra.cs.ldap.ZLdapContext in project zm-mailbox by Zimbra.
the class TestLdapZLdapContext method searchPaged.
@Test
public void searchPaged() throws Exception {
int SIZE_LIMIT = 5;
String base = LdapConstants.DN_ROOT_DSE;
ZLdapFilter filter = ZLdapFilterFactory.getInstance().anyEntry();
String[] returnAttrs = new String[] { "objectClass" };
final List<String> result = new ArrayList<String>();
SearchLdapOptions.SearchLdapVisitor visitor = new SearchLdapOptions.SearchLdapVisitor() {
@Override
public void visit(String dn, Map<String, Object> attrs, IAttributes ldapAttrs) {
result.add(dn);
}
};
SearchLdapOptions searchOptions = new SearchLdapOptions(base, filter, returnAttrs, SIZE_LIMIT, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
boolean caughtException = false;
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapUsage.UNITTEST);
zlc.searchPaged(searchOptions);
} catch (LdapSizeLimitExceededException e) {
caughtException = true;
} finally {
LdapClient.closeContext(zlc);
}
assertTrue(caughtException);
assertEquals(SIZE_LIMIT, result.size());
}
use of com.zimbra.cs.ldap.ZLdapContext in project zm-mailbox by Zimbra.
the class TestLdapZLdapContext method searchDir.
@Test
public void searchDir() throws Exception {
int SIZE_LIMIT = 5;
String base = LdapConstants.DN_ROOT_DSE;
ZLdapFilter filter = ZLdapFilterFactory.getInstance().anyEntry();
String[] returnAttrs = new String[] { "objectClass" };
ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, SIZE_LIMIT, returnAttrs);
int numFound = 0;
boolean caughtException = false;
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapUsage.UNITTEST);
ZSearchResultEnumeration ne = zlc.searchDir(base, filter, searchControls);
while (ne.hasMore()) {
ZSearchResultEntry sr = ne.next();
numFound++;
}
ne.close();
} catch (LdapSizeLimitExceededException e) {
caughtException = true;
} finally {
LdapClient.closeContext(zlc);
}
assertTrue(caughtException);
/*
// unboundid does not return entries if LdapSizeLimitExceededException
// is thrown, See commons on ZLdapContext.searchDir().
if (testConfig != TestLdap.TestConfig.UBID) {
assertEquals(SIZE_LIMIT, numFound);
}
*/
}
use of com.zimbra.cs.ldap.ZLdapContext in project zm-mailbox by Zimbra.
the class LdapProvisioning method deleteHabOrgUnit.
@Override
public void deleteHabOrgUnit(Domain domain, String habOrgUnitName) throws ServiceException {
ZLdapContext zlc = null;
try {
String domainDn = ((LdapEntry) domain).getDN();
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_OU);
if (isEmptyOu(habOrgUnitName, domainDn)) {
zlc.deleteEntry(createOuDn(habOrgUnitName, domainDn));
} else {
throw ServiceException.FAILURE(String.format("HabOrgUnit: %s" + " of doamin:%s is not empty", habOrgUnitName, domainDn), null);
}
} catch (ServiceException e) {
throw ServiceException.FAILURE(String.format("Unable to delete HAB org unit: %s for domain=%s", habOrgUnitName, domain.getName()), e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZLdapContext in project zm-mailbox by Zimbra.
the class LdapProvisioning method createSignature.
private Signature createSignature(Account account, String signatureName, Map<String, Object> signatureAttrs, boolean restoring) throws ServiceException {
signatureName = signatureName.trim();
removeAttrIgnoreCase("objectclass", signatureAttrs);
validateSignatureAttrs(signatureAttrs);
LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
if (ldapEntry == null)
throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
/*
* check if the signature name already exists
*
* We check if the signatureName is the same as the signature on the account.
* For signatures that are in the signature LDAP entries, JNDI will throw
* NameAlreadyBoundException for duplicate names.
*
*/
Signature acctSig = LdapSignature.getAccountSignature(this, account);
if (acctSig != null && signatureName.equalsIgnoreCase(acctSig.getName()))
throw AccountServiceException.SIGNATURE_EXISTS(signatureName);
boolean setAsDefault = false;
List<Signature> existing = getAllSignatures(account);
// If the signature id is supplied with the request, check that it
// is not associated with an existing signature
String signatureId = (String) signatureAttrs.get(Provisioning.A_zimbraSignatureId);
if (signatureId != null) {
for (Signature signature : existing) {
if (signatureId.equals(signature.getAttr(Provisioning.A_zimbraSignatureId))) {
throw AccountServiceException.SIGNATURE_EXISTS(signatureId);
}
}
}
int numSigs = existing.size();
if (numSigs >= account.getLongAttr(A_zimbraSignatureMaxNumEntries, 20))
throw AccountServiceException.TOO_MANY_SIGNATURES();
else if (numSigs == 0)
setAsDefault = true;
account.setCachedData(SIGNATURE_LIST_CACHE_KEY, null);
boolean checkImmutable = !restoring;
CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
callbackContext.setData(DataKey.MAX_SIGNATURE_LEN, String.valueOf(account.getMailSignatureMaxLength()));
AttributeManager.getInstance().preModify(signatureAttrs, null, callbackContext, checkImmutable);
if (signatureId == null) {
signatureId = LdapUtil.generateUUID();
signatureAttrs.put(Provisioning.A_zimbraSignatureId, signatureId);
}
if (acctSig == null) {
// the slot on the account is not occupied, use it
signatureAttrs.put(Provisioning.A_zimbraSignatureName, signatureName);
// pass in setAsDefault as an optimization, since we are updating the account
// entry, we can update the default attr in one LDAP write
LdapSignature.createAccountSignature(this, account, signatureAttrs, setAsDefault);
return LdapSignature.getAccountSignature(this, account);
}
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_SIGNATURE);
String dn = getSignatureDn(ldapEntry, signatureName);
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.mapToAttrs(signatureAttrs);
entry.setAttr(A_objectClass, "zimbraSignature");
entry.setAttr(Provisioning.A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
entry.setDN(dn);
zlc.createEntry(entry);
Signature signature = getSignatureById(account, ldapEntry, signatureId, zlc);
AttributeManager.getInstance().postModify(signatureAttrs, signature, callbackContext);
if (setAsDefault)
setDefaultSignature(account, signatureId);
return signature;
} catch (LdapEntryAlreadyExistException nabe) {
throw AccountServiceException.SIGNATURE_EXISTS(signatureName);
} catch (LdapException e) {
throw e;
} catch (AccountServiceException e) {
throw e;
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to create signature: " + signatureName, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZLdapContext in project zm-mailbox by Zimbra.
the class LdapProvisioning method searchContainingDynamicGroupIdsForExternalAddress.
/*
* returns zimbraId of dynamic groups containing addr as an external member.
*/
private Set<String> searchContainingDynamicGroupIdsForExternalAddress(String addr, ZLdapContext initZlc) {
final Set<String> groupIds = Sets.newHashSet();
SearchLdapVisitor visitor = new SearchLdapVisitor(false) {
@Override
public void visit(String dn, IAttributes ldapAttrs) throws StopIteratingException {
String groupId = null;
try {
groupId = ldapAttrs.getAttrString(A_zimbraGroupId);
} catch (ServiceException e) {
ZimbraLog.account.warn("unable to get attr", e);
}
if (groupId != null) {
groupIds.add(groupId);
}
}
};
ZLdapContext zlc = initZlc;
try {
if (zlc == null) {
zlc = LdapClient.getContext(LdapServerType.REPLICA, LdapUsage.SEARCH);
}
String base = mDIT.mailBranchBaseDN();
ZLdapFilter filter = filterFactory.dynamicGroupsStaticUnitByMemberAddr(addr);
SearchLdapOptions searchOptions = new SearchLdapOptions(base, filter, new String[] { A_zimbraGroupId }, SearchLdapOptions.SIZE_UNLIMITED, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
zlc.searchPaged(searchOptions);
} catch (ServiceException e) {
ZimbraLog.account.warn("unable to search dynamic groups for guest acct", e);
} finally {
if (initZlc == null) {
LdapClient.closeContext(zlc);
}
}
return groupIds;
}
Aggregations