1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service.impl;
17
18 import javax.mail.MessagingException;
19
20 import org.kuali.rice.core.api.CoreConstants;
21 import org.kuali.rice.core.api.mail.MailMessage;
22 import org.kuali.rice.core.api.mail.Mailer;
23 import org.kuali.rice.krad.exception.InvalidAddressException;
24 import org.kuali.rice.krad.service.KRADServiceLocator;
25 import org.kuali.rice.krad.service.MailService;
26 import org.kuali.rice.krad.util.KRADConstants;
27
28 public class MailServiceImpl implements MailService {
29 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MailServiceImpl.class);
30
31 private String batchMailingList;
32 private Mailer mailer;
33
34 private String nonProductionNotificationMailingList;
35 private boolean realNotificationsEnabled = true;
36
37
38
39
40 public void setMailer(Mailer mailer) {
41 this.mailer = mailer;
42 }
43
44
45
46
47 public MailServiceImpl() {
48 super();
49 }
50
51
52
53
54
55 public void setBatchMailingList(String batchMailingList) {
56 this.batchMailingList = batchMailingList;
57 }
58
59
60
61
62 public String getBatchMailingList() {
63 return batchMailingList;
64 }
65
66
67
68
69
70
71
72 @Override
73 public void sendMessage(MailMessage message) throws InvalidAddressException, MessagingException {
74 mailer.sendEmail(composeMessage(message));
75 }
76
77 protected MailMessage composeMessage(MailMessage message){
78
79 MailMessage mm = new MailMessage();
80
81
82 if(!isRealNotificationsEnabled()){
83 getNonProductionMessage(message);
84 }
85
86 String app = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(CoreConstants.Config.APPLICATION_ID);
87 String env = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.ENVIRONMENT_KEY);
88
89 mm.setToAddresses(message.getToAddresses());
90 mm.setBccAddresses(message.getBccAddresses());
91 mm.setCcAddresses(message.getCcAddresses());
92 mm.setSubject(app + " " + env + ": " + message.getSubject());
93 mm.setMessage(message.getMessage());
94 mm.setFromAddress(message.getFromAddress());
95 return mm;
96 }
97
98 public String getNonProductionNotificationMailingList() {
99 return this.nonProductionNotificationMailingList;
100 }
101
102
103
104
105 public void setNonProductionNotificationMailingList(
106 String nonProductionNotificationMailingList) {
107 this.nonProductionNotificationMailingList = nonProductionNotificationMailingList;
108 }
109
110
111
112
113 public boolean isRealNotificationsEnabled() {
114 return this.realNotificationsEnabled;
115 }
116
117
118
119
120 public void setRealNotificationsEnabled(boolean realNotificationsEnabled) {
121 this.realNotificationsEnabled = realNotificationsEnabled;
122 }
123
124 protected MailMessage getNonProductionMessage(MailMessage message){
125 StringBuilder buf = new StringBuilder();
126 buf.append("Email To: ").append(message.getToAddresses()).append("\n");
127 buf.append("Email CC: ").append(message.getCcAddresses()).append("\n");
128 buf.append("Email BCC: ").append(message.getBccAddresses()).append("\n\n");
129 buf.append(message.getMessage());
130
131 message.getToAddresses().clear();
132
133 message.addToAddress(getNonProductionNotificationMailingList());
134 message.getBccAddresses().clear();
135 message.getCcAddresses().clear();
136 message.setMessage(buf.toString());
137
138 return message;
139 }
140 }