Skip to content

Commit

Permalink
add support for html (MID-2072)
Browse files Browse the repository at this point in the history
and define from, cc, bcc

(cherry picked from commit f367a02)
  • Loading branch information
MICHAEL.GRUBER authored and mederly committed Feb 7, 2017
1 parent b8ab19b commit 441f4dc
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 5 deletions.
Expand Up @@ -645,6 +645,14 @@
<xsd:complexContent>
<xsd:extension base="tns:EventHandlerType">
<xsd:sequence>
<xsd:element name="fromExpression" type="c:ExpressionType" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
from address
overwrites notificationConfiguration/mail/defaultFrom
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="recipientExpression" type="c:ExpressionType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
Expand All @@ -654,6 +662,27 @@
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="ccExpression" type="c:ExpressionType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
cc Recipient(s) that should get the notifications.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="bccExpression" type="c:ExpressionType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
bcc Recipient(s) that should get the notifications.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="contentTypeExpression" type="c:ExpressionType" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
contentType used in email
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="subjectExpression" type="c:ExpressionType" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
Expand Down
Expand Up @@ -27,9 +27,12 @@
public class Message implements DebugDumpable {

private List<String> to;
private List<String> cc;
private List<String> bcc;
private String subject;
private String body; // todo
private String contentType;
private String from;

public String getBody() {
return body;
Expand All @@ -54,6 +57,22 @@ public List<String> getTo() {
public void setTo(List<String> to) {
this.to = to;
}

public List<String> getCc() {
return cc;
}

public void setCc(List<String> cc) {
this.cc = cc;
}

public List<String> getBcc() {
return bcc;
}

public void setBcc(List<String> bcc) {
this.bcc = bcc;
}

public String getContentType() {
return contentType;
Expand All @@ -62,11 +81,25 @@ public String getContentType() {
public void setContentType(String contentType) {
this.contentType = contentType;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

@Override
public String toString() {
String fromString = from!= null ? ", from='" + from + "' " : "";
String ccString = cc!= null ? ", cc='" + cc + "' " : "";
String bccString = bcc!= null ? ", bcc='" + bcc + "' " : "";
return "Message{" +
"to='" + to + '\'' +
"to='" + to + '\'' +
fromString +
ccString +
bccString +
", subject='" + subject + '\'' +
", contentType='" + contentType + '\'' +
", body='" + body + '\'' +
Expand All @@ -81,12 +114,27 @@ public String debugDump() {
@Override
public String debugDump(int indent) {
StringBuilder rv = new StringBuilder();
rv.append("\n");
DebugUtil.debugDumpLabel(rv, "Message", indent);
rv.append("\n");

if (from != null){
DebugUtil.debugDumpWithLabel(rv, "From", from, indent+1);
rv.append("\n");
}

DebugUtil.debugDumpWithLabel(rv, "To", to, indent+1);
rv.append("\n");

if (cc != null){
DebugUtil.debugDumpWithLabel(rv, "Cc", cc, indent+1);
rv.append("\n");
}
if (bcc != null){
DebugUtil.debugDumpWithLabel(rv, "Bcc", bcc, indent+1);
rv.append("\n");
}

DebugUtil.debugDumpWithLabel(rv, "Subject", subject, indent+1);
rv.append("\n");

Expand Down
Expand Up @@ -135,7 +135,7 @@ public void send(Message mailMessage, String transportName, Task task, Operation

long start = System.currentTimeMillis();

String from = mailConfigurationType.getDefaultFrom() != null ? mailConfigurationType.getDefaultFrom() : "nobody@nowhere.org";
String defaultFrom = mailConfigurationType.getDefaultFrom() != null ? mailConfigurationType.getDefaultFrom() : "nobody@nowhere.org";

for (MailServerConfigurationType mailServerConfigurationType : mailConfigurationType.getServer()) {

Expand Down Expand Up @@ -188,9 +188,23 @@ public void send(Message mailMessage, String transportName, Task task, Operation

try {
MimeMessage mimeMessage = new MimeMessage(session);
String from = mailMessage.getFrom() != null ? mailMessage.getFrom() : defaultFrom;
mimeMessage.setFrom(new InternetAddress(from));
for (String recipient : mailMessage.getTo()) {
mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient));

if (mailMessage.getTo() != null){
for (String recipient : mailMessage.getTo()) {
mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient));
}
}
if (mailMessage.getCc() != null){
for (String recipientCc : mailMessage.getCc()) {
mimeMessage.addRecipient(javax.mail.Message.RecipientType.CC, new InternetAddress(recipientCc));
}
}
if (mailMessage.getBcc() != null){
for (String recipientBcc : mailMessage.getBcc()) {
mimeMessage.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(recipientBcc));
}
}
mimeMessage.setSubject(mailMessage.getSubject(), "utf-8");
String contentType = mailMessage.getContentType();
Expand Down Expand Up @@ -238,7 +252,7 @@ public void send(Message mailMessage, String transportName, Task task, Operation


private String formatToFile(Message mailMessage) {
return "============================================ " + new Date() + "\n" + mailMessage.toString() + "\n\n";
return "============================================ " + "\n" +new Date() + "\n" + mailMessage.toString() + "\n\n";
}

@Override
Expand Down
Expand Up @@ -124,6 +124,8 @@ public boolean processEvent(Event event, EventHandlerType eventHandlerType, Noti

String body = getBodyFromExpression(event, generalNotifierType, variables, task, result);
String subject = getSubjectFromExpression(event, generalNotifierType, variables, task, result);
String from = getFromFromExpression(event, generalNotifierType, variables, task, result);
String contentType = getContentTypeFromExpression(event, generalNotifierType, variables, task, result);

if (body == null) {
body = getBody(event, generalNotifierType, transportName, task, result);
Expand All @@ -135,9 +137,16 @@ public boolean processEvent(Event event, EventHandlerType eventHandlerType, Noti

Message message = new Message();
message.setBody(body != null ? body : "");
message.setContentType(contentType);
message.setContentType(generalNotifierType.getContentType());
message.setSubject(subject);

if (from != null)message.setFrom(from);
message.setTo(recipientsAddresses); // todo cc/bcc recipients
List<String> cc = getCcAddresses(event, generalNotifierType, variables, task, result);
if (cc != null)message.setCc(cc);
List<String> bcc = getBccAddresses(event, generalNotifierType, variables, task, result);
if (bcc != null)message.setBcc(bcc);;

getLogger().trace("Sending notification via transport {}:\n{}", transportName, message);
transport.send(message, transportName, task, result);
Expand Down Expand Up @@ -214,6 +223,38 @@ protected List<String> getRecipientsAddresses(Event event, GeneralNotifierType g
return addresses;
}

protected List<String> getCcAddresses(Event event, GeneralNotifierType generalNotifierType, ExpressionVariables variables,
Task task, OperationResult result) {
List<String> addresses = null;
if (!generalNotifierType.getCcExpression().isEmpty()) {
addresses = new ArrayList<>();
for (ExpressionType expressionType : generalNotifierType.getCcExpression()) {
List<String> r = evaluateExpressionChecked(expressionType, variables, "notification cc-recipient", task, result);
if (r != null) {
addresses.addAll(r);
}
}

}
return addresses;
}

protected List<String> getBccAddresses(Event event, GeneralNotifierType generalNotifierType, ExpressionVariables variables,
Task task, OperationResult result) {
List<String> addresses = null;
if (!generalNotifierType.getBccExpression().isEmpty()) {
addresses = new ArrayList<>();
for (ExpressionType expressionType : generalNotifierType.getBccExpression()) {
List<String> r = evaluateExpressionChecked(expressionType, variables, "notification cc-recipient", task, result);
if (r != null) {
addresses.addAll(r);
}
}

}
return addresses;
}

protected String getSubjectFromExpression(Event event, GeneralNotifierType generalNotifierType, ExpressionVariables variables,
Task task, OperationResult result) {
if (generalNotifierType.getSubjectExpression() != null) {
Expand All @@ -232,6 +273,40 @@ protected String getSubjectFromExpression(Event event, GeneralNotifierType gener
}
}

protected String getFromFromExpression(Event event, GeneralNotifierType generalNotifierType, ExpressionVariables variables,
Task task, OperationResult result) {
if (generalNotifierType.getFromExpression() != null) {
List<String> fromList = evaluateExpressionChecked(generalNotifierType.getFromExpression(), variables, "from expression",
task, result);
if (fromList == null || fromList.isEmpty()) {
getLogger().info("from expression for event " + event.getId() + " returned nothing.");
}
if (fromList.size() > 1) {
getLogger().warn("from expression for event " + event.getId() + " returned more than 1 item.");
}
return fromList.get(0);
} else {
return null;
}
}

protected String getContentTypeFromExpression(Event event, GeneralNotifierType generalNotifierType, ExpressionVariables variables,
Task task, OperationResult result) {
if (generalNotifierType.getContentTypeExpression() != null) {
List<String> contentTypeList = evaluateExpressionChecked(generalNotifierType.getContentTypeExpression(), variables, "contentType expression",
task, result);
if (contentTypeList == null || contentTypeList.isEmpty()) {
getLogger().info("contentType expression for event " + event.getId() + " returned nothing.");
}
if (contentTypeList.size() > 1) {
getLogger().warn("contentType expression for event " + event.getId() + " returned more than 1 item.");
}
return contentTypeList.get(0);
} else {
return null;
}
}

protected String getBodyFromExpression(Event event, GeneralNotifierType generalNotifierType, ExpressionVariables variables,
Task task, OperationResult result) {
if (generalNotifierType.getBodyExpression() != null) {
Expand Down

0 comments on commit 441f4dc

Please sign in to comment.