use of com.zimbra.cs.account.Signature in project zm-mailbox by Zimbra.
the class LdapProvisioning method getAllSignatures.
@Override
public List<Signature> getAllSignatures(Account account) throws ServiceException {
LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
if (ldapEntry == null)
throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
@SuppressWarnings("unchecked") List<Signature> result = (List<Signature>) account.getCachedData(SIGNATURE_LIST_CACHE_KEY);
if (result != null) {
return result;
}
result = new ArrayList<Signature>();
Signature acctSig = LdapSignature.getAccountSignature(this, account);
if (acctSig != null)
result.add(acctSig);
result = getSignaturesByQuery(account, ldapEntry, filterFactory.allSignatures(), null, result);
result = Collections.unmodifiableList(result);
account.setCachedData(SIGNATURE_LIST_CACHE_KEY, result);
return result;
}
use of com.zimbra.cs.account.Signature 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.account.Signature in project zm-mailbox by Zimbra.
the class LdapProvisioning method getSignaturesByQuery.
private List<Signature> getSignaturesByQuery(Account acct, LdapEntry entry, ZLdapFilter filter, ZLdapContext initZlc, List<Signature> result) throws ServiceException {
if (result == null) {
result = new ArrayList<Signature>();
}
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 LdapSignature(acct, sr.getDN(), sr.getAttributes(), this));
}
ne.close();
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to lookup signature via query: " + filter.toFilterString() + " message: " + e.getMessage(), e);
}
return result;
}
use of com.zimbra.cs.account.Signature in project zm-mailbox by Zimbra.
the class GetSignatures method handle.
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Account account = getRequestedAccount(zsc);
if (!canAccessAccount(zsc, account))
throw ServiceException.PERM_DENIED("can not access account");
Element response = zsc.createElement(AccountConstants.GET_SIGNATURES_RESPONSE);
List<Signature> signatures = Provisioning.getInstance().getAllSignatures(account);
for (Signature sig : signatures) ToXML.encodeSignature(response, sig);
return response;
}
use of com.zimbra.cs.account.Signature in project zm-mailbox by Zimbra.
the class ModifySignature method handle.
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Account account = getRequestedAccount(zsc);
if (!canModifyOptions(zsc, account))
throw ServiceException.PERM_DENIED("can not modify options");
Provisioning prov = Provisioning.getInstance();
Element eSignature = request.getElement(AccountConstants.E_SIGNATURE);
Signature signature = null;
String id = eSignature.getAttribute(AccountConstants.A_ID);
signature = prov.get(account, Key.SignatureBy.id, id);
if (signature == null)
throw AccountServiceException.NO_SUCH_SIGNATURE(id);
List<Element> contents = eSignature.listElements(AccountConstants.E_CONTENT);
Map<String, Object> attrs = new HashMap<String, Object>();
for (Element eContent : contents) {
String type = eContent.getAttribute(AccountConstants.A_TYPE);
String attr = SignatureUtil.mimeTypeToAttrName(type);
if (attr == null)
throw ServiceException.INVALID_REQUEST("invalid type " + type, null);
if (attrs.get(attr) != null)
throw ServiceException.INVALID_REQUEST("only one " + type + " content is allowed", null);
String content = eContent.getText();
if (content != null)
attrs.put(attr, content);
}
String name = eSignature.getAttribute(AccountConstants.A_NAME, null);
if (name != null)
attrs.put(Provisioning.A_zimbraSignatureName, name);
Element eContactId = eSignature.getOptionalElement(AccountConstants.E_CONTACT_ID);
if (eContactId != null)
attrs.put(Provisioning.A_zimbraPrefMailSignatureContactId, eContactId.getText());
prov.modifySignature(account, signature.getId(), attrs);
Element response = zsc.createElement(AccountConstants.MODIFY_SIGNATURE_RESPONSE);
return response;
}
Aggregations