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