@Test public void testDisableHtml() throws Exception { setup(false); commonSetup(); recipientIndex.put(RECIPIENT2, person2a); Collection<UserActionRequest> results = sut.notify(NOTIFICATION_TYPE_NO_REPLY, recipients, Collections.EMPTY_MAP, recipientIndex); mockery.assertIsSatisfied(); NotificationEmailDTO request = assertGetSingleResult(results); assertNull(request.getHtmlBody()); assertEquals(TEXT_BODY_RENDERED, request.getTextBody()); assertEquals(PREFIX + SUBJECT_RENDERED, request.getSubject()); assertEquals(EMAIL1, request.getToRecipient()); assertTrue(request.getBccRecipients() == null || request.getBccRecipients().isEmpty()); assertTrue(request.getDescription() != null && !request.getDescription().isEmpty()); assertFalse(request.isHighPriority()); assertNull(request.getReplyTo()); }
@Override public Collection<UserActionRequest> notify(final NotificationType inType, final Collection<Long> inRecipients, final Map<String, Object> inProperties, final Map<Long, PersonModelView> inRecipientIndex) throws Exception { EmailNotificationTemplate template = templates.get(inType); if (template == null) { return null; } List<String> addresses = new ArrayList<String>(inRecipients.size()); Map<String, String> addressesWithTokens = Collections.EMPTY_MAP; if (template.getReplyAddressType() == ReplyAction.COMMENT) { Object obj = inProperties.get("activity"); if (obj == null || !(obj instanceof ActivityDTO)) { throw new ExecutionException("Notification requires activity property for building token."); } ActivityDTO activity = (ActivityDTO) obj; addressesWithTokens = new HashMap<String, String>(inRecipients.size()); String tokenData = tokenContentFormatter.buildForActivity(activity.getId()); boolean generallyAllowed = activityAuthorizer.authorize(activity, ActivityInteractionType.COMMENT, false); for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { if (generallyAllowed || activityAuthorizer.authorize(recipientId, activity, ActivityInteractionType.COMMENT)) { String replyAddress = tokenAddressBuilder.build(tokenData, recipientId); addressesWithTokens.put(address, replyAddress); } else { addresses.add(address); } } } } else { for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { addresses.add(address); } } } int emailCount = addressesWithTokens.size() + (addresses.isEmpty() ? 0 : 1); if (emailCount == 0) { return null; } List<UserActionRequest> requests = new ArrayList<UserActionRequest>(emailCount); NotificationEmailDTO email = new NotificationEmailDTO(); Context velocityContext = new VelocityContext(new VelocityContext(inProperties, velocityGlobalContext)); velocityContext.put("context", velocityContext); velocityContext.put("type", inType); if (addressesWithTokens.size() + addresses.size() == 1) { velocityContext.put("recipient", inRecipientIndex.get(inRecipients.iterator().next())); } StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + inType, template.getSubjectTemplate()); email.setSubject(subjectPrefix + writer.toString()); email.setHighPriority(Boolean.TRUE.equals(inProperties.get(NotificationPropertyKeys.HIGH_PRIORITY))); String noReplyTextBody = null; String replyTextBody = null; String noReplyHtmlBody = null; String replyHtmlBody = null; Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyTextBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyTextBody = writer.toString(); } if (sendHtml) { final String htmlBodyTemplateResourcePath = template.getHtmlBodyTemplateResourcePath(); if (htmlBodyTemplateResourcePath != null) { vt = velocityEngine.getTemplate(htmlBodyTemplateResourcePath); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyHtmlBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyHtmlBody = writer.toString(); } } } if (!addressesWithTokens.isEmpty()) { email.setTextBody(replyTextBody); if (replyHtmlBody != null) { email.setHtmlBody(replyHtmlBody); } for (Entry<String, String> entry : addressesWithTokens.entrySet()) { NotificationEmailDTO userEmail = email.clone(); userEmail.setReplyTo(entry.getValue()); String address = entry.getKey(); userEmail.setToRecipient(address); userEmail.setDescription(inType + " with token to " + address); requests.add(new UserActionRequest("sendEmailNotificationAction", null, userEmail)); } } if (!addresses.isEmpty()) { email.setTextBody(noReplyTextBody); if (noReplyHtmlBody != null) { email.setHtmlBody(noReplyHtmlBody); } if (addresses.size() == 1) { final String address = addresses.get(0); email.setToRecipient(address); email.setDescription(inType + " to " + address); } else { email.setBccRecipients(StringUtils.join(addresses, ',')); email.setDescription(inType + " to " + inRecipients.size() + " recipients"); } if (template.getReplyAddressType() == ReplyAction.ACTOR) { Object obj = inProperties.get(NotificationPropertyKeys.ACTOR); if (obj instanceof HasEmail) { HasEmail actor = (HasEmail) obj; String actorEmail = actor.getEmail(); if (StringUtils.isNotBlank(actorEmail)) { email.setReplyTo(actorEmail); } } } requests.add(new UserActionRequest("sendEmailNotificationAction", null, email)); } return requests; }
EmailNotifier implements Notifier { @Override public Collection<UserActionRequest> notify(final NotificationType inType, final Collection<Long> inRecipients, final Map<String, Object> inProperties, final Map<Long, PersonModelView> inRecipientIndex) throws Exception { EmailNotificationTemplate template = templates.get(inType); if (template == null) { return null; } List<String> addresses = new ArrayList<String>(inRecipients.size()); Map<String, String> addressesWithTokens = Collections.EMPTY_MAP; if (template.getReplyAddressType() == ReplyAction.COMMENT) { Object obj = inProperties.get("activity"); if (obj == null || !(obj instanceof ActivityDTO)) { throw new ExecutionException("Notification requires activity property for building token."); } ActivityDTO activity = (ActivityDTO) obj; addressesWithTokens = new HashMap<String, String>(inRecipients.size()); String tokenData = tokenContentFormatter.buildForActivity(activity.getId()); boolean generallyAllowed = activityAuthorizer.authorize(activity, ActivityInteractionType.COMMENT, false); for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { if (generallyAllowed || activityAuthorizer.authorize(recipientId, activity, ActivityInteractionType.COMMENT)) { String replyAddress = tokenAddressBuilder.build(tokenData, recipientId); addressesWithTokens.put(address, replyAddress); } else { addresses.add(address); } } } } else { for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { addresses.add(address); } } } int emailCount = addressesWithTokens.size() + (addresses.isEmpty() ? 0 : 1); if (emailCount == 0) { return null; } List<UserActionRequest> requests = new ArrayList<UserActionRequest>(emailCount); NotificationEmailDTO email = new NotificationEmailDTO(); Context velocityContext = new VelocityContext(new VelocityContext(inProperties, velocityGlobalContext)); velocityContext.put("context", velocityContext); velocityContext.put("type", inType); if (addressesWithTokens.size() + addresses.size() == 1) { velocityContext.put("recipient", inRecipientIndex.get(inRecipients.iterator().next())); } StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + inType, template.getSubjectTemplate()); email.setSubject(subjectPrefix + writer.toString()); email.setHighPriority(Boolean.TRUE.equals(inProperties.get(NotificationPropertyKeys.HIGH_PRIORITY))); String noReplyTextBody = null; String replyTextBody = null; String noReplyHtmlBody = null; String replyHtmlBody = null; Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyTextBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyTextBody = writer.toString(); } if (sendHtml) { final String htmlBodyTemplateResourcePath = template.getHtmlBodyTemplateResourcePath(); if (htmlBodyTemplateResourcePath != null) { vt = velocityEngine.getTemplate(htmlBodyTemplateResourcePath); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyHtmlBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyHtmlBody = writer.toString(); } } } if (!addressesWithTokens.isEmpty()) { email.setTextBody(replyTextBody); if (replyHtmlBody != null) { email.setHtmlBody(replyHtmlBody); } for (Entry<String, String> entry : addressesWithTokens.entrySet()) { NotificationEmailDTO userEmail = email.clone(); userEmail.setReplyTo(entry.getValue()); String address = entry.getKey(); userEmail.setToRecipient(address); userEmail.setDescription(inType + " with token to " + address); requests.add(new UserActionRequest("sendEmailNotificationAction", null, userEmail)); } } if (!addresses.isEmpty()) { email.setTextBody(noReplyTextBody); if (noReplyHtmlBody != null) { email.setHtmlBody(noReplyHtmlBody); } if (addresses.size() == 1) { final String address = addresses.get(0); email.setToRecipient(address); email.setDescription(inType + " to " + address); } else { email.setBccRecipients(StringUtils.join(addresses, ',')); email.setDescription(inType + " to " + inRecipients.size() + " recipients"); } if (template.getReplyAddressType() == ReplyAction.ACTOR) { Object obj = inProperties.get(NotificationPropertyKeys.ACTOR); if (obj instanceof HasEmail) { HasEmail actor = (HasEmail) obj; String actorEmail = actor.getEmail(); if (StringUtils.isNotBlank(actorEmail)) { email.setReplyTo(actorEmail); } } } requests.add(new UserActionRequest("sendEmailNotificationAction", null, email)); } return requests; } }
EmailNotifier implements Notifier { @Override public Collection<UserActionRequest> notify(final NotificationType inType, final Collection<Long> inRecipients, final Map<String, Object> inProperties, final Map<Long, PersonModelView> inRecipientIndex) throws Exception { EmailNotificationTemplate template = templates.get(inType); if (template == null) { return null; } List<String> addresses = new ArrayList<String>(inRecipients.size()); Map<String, String> addressesWithTokens = Collections.EMPTY_MAP; if (template.getReplyAddressType() == ReplyAction.COMMENT) { Object obj = inProperties.get("activity"); if (obj == null || !(obj instanceof ActivityDTO)) { throw new ExecutionException("Notification requires activity property for building token."); } ActivityDTO activity = (ActivityDTO) obj; addressesWithTokens = new HashMap<String, String>(inRecipients.size()); String tokenData = tokenContentFormatter.buildForActivity(activity.getId()); boolean generallyAllowed = activityAuthorizer.authorize(activity, ActivityInteractionType.COMMENT, false); for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { if (generallyAllowed || activityAuthorizer.authorize(recipientId, activity, ActivityInteractionType.COMMENT)) { String replyAddress = tokenAddressBuilder.build(tokenData, recipientId); addressesWithTokens.put(address, replyAddress); } else { addresses.add(address); } } } } else { for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { addresses.add(address); } } } int emailCount = addressesWithTokens.size() + (addresses.isEmpty() ? 0 : 1); if (emailCount == 0) { return null; } List<UserActionRequest> requests = new ArrayList<UserActionRequest>(emailCount); NotificationEmailDTO email = new NotificationEmailDTO(); Context velocityContext = new VelocityContext(new VelocityContext(inProperties, velocityGlobalContext)); velocityContext.put("context", velocityContext); velocityContext.put("type", inType); if (addressesWithTokens.size() + addresses.size() == 1) { velocityContext.put("recipient", inRecipientIndex.get(inRecipients.iterator().next())); } StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + inType, template.getSubjectTemplate()); email.setSubject(subjectPrefix + writer.toString()); email.setHighPriority(Boolean.TRUE.equals(inProperties.get(NotificationPropertyKeys.HIGH_PRIORITY))); String noReplyTextBody = null; String replyTextBody = null; String noReplyHtmlBody = null; String replyHtmlBody = null; Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyTextBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyTextBody = writer.toString(); } if (sendHtml) { final String htmlBodyTemplateResourcePath = template.getHtmlBodyTemplateResourcePath(); if (htmlBodyTemplateResourcePath != null) { vt = velocityEngine.getTemplate(htmlBodyTemplateResourcePath); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyHtmlBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyHtmlBody = writer.toString(); } } } if (!addressesWithTokens.isEmpty()) { email.setTextBody(replyTextBody); if (replyHtmlBody != null) { email.setHtmlBody(replyHtmlBody); } for (Entry<String, String> entry : addressesWithTokens.entrySet()) { NotificationEmailDTO userEmail = email.clone(); userEmail.setReplyTo(entry.getValue()); String address = entry.getKey(); userEmail.setToRecipient(address); userEmail.setDescription(inType + " with token to " + address); requests.add(new UserActionRequest("sendEmailNotificationAction", null, userEmail)); } } if (!addresses.isEmpty()) { email.setTextBody(noReplyTextBody); if (noReplyHtmlBody != null) { email.setHtmlBody(noReplyHtmlBody); } if (addresses.size() == 1) { final String address = addresses.get(0); email.setToRecipient(address); email.setDescription(inType + " to " + address); } else { email.setBccRecipients(StringUtils.join(addresses, ',')); email.setDescription(inType + " to " + inRecipients.size() + " recipients"); } if (template.getReplyAddressType() == ReplyAction.ACTOR) { Object obj = inProperties.get(NotificationPropertyKeys.ACTOR); if (obj instanceof HasEmail) { HasEmail actor = (HasEmail) obj; String actorEmail = actor.getEmail(); if (StringUtils.isNotBlank(actorEmail)) { email.setReplyTo(actorEmail); } } } requests.add(new UserActionRequest("sendEmailNotificationAction", null, email)); } return requests; } EmailNotifier(final VelocityEngine inVelocityEngine, final Context inVelocityGlobalContext,
            final Map<NotificationType, EmailNotificationTemplate> inTemplates, final String inSubjectPrefix,
            final TokenContentFormatter inTokenContentFormatter,
            final TokenContentEmailAddressBuilder inTokenAddressBuilder,
            final ActivityInteractionAuthorizationStrategy inActivityAuthorizer, final boolean inSendHtml); }
EmailNotifier implements Notifier { @Override public Collection<UserActionRequest> notify(final NotificationType inType, final Collection<Long> inRecipients, final Map<String, Object> inProperties, final Map<Long, PersonModelView> inRecipientIndex) throws Exception { EmailNotificationTemplate template = templates.get(inType); if (template == null) { return null; } List<String> addresses = new ArrayList<String>(inRecipients.size()); Map<String, String> addressesWithTokens = Collections.EMPTY_MAP; if (template.getReplyAddressType() == ReplyAction.COMMENT) { Object obj = inProperties.get("activity"); if (obj == null || !(obj instanceof ActivityDTO)) { throw new ExecutionException("Notification requires activity property for building token."); } ActivityDTO activity = (ActivityDTO) obj; addressesWithTokens = new HashMap<String, String>(inRecipients.size()); String tokenData = tokenContentFormatter.buildForActivity(activity.getId()); boolean generallyAllowed = activityAuthorizer.authorize(activity, ActivityInteractionType.COMMENT, false); for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { if (generallyAllowed || activityAuthorizer.authorize(recipientId, activity, ActivityInteractionType.COMMENT)) { String replyAddress = tokenAddressBuilder.build(tokenData, recipientId); addressesWithTokens.put(address, replyAddress); } else { addresses.add(address); } } } } else { for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { addresses.add(address); } } } int emailCount = addressesWithTokens.size() + (addresses.isEmpty() ? 0 : 1); if (emailCount == 0) { return null; } List<UserActionRequest> requests = new ArrayList<UserActionRequest>(emailCount); NotificationEmailDTO email = new NotificationEmailDTO(); Context velocityContext = new VelocityContext(new VelocityContext(inProperties, velocityGlobalContext)); velocityContext.put("context", velocityContext); velocityContext.put("type", inType); if (addressesWithTokens.size() + addresses.size() == 1) { velocityContext.put("recipient", inRecipientIndex.get(inRecipients.iterator().next())); } StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + inType, template.getSubjectTemplate()); email.setSubject(subjectPrefix + writer.toString()); email.setHighPriority(Boolean.TRUE.equals(inProperties.get(NotificationPropertyKeys.HIGH_PRIORITY))); String noReplyTextBody = null; String replyTextBody = null; String noReplyHtmlBody = null; String replyHtmlBody = null; Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyTextBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyTextBody = writer.toString(); } if (sendHtml) { final String htmlBodyTemplateResourcePath = template.getHtmlBodyTemplateResourcePath(); if (htmlBodyTemplateResourcePath != null) { vt = velocityEngine.getTemplate(htmlBodyTemplateResourcePath); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyHtmlBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyHtmlBody = writer.toString(); } } } if (!addressesWithTokens.isEmpty()) { email.setTextBody(replyTextBody); if (replyHtmlBody != null) { email.setHtmlBody(replyHtmlBody); } for (Entry<String, String> entry : addressesWithTokens.entrySet()) { NotificationEmailDTO userEmail = email.clone(); userEmail.setReplyTo(entry.getValue()); String address = entry.getKey(); userEmail.setToRecipient(address); userEmail.setDescription(inType + " with token to " + address); requests.add(new UserActionRequest("sendEmailNotificationAction", null, userEmail)); } } if (!addresses.isEmpty()) { email.setTextBody(noReplyTextBody); if (noReplyHtmlBody != null) { email.setHtmlBody(noReplyHtmlBody); } if (addresses.size() == 1) { final String address = addresses.get(0); email.setToRecipient(address); email.setDescription(inType + " to " + address); } else { email.setBccRecipients(StringUtils.join(addresses, ',')); email.setDescription(inType + " to " + inRecipients.size() + " recipients"); } if (template.getReplyAddressType() == ReplyAction.ACTOR) { Object obj = inProperties.get(NotificationPropertyKeys.ACTOR); if (obj instanceof HasEmail) { HasEmail actor = (HasEmail) obj; String actorEmail = actor.getEmail(); if (StringUtils.isNotBlank(actorEmail)) { email.setReplyTo(actorEmail); } } } requests.add(new UserActionRequest("sendEmailNotificationAction", null, email)); } return requests; } EmailNotifier(final VelocityEngine inVelocityEngine, final Context inVelocityGlobalContext,
            final Map<NotificationType, EmailNotificationTemplate> inTemplates, final String inSubjectPrefix,
            final TokenContentFormatter inTokenContentFormatter,
            final TokenContentEmailAddressBuilder inTokenAddressBuilder,
            final ActivityInteractionAuthorizationStrategy inActivityAuthorizer, final boolean inSendHtml); @Override Collection<UserActionRequest> notify(final NotificationType inType, final Collection<Long> inRecipients,
            final Map<String, Object> inProperties, final Map<Long, PersonModelView> inRecipientIndex); }
EmailNotifier implements Notifier { @Override public Collection<UserActionRequest> notify(final NotificationType inType, final Collection<Long> inRecipients, final Map<String, Object> inProperties, final Map<Long, PersonModelView> inRecipientIndex) throws Exception { EmailNotificationTemplate template = templates.get(inType); if (template == null) { return null; } List<String> addresses = new ArrayList<String>(inRecipients.size()); Map<String, String> addressesWithTokens = Collections.EMPTY_MAP; if (template.getReplyAddressType() == ReplyAction.COMMENT) { Object obj = inProperties.get("activity"); if (obj == null || !(obj instanceof ActivityDTO)) { throw new ExecutionException("Notification requires activity property for building token."); } ActivityDTO activity = (ActivityDTO) obj; addressesWithTokens = new HashMap<String, String>(inRecipients.size()); String tokenData = tokenContentFormatter.buildForActivity(activity.getId()); boolean generallyAllowed = activityAuthorizer.authorize(activity, ActivityInteractionType.COMMENT, false); for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { if (generallyAllowed || activityAuthorizer.authorize(recipientId, activity, ActivityInteractionType.COMMENT)) { String replyAddress = tokenAddressBuilder.build(tokenData, recipientId); addressesWithTokens.put(address, replyAddress); } else { addresses.add(address); } } } } else { for (long recipientId : inRecipients) { String address = inRecipientIndex.get(recipientId).getEmail(); if (StringUtils.isNotBlank(address)) { addresses.add(address); } } } int emailCount = addressesWithTokens.size() + (addresses.isEmpty() ? 0 : 1); if (emailCount == 0) { return null; } List<UserActionRequest> requests = new ArrayList<UserActionRequest>(emailCount); NotificationEmailDTO email = new NotificationEmailDTO(); Context velocityContext = new VelocityContext(new VelocityContext(inProperties, velocityGlobalContext)); velocityContext.put("context", velocityContext); velocityContext.put("type", inType); if (addressesWithTokens.size() + addresses.size() == 1) { velocityContext.put("recipient", inRecipientIndex.get(inRecipients.iterator().next())); } StringWriter writer = new StringWriter(); velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + inType, template.getSubjectTemplate()); email.setSubject(subjectPrefix + writer.toString()); email.setHighPriority(Boolean.TRUE.equals(inProperties.get(NotificationPropertyKeys.HIGH_PRIORITY))); String noReplyTextBody = null; String replyTextBody = null; String noReplyHtmlBody = null; String replyHtmlBody = null; Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath()); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyTextBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyTextBody = writer.toString(); } if (sendHtml) { final String htmlBodyTemplateResourcePath = template.getHtmlBodyTemplateResourcePath(); if (htmlBodyTemplateResourcePath != null) { vt = velocityEngine.getTemplate(htmlBodyTemplateResourcePath); EventCartridge ec = new EventCartridge(); ec.addEventHandler(new EscapeHtmlReference()); ec.attachToContext(velocityContext); if (!addresses.isEmpty()) { velocityContext.put("hasReplyAddress", false); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); noReplyHtmlBody = writer.toString(); } if (!addressesWithTokens.isEmpty()) { velocityContext.put("hasReplyAddress", true); writer.getBuffer().setLength(0); vt.merge(velocityContext, writer); replyHtmlBody = writer.toString(); } } } if (!addressesWithTokens.isEmpty()) { email.setTextBody(replyTextBody); if (replyHtmlBody != null) { email.setHtmlBody(replyHtmlBody); } for (Entry<String, String> entry : addressesWithTokens.entrySet()) { NotificationEmailDTO userEmail = email.clone(); userEmail.setReplyTo(entry.getValue()); String address = entry.getKey(); userEmail.setToRecipient(address); userEmail.setDescription(inType + " with token to " + address); requests.add(new UserActionRequest("sendEmailNotificationAction", null, userEmail)); } } if (!addresses.isEmpty()) { email.setTextBody(noReplyTextBody); if (noReplyHtmlBody != null) { email.setHtmlBody(noReplyHtmlBody); } if (addresses.size() == 1) { final String address = addresses.get(0); email.setToRecipient(address); email.setDescription(inType + " to " + address); } else { email.setBccRecipients(StringUtils.join(addresses, ',')); email.setDescription(inType + " to " + inRecipients.size() + " recipients"); } if (template.getReplyAddressType() == ReplyAction.ACTOR) { Object obj = inProperties.get(NotificationPropertyKeys.ACTOR); if (obj instanceof HasEmail) { HasEmail actor = (HasEmail) obj; String actorEmail = actor.getEmail(); if (StringUtils.isNotBlank(actorEmail)) { email.setReplyTo(actorEmail); } } } requests.add(new UserActionRequest("sendEmailNotificationAction", null, email)); } return requests; } EmailNotifier(final VelocityEngine inVelocityEngine, final Context inVelocityGlobalContext,
            final Map<NotificationType, EmailNotificationTemplate> inTemplates, final String inSubjectPrefix,
            final TokenContentFormatter inTokenContentFormatter,
            final TokenContentEmailAddressBuilder inTokenAddressBuilder,
            final ActivityInteractionAuthorizationStrategy inActivityAuthorizer, final boolean inSendHtml); @Override Collection<UserActionRequest> notify(final NotificationType inType, final Collection<Long> inRecipients,
            final Map<String, Object> inProperties, final Map<Long, PersonModelView> inRecipientIndex);  }