Saturday, June 13, 2015

Custom Alfresco MailActionExecuter to support CC, BCC, CC Lists, and BCC Lists

I needed to create a custom alfresco MailActionExecuter action so it could do several additional things that are not provided from the vanilla OOB alfresco version.  I did this case study with version 4.2.e, but double-checked in the enterpise 5.0.1 code base, that it does not have this support yet either.  Here's what I needed to do:
  • Support CC, BCC, CC Lists, and BCC Lists
  • Fix the problem of if there are X number of multiple recipients, then X number of emails are sent.  Why?  We only need one email sent for the X multiple recipients.  For example, if you had three recipients in your email list, 3 separate emails was sent.  We only need one email sent that will have a To/CC/BCC list that includes the three recipients.
NOTEGrab the source code for the 4.2.e, 5.0.1, and the custom code described in the steps below.

Steps:
  1. Grap the source code for MailActionExecuter located in the alfresco-repository source jar, grab from either:
    • alfresco public maven or alfresco private maven (depending on community or enterprise version).  
      • I opted for this method and downloaded both alfresco-repository-4.2.e-sources.jar and alfresco-repository-5.0.1-sources.jar.
    • sdk source code zip download for your particular alfresco version
  2. Create a custom version of MailActionExecuter and put it in a custom package (i.e. com.cherryshoe.action.executer).  Changes in the MailActionExecuter are indicated by comments that start with "NEW CHERRYSHOE" that tell you what was changed and why. 
    package com.cherryshoe.action.executer;
    
    /*
     * These are NEW CHERRYSHOE updates for org.alfresco.repo.action.executer.MailActionExecuter.
     * The Alfresco MailActionExecuter has the following issues that will be fixed for the NEW CHERRYSHOE version:
     * 1.  If there are X multiple recipients, then X emails are sent.  We only need ONE email sent for the
     * X multiple recipients.
     * 2.  PARAM_CC, PARAM_CC_MANY, PARAM_BCC, and PARAM_BCC_MANY are not currently supported.  
     */
    
    import java.io.Serializable;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Locale;
    import java.util.Map;
    import java.util.Set;
    import java.util.concurrent.atomic.AtomicInteger;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    import org.alfresco.error.AlfrescoRuntimeException;
    import org.alfresco.model.ContentModel;
    import org.alfresco.repo.action.ParameterDefinitionImpl;
    import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
    import org.alfresco.repo.action.executer.TestModeable;
    import org.alfresco.repo.admin.SysAdminParams;
    import org.alfresco.repo.security.authentication.AuthenticationUtil;
    import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
    import org.alfresco.repo.template.DateCompareMethod;
    import org.alfresco.repo.template.HasAspectMethod;
    import org.alfresco.repo.template.I18NMessageMethod;
    import org.alfresco.repo.template.TemplateNode;
    import org.alfresco.repo.tenant.TenantService;
    import org.alfresco.repo.tenant.TenantUtil;
    import org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork;
    import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
    import org.alfresco.repo.transaction.RetryingTransactionHelper;
    import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
    import org.alfresco.repo.transaction.TransactionListenerAdapter;
    import org.alfresco.service.ServiceRegistry;
    import org.alfresco.service.cmr.action.Action;
    import org.alfresco.service.cmr.action.ParameterDefinition;
    import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
    import org.alfresco.service.cmr.preference.PreferenceService;
    import org.alfresco.service.cmr.repository.NodeRef;
    import org.alfresco.service.cmr.repository.NodeService;
    import org.alfresco.service.cmr.repository.TemplateImageResolver;
    import org.alfresco.service.cmr.repository.TemplateService;
    import org.alfresco.service.cmr.security.AuthenticationService;
    import org.alfresco.service.cmr.security.AuthorityService;
    import org.alfresco.service.cmr.security.AuthorityType;
    import org.alfresco.service.cmr.security.PersonService;
    import org.alfresco.util.Pair;
    import org.alfresco.util.UrlUtil;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.commons.validator.routines.EmailValidator;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.extensions.surf.util.I18NUtil;
    import org.springframework.mail.MailException;
    import org.springframework.mail.MailPreparationException;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.mail.javamail.MimeMessagePreparator;
    import org.springframework.util.StringUtils;
    
    /**
     * Mail action executor implementation.
     * <p/>
     * <em>Note on executing this action as System:</em> it is allowed to execute {@link #NAME mail} actions as system.
     * However there is a limitation if you do so. Because the system user is not a normal user and specifically because
     * there is no corresponding {@link ContentModel#TYPE_PERSON cm:person} node for system, it is not possible to use
     * any reference to that person in the associated email template. Various email templates use a '{@link TemplateNode person}' object
     * in the FTL model to access things like first name, last name etc.
     * In the case of mail actions sent while running as system, none of these will be available.
     * 
     * @author Roy Wetherall
     */
    public class MailActionExecuter extends ActionExecuterAbstractBase
                                    implements InitializingBean, TestModeable
    {
        private static Log logger = LogFactory.getLog(MailActionExecuter.class);
        
        /**
         * Action executor constants
         */
        public static final String NAME = "mail";
        public static final String PARAM_LOCALE = "locale";
        public static final String PARAM_TO = "to";
        public static final String PARAM_CC = "cc"; // NEW CHERRYSHOE
        public static final String PARAM_BCC = "bcc"; // NEW CHERRYSHOE
        public static final String PARAM_TO_MANY = "to_many";
        public static final String PARAM_CC_MANY = "cc_many"; // NEW CHERRYSHOE
        public static final String PARAM_BCC_MANY = "bcc_many"; // NEW CHERRYSHOE
        public static final String PARAM_SUBJECT = "subject";
        public static final String PARAM_SUBJECT_PARAMS = "subjectParams";
        public static final String PARAM_TEXT = "text";
        public static final String PARAM_HTML = "html";
        public static final String PARAM_FROM = "from";
        public static final String PARAM_FROM_PERSONAL_NAME = "fromPersonalName";
        public static final String PARAM_TEMPLATE = "template";
        public static final String PARAM_TEMPLATE_MODEL = "template_model";
        public static final String PARAM_IGNORE_SEND_FAILURE = "ignore_send_failure";
        public static final String PARAM_SEND_AFTER_COMMIT = "send_after_commit";
       
        /**
         * From address
         */
        private static final String FROM_ADDRESS = "alfresco@alfresco.org";
        
        /**
         * The java mail sender
         */
        private JavaMailSender mailService;
        
        /**
         * The Template service
         */
        private TemplateService templateService;
        
        /**
         * The Person service
         */
        private PersonService personService;
        
        /**
         * The Authentication service
         */
        private AuthenticationService authService;
        
        /**
         * The Node Service
         */
        private NodeService nodeService;
        
        /**
         * The Authority Service
         */
        private AuthorityService authorityService;
        
        /**
         * The Service registry
         */
        private ServiceRegistry serviceRegistry;
        
        /**
         * System Administration parameters, including URL information
         */
        private SysAdminParams sysAdminParams;
        
        /**
         * The Preference Service
         */
        private PreferenceService preferenceService;
        
        /**
         * The Tenant Service
         */
        private TenantService tenantService;
        
        /**
         * Mail header encoding scheme
         */
        private String headerEncoding = null;
        
        /**
         * Default from address
         */
        private String fromDefaultAddress = null;
        
        /**
         * Is the from field enabled? Or must we always use the default address.
         */
        private boolean fromEnabled = true;
        
        
        private boolean sendTestMessage = false;
        private String testMessageTo = null;
        private String testMessageSubject = "Test message";
        private String testMessageText = "This is a test message.";
    
        private boolean validateAddresses = true;
        
        /**
         * Test mode prevents email messages from being sent.
         * It is used when unit testing when we don't actually want to send out email messages.
         * 
         * MER 20/11/2009 This is a quick and dirty fix. It should be replaced by being 
         * "mocked out" or some other better way of running the unit tests. 
         */
        private boolean testMode = false;
        private MimeMessage lastTestMessage;
    
        private TemplateImageResolver imageResolver;
    
        /**
         * @param javaMailSender    the java mail sender
         */
        public void setMailService(JavaMailSender javaMailSender) 
        {
            this.mailService = javaMailSender;
        }
        
        /**
         * @param templateService   the TemplateService
         */
        public void setTemplateService(TemplateService templateService)
        {
            this.templateService = templateService;
        }
        
        /**
         * @param personService     the PersonService
         */
        public void setPersonService(PersonService personService)
        {
            this.personService = personService;
        }
        
        public void setPreferenceService(PreferenceService preferenceService)
        {
            this.preferenceService = preferenceService;
        }
        
        /**
         * @param authService       the AuthenticationService
         */
        public void setAuthenticationService(AuthenticationService authService)
        {
            this.authService = authService;
        }
        
        /**
         * @param serviceRegistry   the ServiceRegistry
         */
        public void setServiceRegistry(ServiceRegistry serviceRegistry)
        {
            this.serviceRegistry = serviceRegistry;
        }
        
        /**
         * @param authorityService  the AuthorityService
         */
        public void setAuthorityService(AuthorityService authorityService)
        {
            this.authorityService = authorityService;
        }
        
        /**
         * @param nodeService       the NodeService to set.
         */
        public void setNodeService(NodeService nodeService)
        {
            this.nodeService = nodeService;
        }
        
        /**
         * @param tenantService       the TenantService to set.
         */
        public void setTenantService(TenantService tenantService)
        {
            this.tenantService = tenantService;
        }
        
        /**
         * @param headerEncoding     The mail header encoding to set.
         */
        public void setHeaderEncoding(String headerEncoding)
        {
            this.headerEncoding = headerEncoding;
        }
        
        /**
         * @param fromAddress   The default mail address.
         */
        public void setFromAddress(String fromAddress)
        {
            this.fromDefaultAddress = fromAddress;
        }
    
        public void setSysAdminParams(SysAdminParams sysAdminParams)
        {
            this.sysAdminParams = sysAdminParams;
        }
        
        public void setImageResolver(TemplateImageResolver imageResolver)
        {
            this.imageResolver = imageResolver;
        }
        
        public void setTestMessageTo(String testMessageTo)
        {
            this.testMessageTo = testMessageTo;
        }
        
        public String getTestMessageTo()
        {
            return testMessageTo;
        }
        
        public void setTestMessageSubject(String testMessageSubject)
        {
            this.testMessageSubject = testMessageSubject;
        }
        
        public void setTestMessageText(String testMessageText)
        {
            this.testMessageText = testMessageText;
        }
    
        public void setSendTestMessage(boolean sendTestMessage)
        {
            this.sendTestMessage = sendTestMessage;
        }
        
        /**
         * This stores an email address which, if it is set, overrides ALL email recipients sent from
         * this class. It is intended for dev/test usage only !!
         */
        private String testModeRecipient;
    
        /**
         * Send a test message
         * 
         * @return true, message sent 
         * @throws AlfrescoRuntimeException 
         */
        public boolean sendTestMessage() 
        {
            if(testMessageTo == null || testMessageTo.length() == 0)
            {
                throw new AlfrescoRuntimeException("email.outbound.err.test.no.to");
            }
            if(testMessageSubject == null || testMessageSubject.length() == 0)
            {
                throw new AlfrescoRuntimeException("email.outbound.err.test.no.subject");
            }
            if(testMessageText == null || testMessageText.length() == 0)
            {
                throw new AlfrescoRuntimeException("email.outbound.err.test.no.text");
            }
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put(PARAM_TO, testMessageTo);
            params.put(PARAM_SUBJECT, testMessageSubject);
            params.put(PARAM_TEXT, testMessageText);
            
            Action ruleAction = serviceRegistry.getActionService().createAction(NAME, params);
            
            MimeMessageHelper message = prepareEmail(ruleAction, null,
                    new Pair<String, Locale>(testMessageTo, getLocaleForUser(testMessageTo)), getFrom(ruleAction));
            try
            {
                mailService.send(message.getMimeMessage());
                onSend();
            }
            catch (MailException me)
            {
                onFail();
                StringBuffer txt = new StringBuffer();
                
                txt.append(me.getClass().getName() + ", " + me.getMessage());
                
                Throwable cause = me.getCause();
                while (cause != null)
                {
                    txt.append(", ");
                    txt.append(cause.getClass().getName() + ", " + cause.getMessage());
                    cause = cause.getCause();
                }
                
                Object[] args = {testMessageTo, txt.toString()};
                throw new AlfrescoRuntimeException("email.outbound.err.send.failed", args, me);
            }
            
            return true;
        }
        
        public void setTestModeRecipient(String testModeRecipient)
        {
            this.testModeRecipient = testModeRecipient;
        }
    
        public void setValidateAddresses(boolean validateAddresses)
        {
            this.validateAddresses = validateAddresses;
        }
    
        
        @Override
        public void init()
        {
            if(logger.isDebugEnabled())
            {
                logger.debug("Init called, testMessageTo=" + testMessageTo);
            }
            
            numberSuccessfulSends.set(0);
            numberFailedSends.set(0);
            
            super.init();
            if (sendTestMessage && testMessageTo != null)
            {
                AuthenticationUtil.runAs(new RunAsWork<Object>()
                {
                    public Object doWork() throws Exception
                    {
                        Map<String, Serializable> params = new HashMap<String, Serializable>();
                        params.put(PARAM_TO, testMessageTo);
                        params.put(PARAM_SUBJECT, testMessageSubject);
                        params.put(PARAM_TEXT, testMessageText);
    
                        Action ruleAction = serviceRegistry.getActionService().createAction(NAME, params);
                        executeImpl(ruleAction, null);
                        return null;
                    }
                }, AuthenticationUtil.getSystemUserName());
            }
        }
    
        /**
         * Initialise bean
         */
        public void afterPropertiesSet() throws Exception
        {
            if (fromDefaultAddress == null || fromDefaultAddress.length() == 0)
            {
                fromDefaultAddress = FROM_ADDRESS;
            }
            
        }
        
        /**
         * Send an email message
         * 
         * @throws AlfrescoRuntimeExeption
         */
        @Override
        protected void executeImpl(
                final Action ruleAction,
                final NodeRef actionedUponNodeRef) 
        {
           
            //Prepare our messages before the commit, in-case of deletes
            MimeMessageHelper[] messages = null; 
            if (validNodeRefIfPresent(actionedUponNodeRef))
            {
            messages = prepareEmails(ruleAction, actionedUponNodeRef);
            }
            final MimeMessageHelper[] finalMessages = messages;
            
            //Send out messages
            if(finalMessages!=null){
                if (sendAfterCommit(ruleAction))
                {
                    AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter()
                    {
                        @Override
                        public void afterCommit()
                        {
                            RetryingTransactionHelper helper = serviceRegistry.getTransactionService().getRetryingTransactionHelper();
                            helper.doInTransaction(new RetryingTransactionCallback<Void>()
                            {
                                @Override
                                public Void execute() throws Throwable
                                {
                                    for (MimeMessageHelper message : finalMessages) {
                                        sendEmail(ruleAction, message);
                                    }
                                    
                                    return null;
                                }
                            }, false, true);
                        }
                    });
                }
                else 
                {
                        for (MimeMessageHelper message : finalMessages) {
                            sendEmail(ruleAction, message);
                            break; // NEW CHERRYSHOE The code before it got here should be preventing multiple messages to be created, but just in case, only call sendEmail once
                        }
                }
            }
    
        }
        
        
        private boolean validNodeRefIfPresent(NodeRef actionedUponNodeRef)
        {
            if (actionedUponNodeRef == null)
            {
                // We must expect that null might be passed in (ALF-11625)
                // since the mail action might not relate to a specific nodeRef.
                return true;
            }
            else
            {
                // Only try and send the email if the actioned upon node reference still exists
                // (i.e. if one has been specified it must be valid)
                return nodeService.exists(actionedUponNodeRef);
            }
        }
        
        private boolean sendAfterCommit(Action action)
        {
            Boolean sendAfterCommit = (Boolean) action.getParameterValue(PARAM_SEND_AFTER_COMMIT);
            return sendAfterCommit == null ? false : sendAfterCommit.booleanValue();
        }
        
        /*
         * NEW CHERRYSHOE: Alfresco OOB code here is setting the number of messages to send determined by how many recipients there are total. 
         * That seems totally wrong, as you should just have ONE email sent period no matter how many recipients, with the recipients properly set in the ONE email message.
         * The getRecipients has been modified to return after at least one recipient is determined.  Also break after the loop is finished even though collections is size of 1.
         */
        private MimeMessageHelper[] prepareEmails(final Action ruleAction, final NodeRef actionedUponNodeRef)
        {
            List<Pair<String, Locale>> recipients = getRecipients(ruleAction);
            
            Pair<InternetAddress, Locale> from = getFrom(ruleAction);
            
            if (logger.isDebugEnabled())
            {
                logger.debug("From: address=" + from.getFirst() + " ,locale=" + from.getSecond());
            }
            
            MimeMessageHelper[] messages = new MimeMessageHelper[recipients.size()];
            int recipientIndex = 0;
            for (Pair<String, Locale> recipient : recipients)
            {
                if (logger.isDebugEnabled())
                {
                    logger.debug("Recipient: address=" + recipient.getFirst() + " ,locale=" + recipient.getSecond());
                }
                
                messages[recipientIndex] = prepareEmail(ruleAction, actionedUponNodeRef, recipient, from);
                recipientIndex++;
                
                // NEW CHERRYSHOE only call prepareEmail once
                break;
            }
            
            return messages;
        }
        
        public MimeMessageHelper prepareEmail(final Action ruleAction , final NodeRef actionedUponNodeRef, final Pair<String, Locale> recipient, final Pair<InternetAddress, Locale> sender)
        {
            // Create the mime mail message.
            // Hack: using an array here to get around the fact that inner classes aren't closures.
            // The MimeMessagePreparator.prepare() signature does not allow us to return a value and yet
            // we can't set a result on a bare, non-final object reference due to Java language restrictions.
            final MimeMessageHelper[] messageRef = new MimeMessageHelper[1];
            MimeMessagePreparator mailPreparer = new MimeMessagePreparator()
            {
                @SuppressWarnings("unchecked")
                public void prepare(MimeMessage mimeMessage) throws MessagingException
                {
                    if (logger.isDebugEnabled())
                    {
                       logger.debug(ruleAction.getParameterValues());
                    }
                    
                    messageRef[0] = new MimeMessageHelper(mimeMessage);
                    
                    // set header encoding if one has been supplied
                    if (headerEncoding != null && headerEncoding.length() != 0)
                    {
                        mimeMessage.setHeader("Content-Transfer-Encoding", headerEncoding);
                    }
                    
                    // set recipient
                    String to = (String)ruleAction.getParameterValue(PARAM_TO);
                    String cc = (String)ruleAction.getParameterValue(PARAM_CC); // NEW CHERRYSHOE
                    String bcc = (String)ruleAction.getParameterValue(PARAM_BCC); // NEW CHERRYSHOE
                    // NEW CHERRYSHOE use apache string utils to check for is blank
                    if (!org.apache.commons.lang.StringUtils.isBlank(to) || !org.apache.commons.lang.StringUtils.isBlank(cc) || !org.apache.commons.lang.StringUtils.isBlank(bcc))
                    {
                        if (!org.apache.commons.lang.StringUtils.isBlank(to)) {
                            messageRef[0].setTo(to);
                        }
                        if (!org.apache.commons.lang.StringUtils.isBlank(cc)) {
                            messageRef[0].setCc(cc);
                        }
                        if (!org.apache.commons.lang.StringUtils.isBlank(bcc)) {
                            messageRef[0].setBcc(bcc);
                        }
                        
                        // Note: there is no validation on the username to check that it actually is an email address.
                        // TODO Fix this.
                    }
                    else
                    {
                        // NEW CHERRYSHOE added CC and BCC support.  For TO, CC, and BCC, respectively, 
                        // created new method setUpManyRecipients to set up multiple recipients
                        // *****TO*****
                        List<String> toRecipients = null;
                        toRecipients = setUpManyRecipients(ruleAction, PARAM_TO_MANY);
                        if(toRecipients.size() > 0)
                        {
                            messageRef[0].setTo(toRecipients.toArray(new String[toRecipients.size()]));
                        }
    
                        // *****CC*****                  
                        List<String> ccRecipients = null;
                        ccRecipients = setUpManyRecipients(ruleAction, PARAM_CC_MANY);
                        if(ccRecipients.size() > 0)
                        {
                            messageRef[0].setCc(ccRecipients.toArray(new String[ccRecipients.size()]));     
                        }
                        
                        // *****BCC*****
                        List<String> bccRecipients = null;
                        bccRecipients = setUpManyRecipients(ruleAction, PARAM_BCC_MANY);
                        if(bccRecipients.size() > 0)
                        {
                            messageRef[0].setBcc(bccRecipients.toArray(new String[bccRecipients.size()]));
                        }
    
                    }
                    
                    // from person - not to be performed for the "admin" or "system" users
                    NodeRef fromPerson = null;
                    
                    final String currentUserName = authService.getCurrentUserName();
                    
                    final List<String> usersNotToBeUsedInFromField = Arrays.asList(new String[] {AuthenticationUtil.getAdminUserName(),
                                                                                                 AuthenticationUtil.getSystemUserName(),
                                                                                                 AuthenticationUtil.getGuestUserName()});
                    if ( !usersNotToBeUsedInFromField.contains(currentUserName))
                    {
                        fromPerson = personService.getPerson(currentUserName);
                    }
                    
                    if(isFromEnabled())
                    {   
                        // Use the FROM parameter in preference to calculating values.
                        String from = (String)ruleAction.getParameterValue(PARAM_FROM);
                        if (from != null && from.length() > 0)
                        {
                            if(logger.isDebugEnabled())
                            {
                                logger.debug("from specified as a parameter, from:" + from);
                            }
                            
                            // Check whether or not to use a personal name for the email (will be RFC 2047 encoded)
                            String fromPersonalName = (String)ruleAction.getParameterValue(PARAM_FROM_PERSONAL_NAME);
                            if(fromPersonalName != null && fromPersonalName.length() > 0) 
                            {
                                try
                                {
                                    messageRef[0].setFrom(from, fromPersonalName);
                                }
                                catch (UnsupportedEncodingException error)
                                {
                                    // Uses the JVM's default encoding, can never be unsupported. Just in case, revert to simple email
                                    messageRef[0].setFrom(from);
                                }
                            }
                            else
                            {
                                messageRef[0].setFrom(from);
                            }
                        }
                        else
                        {
                            // set the from address from the current user
                            String fromActualUser = null;
                            if (fromPerson != null)
                            {
                                fromActualUser = (String) nodeService.getProperty(fromPerson, ContentModel.PROP_EMAIL);
                            }
                        
                            if (fromActualUser != null && fromActualUser.length() != 0)
                            {
                                if(logger.isDebugEnabled())
                                {
                                    logger.debug("looked up email address for :" + fromPerson + " email from " + fromActualUser);
                                }
                                messageRef[0].setFrom(fromActualUser);
                            }
                            else
                            {
                                // from system or user does not have email address
                                messageRef[0].setFrom(fromDefaultAddress);
                            }
                        }
    
                    }
                    else
                    {
                        if(logger.isDebugEnabled())
                        {
                            logger.debug("from not enabled - sending from default address:" + fromDefaultAddress);
                        }
                        // from is not enabled.
                        messageRef[0].setFrom(fromDefaultAddress);
                    }
                    
    
    
                    
                    // set subject line
                    messageRef[0].setSubject((String)ruleAction.getParameterValue(PARAM_SUBJECT));
                    
                    if ((testModeRecipient != null) && (testModeRecipient.length() > 0) && (! testModeRecipient.equals("${dev.email.recipient.address}")))
                    {
                        // If we have an override for the email recipient, we'll send the email to that address instead.
                        // We'll prefix the subject with the original recipient, but leave the email message unchanged in every other way.
                        messageRef[0].setTo(testModeRecipient);
                        
                        String emailRecipient = (String)ruleAction.getParameterValue(PARAM_TO);
                        if (emailRecipient == null)
                        {
                           Object obj = ruleAction.getParameterValue(PARAM_TO_MANY);
                           if (obj != null)
                           {
                               emailRecipient = obj.toString();
                           }
                        }
                        
                        String recipientPrefixedSubject = "(" + emailRecipient + ") " + (String)ruleAction.getParameterValue(PARAM_SUBJECT);
                        
                        messageRef[0].setSubject(recipientPrefixedSubject);
                    }
                    
                    
                    // See if an email template has been specified
                    String text = null;
                    
                    // templateRef: either a nodeRef or classpath (see ClasspathRepoTemplateLoader)
                    Serializable ref = ruleAction.getParameterValue(PARAM_TEMPLATE);
                    String templateRef = (ref instanceof NodeRef ? ((NodeRef)ref).toString() : (String)ref);
                    if (templateRef != null)
                    {
                        Map<String, Object> suppliedModel = null;
                        if(ruleAction.getParameterValue(PARAM_TEMPLATE_MODEL) != null)
                        {
                            Object m = ruleAction.getParameterValue(PARAM_TEMPLATE_MODEL);
                            if(m instanceof Map)
                            {
                                suppliedModel = (Map<String, Object>)m;
                            }
                            else
                            {
                                logger.warn("Skipping unsupported email template model parameters of type "
                                        + m.getClass().getName() + " : " + m.toString());
                            }
                        }
                        
                        // build the email template model
                        Map<String, Object> model = createEmailTemplateModel(actionedUponNodeRef, suppliedModel, fromPerson);
    
                        // Determine the locale to use to send the email.
                        Locale locale = recipient.getSecond();
                        if (locale == null)
                        {
                            locale = (Locale)ruleAction.getParameterValue(PARAM_LOCALE);
                        }
                        if (locale == null)
                        {
                            locale = sender.getSecond();
                        }
                        
                        // set subject line
                        String subject = (String)ruleAction.getParameterValue(PARAM_SUBJECT);
                        Object[] subjectParams = (Object[])ruleAction.getParameterValue(PARAM_SUBJECT_PARAMS);
                        String localizedSubject = getLocalizedSubject(subject, subjectParams, locale);
                        if (locale == null)
                        {
                            // process the template against the model
                            text = templateService.processTemplate("freemarker", templateRef, model);
                        }
                        else
                        {
                            // process the template against the model
                            text = templateService.processTemplate("freemarker", templateRef, model, locale);
                        }
                        if ((testModeRecipient != null) && (testModeRecipient.length() > 0) && (! testModeRecipient.equals("${dev.email.recipient.address}")))
                        {
                            // If we have an override for the email recipient, we'll send the email to that address instead.
                            // We'll prefix the subject with the original recipient, but leave the email message unchanged in every other way.
                            messageRef[0].setTo(testModeRecipient);
                            
                            String emailRecipient = recipient.getFirst();
                            
                            String recipientPrefixedSubject = "(" + emailRecipient + ") " + localizedSubject;
                            
                            messageRef[0].setSubject(recipientPrefixedSubject);
                        }
                        else 
                        {
                            messageRef[0].setTo(recipient.getFirst());
                            messageRef[0].setSubject(localizedSubject);
                        }
                    }
                    
                    // set the text body of the message
                    
                    boolean isHTML = false;
                    if (text == null)
                    {
                        text = (String)ruleAction.getParameterValue(PARAM_TEXT);
                    }
                    
                    if (text != null)
                    {
                        if (isHTML(text))
                        {
                            isHTML = true;
                        }
                    }
                    else
                    {
                        text = (String)ruleAction.getParameterValue(PARAM_HTML);
                        if (text != null)
                        {
                            // assume HTML
                            isHTML = true;
                        }
                    }
                    
                    if (text != null)
                    {
                        messageRef[0].setText(text, isHTML);
                    }
                    
                }
            };
            MimeMessage mimeMessage = mailService.createMimeMessage(); 
            try
            {
                mailPreparer.prepare(mimeMessage);
            } catch (Exception e)
            {
                // We're forced to catch java.lang.Exception here. Urgh.
                if (logger.isInfoEnabled())
                {
                    logger.warn("Unable to prepare mail message. Skipping.", e);
                }
            }
            
            return messageRef[0];
        }
        
        /*
         * NEW CHERRYSHOE: Figures out how many recipients there are for a particular paramMany type.
         * Since we only need maximum one recipient, then we will break out when we have that.
         */
        private List<Pair<String, Locale>> getManyRecipients(final Action ruleAction, String paramMany) 
        {
            List<Pair<String, Locale>> recipients = new LinkedList<Pair<String,Locale>>();
            
            // see if multiple recipients have been supplied - as a list of authorities
            Serializable authoritiesValue = ruleAction.getParameterValue(paramMany);
            List<String> authorities = null;
            if (authoritiesValue != null)
            {
                if (authoritiesValue instanceof String)
                {
                    authorities = new ArrayList<String>(1);
                    authorities.add((String)authoritiesValue);
                }
                else
                {
                    authorities = (List<String>)authoritiesValue;
                }
            }
            
            if (authorities != null && authorities.size() != 0)
            {
                for (String authority : authorities)
                {
                    AuthorityType authType = AuthorityType.getAuthorityType(authority);
                    if (authType.equals(AuthorityType.USER))
                    {
                        // Formerly, this code checked personExists(auth) but we now support emailing addresses who are not yet Alfresco users.
                        // Check the user name to be a valid email and we don't need to log an error in this case
                        // ALF-19231
                        // Validate the email, allowing for local email addresses
                        if (authority != null && authority.length() != 0)
                        {
                            if (personExists(authority))
                            {
                                EmailValidator emailValidator = EmailValidator.getInstance(true);
                                if (validateAddresses && emailValidator.isValid(authority))
                                {
                                    Locale locale = getLocaleForUser(authority);
                                    recipients.add(new Pair<String, Locale>(authority, locale));
                                    break; // only need one maximum recipient
                                }
                                else
                                {
                                    String address = getPersonEmail(authority);
                                    if (address != null && address.length() != 0 && validateAddress(address))
                                    {
                                        Locale locale = getLocaleForUser(authority);
                                        recipients.add(new Pair<String, Locale>(address, locale));
                                        break; // only need one maximum recipient
                                    }
                                }
                            }
                            else
                            {
                                recipients.add(new Pair<String, Locale>(authority, null));
                                break; // only need one maximum recipient
                            }
                        }
                    }
                    else if (authType.equals(AuthorityType.GROUP) || authType.equals(AuthorityType.EVERYONE))
                    {
                        // NEW CHERRYSHOE
                        // Notify all members of the group
                        Set<String> users;
                        if (authType.equals(AuthorityType.GROUP))
                        {        
                            users = authorityService.getContainedAuthorities(AuthorityType.USER, authority, false);
                        }
                        else
                        {
                            users = authorityService.getAllAuthorities(AuthorityType.USER);
                        }
                        
                        for (String userAuth : users)
                        {
                            if (personExists(userAuth))
                            {
                                // Check the user name to be a valid email and we don't need to log an error in this case
                                // ALF-19231
                                // Validate the email, allowing for local email addresses
                                EmailValidator emailValidator = EmailValidator.getInstance(true);
                                if (validateAddresses && emailValidator.isValid(userAuth))
                                {
                                    if (userAuth != null && userAuth.length() != 0)
                                    {
                                        Locale locale = getLocaleForUser(userAuth);
                                        recipients.add(new Pair<String, Locale>(userAuth, locale));
                                        break; // only need one maximum recipient
                                    }
                                }
                                else
                                {
                                    String address = getPersonEmail(userAuth);
                                    if (address != null && address.length() != 0 && validateAddress(address))
                                    {
                                        Locale locale = getLocaleForUser(userAuth);
                                        recipients.add(new Pair<String, Locale>(address, locale));
                                        break; // only need one maximum recipient
                                    }
                                }
                            }
                            else
                            {
                                recipients.add(new Pair<String, Locale>(authority, null));
                                break; // only need one maximum recipient
                            }
                        }
                    }
                }
                if(recipients.size() <= 0)
                {
                    // All recipients were invalid
                    throw new MailPreparationException(
                            "All recipients for the mail action were invalid"
                    );
                }
            }
            else
            {
                // No recipients have been specified, change to info only don't throw an error
                logger.info(
                        "No recipient has been specified for the mail action in getManyRecipients for paramMany[" + paramMany + "]"
                );
            }
            
            return recipients;
        }
        
        /*
         * NEW CHERRYSHOE Sets up the PARAM_X_MANY recipients based on the "param" parameter.
         */
        private List<String> setUpManyRecipients(final Action ruleAction, String paramMany)
        throws MailPreparationException {
            
            // see if multiple to recipients have been supplied - as a list of authorities
            Serializable authoritiesValue = ruleAction.getParameterValue(paramMany);
            List<String> authorities = null;
            if (authoritiesValue != null)
            {
                if (authoritiesValue instanceof String)
                {
                    authorities = new ArrayList<String>(1);
                    authorities.add((String)authoritiesValue);
                }
                else
                {
                    authorities = (List<String>)authoritiesValue;
                }
            }
            
            List<String> recipients = new ArrayList<String>(authorities.size());
            if (authorities != null && authorities.size() != 0)
            {
                if (logger.isTraceEnabled()) { logger.trace(authorities.size() + " recipient(s) for mail"); }
                
                for (String authority : authorities)
                {
                    final AuthorityType authType = AuthorityType.getAuthorityType(authority);
                    
                    if (logger.isTraceEnabled()) { logger.trace(" authority type: " + authType); }
                    
                    if (authType.equals(AuthorityType.USER))
                    {
                        if (personService.personExists(authority) == true)
                        {
                            NodeRef person = personService.getPerson(authority);
                            String address = (String)nodeService.getProperty(person, ContentModel.PROP_EMAIL);
                            if (address != null && address.length() != 0 && validateAddress(address))
                            {
                                if (logger.isTraceEnabled()) { logger.trace("Recipient (person) exists in Alfresco with known email."); }
                                recipients.add(address);
                            }
                            else
                            {
                                if (logger.isTraceEnabled()) { logger.trace("Recipient (person) exists in Alfresco without known email."); }
                                // If the username looks like an email address, we'll use that.
                                if (validateAddress(authority)) { recipients.add(authority); }
                            }
                        }
                        else
                        {
                            if (logger.isTraceEnabled()) { logger.trace("Recipient does not exist in Alfresco."); }
                            if (validateAddress(authority)) { recipients.add(authority); }
                        }
                    }
                    else if (authType.equals(AuthorityType.GROUP) || authType.equals(AuthorityType.EVERYONE))
                    {
                        if (logger.isTraceEnabled()) { logger.trace("Recipient is a group..."); }
                        // Notify all members of the group
                        Set<String> users;
                        if (authType.equals(AuthorityType.GROUP))
                        {        
                            users = authorityService.getContainedAuthorities(AuthorityType.USER, authority, false);
                        }
                        else
                        {
                            users = authorityService.getAllAuthorities(AuthorityType.USER);
                        }
                        
                        for (String userAuth : users)
                        {
                            if (personService.personExists(userAuth) == true)
                            {
                                NodeRef person = personService.getPerson(userAuth);
                                String address = (String)nodeService.getProperty(person, ContentModel.PROP_EMAIL);
                                if (address != null && address.length() != 0)
                                {
                                    recipients.add(address);
                                    if (logger.isTraceEnabled()) { logger.trace("   Group member email is known."); }
                                }
                                else
                                {
                                    if (logger.isTraceEnabled()) { logger.trace("   Group member email not known."); }
                                    if (validateAddress(authority)) { recipients.add(userAuth); }
                                }
                            }
                            else
                            {
                                if (logger.isTraceEnabled()) { logger.trace("   Group member person not found"); }
                                if (validateAddress(authority)) { recipients.add(userAuth); }
                            }
                        }
                    }
                }
                
                if (logger.isTraceEnabled()) { logger.trace(recipients.size() + " valid recipient(s)."); }
              
            }
            else
            {
                // No recipients have been specified, change to info only
                logger.info(
                        "No recipient has been specified for the mail action in setUpManyRecipients for paramMany[" + paramMany + "]"
                );
            }
            return recipients;
        }
        
        private void sendEmail(final Action ruleAction, MimeMessageHelper preparedMessage)
        {
    
            try
            {
                // Send the message unless we are in "testMode"
                if (!testMode)
                {
                    mailService.send(preparedMessage.getMimeMessage());
                    onSend();
                }
                else
                {
                    lastTestMessage = preparedMessage.getMimeMessage();
                }
            }
            catch (MailException e)
            {
                onFail();
                String to = (String)ruleAction.getParameterValue(PARAM_TO);
                if (to == null)
                {
                   Object obj = ruleAction.getParameterValue(PARAM_TO_MANY);
                   if (obj != null)
                   {
                      to = obj.toString();
                   }
                }
                
                // always log the failure
                logger.error("Failed to send email to " + to, e);
                
                // optionally ignore the throwing of the exception
                Boolean ignoreError = (Boolean)ruleAction.getParameterValue(PARAM_IGNORE_SEND_FAILURE);
                if (ignoreError == null || ignoreError.booleanValue() == false)
                {
                    Object[] args = {to, e.toString()};
                    throw new AlfrescoRuntimeException("email.outbound.err.send.failed", args, e);
                }   
            }
        }
        
        /**
         * Attempt to localize the subject, using the subject parameter as the message key.
         * 
         * @param subject Message key for subject lookup
         * @param params Parameters for the message
         * @param locale Locale to use
         * @return The localized message, or subject if the message format could not be found
         */
        private String getLocalizedSubject(String subject, Object[] params, Locale locale)
        {
            String localizedSubject = null;
            if (locale == null)
            {
                localizedSubject = I18NUtil.getMessage(subject, params);
            }
            else 
            {
                localizedSubject = I18NUtil.getMessage(subject, locale, params);
            }
            
            if (localizedSubject == null)
            {
                return subject;
            }
            else
            {
                return localizedSubject;
            }
            
        }
        
        /**
         * 
         * @param ruleAction
         * @return
         */
        private Pair<InternetAddress, Locale> getFrom(Action ruleAction)
        {
            try 
            {
                InternetAddress address;
                Locale locale = null;
                // from person
                String fromPersonName = null;
                
                if (! authService.isCurrentUserTheSystemUser())
                {
                    String currentUserName = authService.getCurrentUserName();
                    if (currentUserName != null && personExists(currentUserName))
                    {
                        fromPersonName = currentUserName;
                        locale = getLocaleForUser(fromPersonName);
                    }
                }
                        
                if(isFromEnabled())
                {   
                    // Use the FROM parameter in preference to calculating values.
                    String from = (String)ruleAction.getParameterValue(PARAM_FROM);
                    if (from != null && from.length() > 0)
                    {
                        if(logger.isDebugEnabled())
                        {
                            logger.debug("from specified as a parameter, from:" + from);
                        }
                    
                        // Check whether or not to use a personal name for the email (will be RFC 2047 encoded)
                        String fromPersonalName = (String)ruleAction.getParameterValue(PARAM_FROM_PERSONAL_NAME);
                        if(fromPersonalName != null && fromPersonalName.length() > 0) 
                        {
                            try
                            {
                                address = new InternetAddress(from, fromPersonalName);
                            }
                            catch (UnsupportedEncodingException error)
                            {
                                address = new InternetAddress(from);
                            }
                        }
                        else
                        {
                            address = new InternetAddress(from);
                        }
                        if (locale == null)
                        {
                            if (personExists(from))
                            {
                                locale = getLocaleForUser(from);
                            }
                        }
                    }
                    else
                    {
                        // FROM enabled but not not specified                
                        String fromActualUser = fromPersonName;
                        if (fromPersonName != null)
                        {
                             NodeRef fromPerson = getPerson(fromPersonName);
                             fromActualUser = (String) nodeService.getProperty(fromPerson, ContentModel.PROP_EMAIL);
                        }
                  
                        if (fromActualUser != null && fromActualUser.length() != 0)
                        {
                            if(logger.isDebugEnabled())
                            {
                                logger.debug("looked up email address for :" + fromPersonName + " email from " + fromActualUser);
                            }
                            address = new InternetAddress(fromActualUser);
                        }
                        else
                        {
                            // from system or user does not have email address
                            address = new InternetAddress(fromDefaultAddress);
                        }
                    }
                }
                else
                {
                    if(logger.isDebugEnabled())
                    {
                        logger.debug("from not enabled - sending from default address:" + fromDefaultAddress);
                    }
                    // from is not enabled.
                    address = new InternetAddress(fromDefaultAddress);
                }
                
                return new Pair<InternetAddress, Locale>(address, locale);
            }
            catch (MessagingException ex)
            {
                throw new AlfrescoRuntimeException("Failed to resolve sender mail address");
            }
        }
        
        /*
         * NEW CHERRYSHOE: Since we only need maximum one recipient, then we will break out when we have that.
         */
        @SuppressWarnings("unchecked")
        private List<Pair<String, Locale>> getRecipients(Action ruleAction) 
        {
            
            List<Pair<String, Locale>> recipients = new LinkedList<Pair<String,Locale>>();
            
            // set single recipient(s)
            String to = (String)ruleAction.getParameterValue(PARAM_TO);
            String cc = (String)ruleAction.getParameterValue(PARAM_CC); // NEW CHERRYSHOE
            String bcc = (String)ruleAction.getParameterValue(PARAM_BCC); // NEW CHERRYSHOE
            // NEW CHERRYSHOE use apache string utils to check for is blank
            if (!org.apache.commons.lang.StringUtils.isBlank(to) || !org.apache.commons.lang.StringUtils.isBlank(cc) || !org.apache.commons.lang.StringUtils.isBlank(bcc))
            {
                if (!org.apache.commons.lang.StringUtils.isBlank(to))
                {
                    Locale locale = null;
                    if (personExists(to))
                    {
                        locale = getLocaleForUser(to);
                    }
                    recipients.add(new Pair<String, Locale>(to, locale));
                    return recipients; // NEW CHERRYSHOE only need one maximum recipient
                } 
                if (!org.apache.commons.lang.StringUtils.isBlank(cc)) 
                {
                    Locale locale = null;
                    if (personExists(cc))
                    {
                        locale = getLocaleForUser(cc);
                    }
                    recipients.add(new Pair<String, Locale>(cc, locale));
                    return recipients; // NEW CHERRYSHOE only need one maximum recipient
                } 
                if (!org.apache.commons.lang.StringUtils.isBlank(bcc)) {
                    Locale locale = null;
                    if (personExists(bcc))
                    {
                        locale = getLocaleForUser(bcc);
                    }
                    recipients.add(new Pair<String, Locale>(bcc, locale));
                    return recipients; // NEW CHERRYSHOE only need one maximum recipient
                }
            }
            else
            {
                // NEW CHERRYSHOE figure out the manyRecipients, broke out alfresco code into
                // new getManyRecipients method
                recipients.addAll(getManyRecipients(ruleAction, PARAM_TO_MANY));
                if (recipients.size() > 0) return recipients; // NEW CHERRYSHOE only need one maximum recipient
                recipients.addAll(getManyRecipients(ruleAction, PARAM_CC_MANY));
                if (recipients.size() > 0) return recipients; // NEW CHERRYSHOE only need one maximum recipient
                recipients.addAll(getManyRecipients(ruleAction, PARAM_BCC_MANY));
            }
            return recipients;
        }
        
        public boolean personExists(final String user)
        {
            boolean exists = false;
            String domain = tenantService.getPrimaryDomain(user); // get primary tenant 
            if (domain != null) 
            { 
                exists = TenantUtil.runAsTenant(new TenantRunAsWork<Boolean>()
                {
                    public Boolean doWork() throws Exception
                    {
                        return personService.personExists(user);
                    }
                }, domain);
            }
            else
            {
                exists = personService.personExists(user);
            }
            return exists;
        }
        
        public NodeRef getPerson(final String user)
        {
            NodeRef person = null;
            String domain = tenantService.getPrimaryDomain(user); // get primary tenant 
            if (domain != null) 
            { 
                person = TenantUtil.runAsTenant(new TenantRunAsWork<NodeRef>()
                {
                    public NodeRef doWork() throws Exception
                    {
                        return personService.getPerson(user);
                    }
                }, domain);
            }
            else
            {
                person = personService.getPerson(user);
            }
            return person;
        }
        
        public String getPersonEmail(final String user)
        {
            final NodeRef person = getPerson(user);
            String email = null;
            String domain = tenantService.getPrimaryDomain(user); // get primary tenant 
            if (domain != null) 
            { 
                email = TenantUtil.runAsTenant(new TenantRunAsWork<String>()
                {
                    public String doWork() throws Exception
                    {
                        return (String) nodeService.getProperty(person, ContentModel.PROP_EMAIL);
                    }
                }, domain);
            }
            else
            {
                email = (String) nodeService.getProperty(person, ContentModel.PROP_EMAIL);
            }
            return email;
        }
        
        /**
         * Gets the specified user's preferred locale, if available.
         * 
         * @param user the username of the user whose locale is sought.
         * @return the preferred locale for that user, if available, else <tt>null</tt>. The result would be <tt>null</tt>
         *         e.g. if the user does not exist in the system.
         */
        private Locale getLocaleForUser(final String user)
        {
            Locale locale = null;
            String localeString = null;
            
            // get primary tenant for the specified user.
            //
            // This can have one of (at least) 3 values currently:
            // 1. In single-tenant (community/enterprise) this will be the empty string.
            // 2. In the cloud, for a username such as this: joe.soap@acme.com:
            //    2A. If the acme.com tenant exists in the system, the primary domain is "acme.com"
            //    2B. Id the acme.xom tenant does not exist in the system, the primary domain is null.
            String domain = tenantService.getPrimaryDomain(user);
            
            if (domain != null) 
            { 
                // If the domain is not null, then the user exists in the system and we may get a preferred locale.
                localeString = TenantUtil.runAsSystemTenant(new TenantRunAsWork<String>()
                {
                    public String doWork() throws Exception
                    {
                        return (String) preferenceService.getPreference(user, "locale");
                    }
                }, domain);
            }
            else
            {
                // If the domain is null, then the beahviour here varies depending on whether it's a single tenant or multi-tenant cloud.
                if (personExists(user))
                {
                    localeString = (String) preferenceService.getPreference(user, "locale");
                }
                // else leave it as null - there's no tenant, no user for that username, so we can't get a preferred locale.
            }
            
            if (localeString != null)
            {
                locale = StringUtils.parseLocaleString(localeString);
            }
    
            return locale;
        }
        
        /**
         * Return true if address has valid format
         * @param address
         * @return
         */
        private boolean validateAddress(String address)
        {
            boolean result = false;
            
            // Validate the email, allowing for local email addresses
            EmailValidator emailValidator = EmailValidator.getInstance(true);
            if (!validateAddresses || emailValidator.isValid(address))
            {
                result = true;
            }
            else 
            {
                logger.error("Failed to send email to '" + address + "' as the address is incorrectly formatted" );
            }
          
            return result;
        }
    
       /**
        * @param ref    The node representing the current document ref (or null)
        * 
        * @return Model map for email templates
        */
       private Map<String, Object> createEmailTemplateModel(NodeRef ref, Map<String, Object> suppliedModel, NodeRef fromPerson)
       {
          Map<String, Object> model = new HashMap<String, Object>(8, 1.0f);
          
          if (fromPerson != null)
          {
              model.put("person", new TemplateNode(fromPerson, serviceRegistry, null));
          }      
          
          if (ref != null)
          {
              model.put("document", new TemplateNode(ref, serviceRegistry, null));
              NodeRef parent = serviceRegistry.getNodeService().getPrimaryParent(ref).getParentRef();
              model.put("space", new TemplateNode(parent, serviceRegistry, null));
          }
          
          // current date/time is useful to have and isn't supplied by FreeMarker by default
          model.put("date", new Date());
          
          // add custom method objects
          model.put("hasAspect", new HasAspectMethod());
          model.put("message", new I18NMessageMethod());
          model.put("dateCompare", new DateCompareMethod());
          
          // add URLs
          model.put("url", new URLHelper(sysAdminParams));
          model.put(TemplateService.KEY_SHARE_URL, UrlUtil.getShareUrl(this.serviceRegistry.getSysAdminParams()));
    
          if (imageResolver != null)
          {
              model.put(TemplateService.KEY_IMAGE_RESOLVER, imageResolver);
          }
    
          // if the caller specified a model, use it without overriding
          if(suppliedModel != null && suppliedModel.size() > 0)
          {
              for(String key : suppliedModel.keySet())
              {
                  if(model.containsKey(key))
                  {
                      if(logger.isDebugEnabled())
                      {
                          logger.debug("Not allowing overwriting of built in model parameter " + key);
                      }
                  }
                  else
                  {
                      model.put(key, suppliedModel.get(key));
                  }
              }
          }
          
          // all done
          return model;
       }
        
        /**
         * Add the parameter definitions
         */
        @Override
        protected void addParameterDefinitions(List<ParameterDefinition> paramList) 
        {
            paramList.add(new ParameterDefinitionImpl(PARAM_TO, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_TO)));
            paramList.add(new ParameterDefinitionImpl(PARAM_TO_MANY, DataTypeDefinition.ANY, false, getParamDisplayLabel(PARAM_TO_MANY), true));
            
            // NEW CHERRYSHOE add CC and BCC support
            paramList.add(new ParameterDefinitionImpl(PARAM_CC, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_CC)));
            paramList.add(new ParameterDefinitionImpl(PARAM_BCC, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_BCC)));
            paramList.add(new ParameterDefinitionImpl(PARAM_CC_MANY, DataTypeDefinition.ANY, false, getParamDisplayLabel(PARAM_CC_MANY), true));
            paramList.add(new ParameterDefinitionImpl(PARAM_BCC_MANY, DataTypeDefinition.ANY, false, getParamDisplayLabel(PARAM_BCC_MANY), true));
            
            paramList.add(new ParameterDefinitionImpl(PARAM_SUBJECT, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_SUBJECT)));
            paramList.add(new ParameterDefinitionImpl(PARAM_TEXT, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_TEXT)));
            paramList.add(new ParameterDefinitionImpl(PARAM_FROM, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_FROM)));
            paramList.add(new ParameterDefinitionImpl(PARAM_TEMPLATE, DataTypeDefinition.NODE_REF, false, getParamDisplayLabel(PARAM_TEMPLATE), false, "ac-email-templates"));
            paramList.add(new ParameterDefinitionImpl(PARAM_TEMPLATE_MODEL, DataTypeDefinition.ANY, false, getParamDisplayLabel(PARAM_TEMPLATE_MODEL), true));
            paramList.add(new ParameterDefinitionImpl(PARAM_IGNORE_SEND_FAILURE, DataTypeDefinition.BOOLEAN, false, getParamDisplayLabel(PARAM_IGNORE_SEND_FAILURE)));
        }
    
        public void setTestMode(boolean testMode)
        {
            this.testMode = testMode;
        }
    
        public boolean isTestMode()
        {
            return testMode;
        }
    
        /**
         * Returns the most recent message that wasn't sent
         *  because TestMode had been enabled.
         */
        public MimeMessage retrieveLastTestMessage()
        {
            return lastTestMessage; 
        }
        
        /**
         * Used when test mode is enabled.
         * Clears the record of the last message that was sent. 
         */
        public void clearLastTestMessage()
        {
            lastTestMessage = null;
        }
    
        public void setFromEnabled(boolean fromEnabled)
        {
            this.fromEnabled = fromEnabled;
        }
    
        public boolean isFromEnabled()
        {
            return fromEnabled;
        }
    
        public static boolean isHTML(String value)
        {
            boolean result = false;
    
            // Note: only simplistic match here - expects <html tag at the start of the text
            String htmlPrefix = "<html";
            String trimmedText = value.trim();
            if (trimmedText.length() >= htmlPrefix.length() &&
                    trimmedText.substring(0, htmlPrefix.length()).equalsIgnoreCase(htmlPrefix))
            {
                result = true;
            }
    
            return result;
        }
    
        public static class URLHelper
        {
            private final SysAdminParams sysAdminParams;
            
            public URLHelper(SysAdminParams sysAdminParams)
            {
                this.sysAdminParams = sysAdminParams;
            }
            
            public String getContext()
            {
               return "/" + sysAdminParams.getAlfrescoContext();
            }
    
            public String getServerPath()
            {
                return sysAdminParams.getAlfrescoProtocol() + "://" + sysAdminParams.getAlfrescoHost() + ":"
                        + sysAdminParams.getAlfrescoPort();
            }
        }
        
        static AtomicInteger numberSuccessfulSends = new AtomicInteger(0);
        static AtomicInteger numberFailedSends = new AtomicInteger(0);
        protected void onSend()
        {
            numberSuccessfulSends.getAndIncrement();
        }
        
        protected void onFail()
        {
            numberFailedSends.getAndIncrement();
        }
        
        public int getNumberSuccessfulSends()
        {
            return numberSuccessfulSends.get();
        }
            
        public int getNumberFailedSends()
        {   
            return numberFailedSends.get();
        }
    }
    
    
  3. I also went ahead and created a custom version of MailActionExecuterMonitor, only changing the package to your new custom package (i.e. com.cherryshoe.action.executer).  I realize I probably didn't have to do this, since nothing was changed in the code besides the package.
    package com.cherryshoe.action.executer;
    
    import org.alfresco.error.AlfrescoRuntimeException;
    import org.springframework.extensions.surf.util.I18NUtil;
    
    public class MailActionExecuterMonitor
    {
        private MailActionExecuter mailActionExceuter;
        
        public String sendTestMessage()
        {
            try
            {
                mailActionExceuter.sendTestMessage();
                Object[] params = {mailActionExceuter.getTestMessageTo()};
                String message = I18NUtil.getMessage("email.outbound.test.send.success", params);
                return message;
            }
            catch
            (AlfrescoRuntimeException are)
            {
                return (are.getMessage());
            }
        }
        public int getNumberFailedSends()
        {
            return mailActionExceuter.getNumberFailedSends();
        }
        
        public int getNumberSuccessfulSends()
        {
            return mailActionExceuter.getNumberSuccessfulSends();
        }
        
        public void setMailActionExecuter(MailActionExecuter mailActionExceuter)
        {
            this.mailActionExceuter = mailActionExceuter;
        }
    }
    
    
    
  4. Overwrite alfresco's outboundSMTP-context.xml spring config file to point to your new custom MailActionExecuter and MailActionExecuterMonitor,  Just put this in the correct location in your AMP file and it will override the OOB location.  
    • Changes in the MailActionExecuterMonitor are indicated by comments that start with "NEW CHERRYSHOE" that tell you what was changed and why. TODO: I should do more research here as to see about not completely overwriting alfresco's version, and see if there was a way to override it.
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
    
    <!-- NEW CHERRYSHOE Override Alfresco OOB outboundSMTP-context.xml to add functionality
    for MailActionExecuter -->
    <beans>
       
       <!--                        -->
       <!-- MAIL SERVICE           -->
       <!--                        -->
        <import resource="classpath:alfresco/subsystems/email/OutboundSMTP/mail-template-services-context.xml"/>
       
        <bean id="mailService" class="org.alfresco.repo.mail.AlfrescoJavaMailSender">
          <property name="host">
             <value>${mail.host}</value>
          </property>
          <property name="port">
             <value>${mail.port}</value>
          </property>
            <property name="protocol">
             <value>${mail.protocol}</value>
          </property>
          <property name="username">
             <value>${mail.username}</value>
          </property>
          <property name="password">
             <value>${mail.password}</value>
          </property>
          <property name="defaultEncoding">
             <value>${mail.encoding}</value>
          </property>
            <property name="javaMailProperties">
            <props>
                    <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                    <prop key="mail.smtp.debug">${mail.smtp.debug}</prop>
                    <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
                    <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
                
                    <prop key="mail.smtps.auth">${mail.smtps.auth}</prop>
                    <prop key="mail.smtps.starttls.enable">${mail.smtps.starttls.enable}</prop>
            </props>
        </property>
            <property name="maxActive" value="${mail.transports.maxActive}"/>
            <property name="maxIdle" value="${mail.transports.maxIdle}"/>
            <property name="maxWait" value="${mail.tranports.maxWait}"/>
            <property name="minEvictableIdleTime" value="${mail.tranports.minEvictableIdleTime}"/>
            <property name="timeBetweenEvictionRuns" value="${mail.tranports.timeBetweenEvictionRuns}"/>
       </bean>
       
        <!-- NEW CHERRYSHOE Override the OOB MailActionExecuter with custom specific -->
        <bean id="mail" class="com.cherryshoe.action.executer.MailActionExecuter" parent="action-executer">
            <property name="queueName" value="outboundMail"/>
            <property name="mailService">
                <ref bean="mailService"></ref>
            </property>
            <property name="templateService">
                <ref bean="mailTemplateService"></ref>
            </property>
            <property name="personService">
                <ref bean="personService"></ref>
            </property>
            <property name="authenticationService">
                <ref bean="authenticationService"></ref>
            </property>
            <property name="nodeService">
                <ref bean="nodeService"></ref>
            </property>
            <property name="tenantService">
                <ref bean="tenantService"></ref>
            </property>
            <property name="authorityService">
                <ref bean="authorityService"></ref>
            </property>
            <property name="preferenceService">
                <ref bean="preferenceService"></ref>
            </property>
            <property name="serviceRegistry">
                <ref bean="ServiceRegistry"></ref>
            </property>
            <property name="imageResolver" ref="defaultImageResolver" />
            <property name="headerEncoding">
                <value>${mail.header}</value>
            </property>
            <property name="validateAddresses">
                <value>${mail.validate.addresses}</value>
                    </property>
                    <property name="fromAddress">
                            <value>${mail.from.default}</value>
                    </property>
                    <property name="fromEnabled">
             <value>${mail.from.enabled}</value>
            </property>
            <property name="sysAdminParams">
                <ref bean="sysAdminParams"></ref>
            </property>
            <property name="sendTestMessage">
                <value>${mail.testmessage.send}</value>
            </property>
            <property name="testMessageTo">
                <value>${mail.testmessage.to}</value>
            </property>
            <property name="testMessageSubject">
                <value>${mail.testmessage.subject}</value>
            </property>
            <property name="testMessageText">
                 <value>${mail.testmessage.text}</value>
            </property>
    
            <!-- For dev/test only - emails sent to test email, irrespective of intended recipient -->
            <property name="testModeRecipient" value="${dev.email.recipient.address}"/>
    
            <!-- For dev/test only - if true, emails are not actually sent -->
            <property name="testMode" value="${dev.email.not.sent}"/>
              
            <!-- NEW CHERRYSHOE I can't remember why I added this, must be in the class that MailActionExecuter is
            inherited from ActionExecuterAbstractBase -->
            <property name="publicAction">
                <value>false</value>
            </property>
        </bean>
         
        <!--  Put analytics actions on a dedicated asynchronous queue to ensure that they will not block
              any other ongoing asynchronous events in the system. -->
        <bean id="mailAsyncThreadPool" class="org.alfresco.util.ThreadPoolExecutorFactoryBean">
            <property name="poolName">
                <value>mailAsyncAction</value>
            </property>
            <property name="corePoolSize">
                <value>${mail.service.corePoolSize}</value>
            </property>
            <property name="maximumPoolSize">
                <value>${mail.service.maximumPoolSize}</value>
            </property>
        </bean>
        
        <bean id="mailAsynchronousActionExecutionQueue" class="org.alfresco.repo.action.AsynchronousActionExecutionQueueImpl" init-method="init">
          <property name="actionServiceImpl" ref="actionService"/>
          <property name="threadPoolExecutor">
             <ref bean="mailAsyncThreadPool"/>
          </property>
          <property name="transactionService">
             <ref bean="transactionService"/>
          </property>
          <property name="policyComponent">
             <ref bean="policyComponent"/>
          </property>
          <property name="id" value="outboundMail"/>
        </bean>
    
        <!-- NEW CHERRYSHOE Override the OOB MailActionExecuterMonitor with custom specific -->   
        <bean id="monitor" class="com.cherryshoe.action.executer.MailActionExecuterMonitor" >
            <property name="mailActionExecuter">
                <ref bean="mail"></ref>
            </property>
        </bean>
    
    </beans>

1 comment:

I appreciate your time in leaving a comment!