1
|
package eu.dnetlib.msro.notification;
|
2
|
|
3
|
import java.util.Arrays;
|
4
|
import java.util.Date;
|
5
|
import java.util.Map;
|
6
|
import java.util.Properties;
|
7
|
import java.util.Set;
|
8
|
import java.util.concurrent.BlockingQueue;
|
9
|
import java.util.concurrent.LinkedBlockingQueue;
|
10
|
|
11
|
import javax.mail.Authenticator;
|
12
|
import javax.mail.Message;
|
13
|
import javax.mail.MessagingException;
|
14
|
import javax.mail.PasswordAuthentication;
|
15
|
import javax.mail.Session;
|
16
|
import javax.mail.Transport;
|
17
|
import javax.mail.internet.InternetAddress;
|
18
|
import javax.mail.internet.MimeMessage;
|
19
|
|
20
|
import org.antlr.stringtemplate.StringTemplate;
|
21
|
import org.apache.commons.io.IOUtils;
|
22
|
import org.apache.commons.logging.Log;
|
23
|
import org.apache.commons.logging.LogFactory;
|
24
|
import org.springframework.beans.factory.annotation.Required;
|
25
|
|
26
|
import com.google.common.base.Splitter;
|
27
|
import com.google.common.collect.Maps;
|
28
|
|
29
|
public class EmailDispatcher {
|
30
|
|
31
|
private String from;
|
32
|
private String fromName;
|
33
|
private String cc;
|
34
|
private String smtpHost;
|
35
|
private int smtpPort = 587;
|
36
|
private String smtpUser;
|
37
|
private String smtpPassword;
|
38
|
private String baseUrl;
|
39
|
private String infrastructure;
|
40
|
|
41
|
private static final Log log = LogFactory.getLog(EmailDispatcher.class);
|
42
|
|
43
|
private final BlockingQueue<Message> queue = new LinkedBlockingQueue<Message>();
|
44
|
|
45
|
public StringTemplate prepareMessage(final String template, final Map<String, Object> tmplParams) {
|
46
|
final StringTemplate st = new StringTemplate(template);
|
47
|
st.setAttributes(tmplParams);
|
48
|
st.setAttribute("baseUrl", baseUrl);
|
49
|
st.setAttribute("infrastructure", infrastructure);
|
50
|
return st;
|
51
|
}
|
52
|
|
53
|
public void sendMail(final Set<String> to, final String subject, final String template, final Map<String, Object> tmplParams) {
|
54
|
try {
|
55
|
final StringTemplate st = prepareMessage(template, tmplParams);
|
56
|
|
57
|
final Session session = Session.getInstance(obtainProperties(), obtainAuthenticator());
|
58
|
|
59
|
final MimeMessage message = new MimeMessage(session);
|
60
|
message.setFrom(new InternetAddress(from, fromName));
|
61
|
message.setSubject(subject);
|
62
|
message.setContent(st.toString(), "text/html; charset=utf-8");
|
63
|
message.setSentDate(new Date());
|
64
|
|
65
|
for (String s : to) {
|
66
|
message.addRecipient(Message.RecipientType.TO, new InternetAddress(s));
|
67
|
}
|
68
|
if ((cc != null) && !cc.isEmpty()) {
|
69
|
for (String aCC : Splitter.on(",").omitEmptyStrings().trimResults().split(getCc())) {
|
70
|
message.addRecipient(Message.RecipientType.CC, new InternetAddress(aCC));
|
71
|
}
|
72
|
}
|
73
|
|
74
|
queue.add(message);
|
75
|
|
76
|
log.info("Mail to " + Arrays.toString(to.toArray()) + " in queue");
|
77
|
} catch (Exception e) {
|
78
|
log.error("Error sending mail", e);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
public void processMailQueue() {
|
83
|
while (true) {
|
84
|
final Message message = queue.poll();
|
85
|
if (message == null) return;
|
86
|
else {
|
87
|
try {
|
88
|
log.info("Sending mail...");
|
89
|
Transport.send(message);
|
90
|
log.info("...sent");
|
91
|
} catch (MessagingException e) {
|
92
|
log.error("Error sending email", e);
|
93
|
queue.add(message);
|
94
|
return;
|
95
|
}
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
|
100
|
private void sendWfStatusMail(final boolean success,
|
101
|
final Set<String> to,
|
102
|
final String wfId,
|
103
|
final String procId,
|
104
|
final String wfName,
|
105
|
final Map<String, String> pendingWfs,
|
106
|
final Map<String, String> responses,
|
107
|
final String error) {
|
108
|
try {
|
109
|
final Map<String, Object> map = Maps.newHashMap();
|
110
|
map.put("wfId", wfId);
|
111
|
map.put("wfName", wfName);
|
112
|
map.put("procId", procId);
|
113
|
if ((pendingWfs != null) && !pendingWfs.isEmpty()) {
|
114
|
map.put("pendingWfs", pendingWfs);
|
115
|
}
|
116
|
if ((responses != null) && !responses.isEmpty()) {
|
117
|
map.put("responses", responses);
|
118
|
}
|
119
|
if ((error != null) && !error.isEmpty()) {
|
120
|
map.put("error", error);
|
121
|
}
|
122
|
|
123
|
String subject = success ? "Workflow '" + wfName + "' has been completed successfully" : "Workflow '" + wfName + "' is failed";
|
124
|
subject += "on " + infrastructure;
|
125
|
final String tmplName = success ? "wf_success.mail.st" : "wf_failed.mail.st";
|
126
|
final String template = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/msro/mail/" + tmplName));
|
127
|
|
128
|
sendMail(to, subject, template, map);
|
129
|
} catch (Exception e) {
|
130
|
log.error("Error generating success-mail", e);
|
131
|
}
|
132
|
}
|
133
|
|
134
|
public void sendSuccessMail(final Set<String> to,
|
135
|
final String wfId,
|
136
|
final String procId,
|
137
|
final String wfName,
|
138
|
final Map<String, String> pendingWfs,
|
139
|
final Map<String, String> responses) {
|
140
|
sendWfStatusMail(true, to, wfId, procId, wfName, pendingWfs, responses, "");
|
141
|
}
|
142
|
|
143
|
public void sendFailedMail(final Set<String> to,
|
144
|
final String wfId,
|
145
|
final String procId,
|
146
|
final String wfName,
|
147
|
final Map<String, String> pendingWfs,
|
148
|
final Map<String, String> responses,
|
149
|
final String error) {
|
150
|
sendWfStatusMail(false, to, wfId, procId, wfName, pendingWfs, responses, error);
|
151
|
}
|
152
|
|
153
|
private Properties obtainProperties() {
|
154
|
final Properties props = new Properties();
|
155
|
props.put("mail.transport.protocol", "smtp");
|
156
|
props.put("mail.smtp.host", smtpHost);
|
157
|
props.put("mail.smtp.port", smtpPort);
|
158
|
props.put("mail.smtp.auth", Boolean.toString((smtpUser != null) && !smtpUser.isEmpty()));
|
159
|
return props;
|
160
|
}
|
161
|
|
162
|
private Authenticator obtainAuthenticator() {
|
163
|
if ((smtpUser == null) || smtpUser.isEmpty()) return null;
|
164
|
|
165
|
return new Authenticator() {
|
166
|
|
167
|
private final PasswordAuthentication authentication = new PasswordAuthentication(smtpUser, smtpPassword);
|
168
|
|
169
|
@Override
|
170
|
protected PasswordAuthentication getPasswordAuthentication() {
|
171
|
return authentication;
|
172
|
}
|
173
|
|
174
|
};
|
175
|
}
|
176
|
|
177
|
public String getFrom() {
|
178
|
return from;
|
179
|
}
|
180
|
|
181
|
@Required
|
182
|
public void setFrom(final String from) {
|
183
|
this.from = from;
|
184
|
}
|
185
|
|
186
|
public String getFromName() {
|
187
|
return fromName;
|
188
|
}
|
189
|
|
190
|
@Required
|
191
|
public void setFromName(final String fromName) {
|
192
|
this.fromName = fromName;
|
193
|
}
|
194
|
|
195
|
public String getCc() {
|
196
|
return cc;
|
197
|
}
|
198
|
|
199
|
@Required
|
200
|
public void setCc(final String cc) {
|
201
|
this.cc = cc;
|
202
|
}
|
203
|
|
204
|
public String getSmtpHost() {
|
205
|
return smtpHost;
|
206
|
}
|
207
|
|
208
|
@Required
|
209
|
public void setSmtpHost(final String smtpHost) {
|
210
|
this.smtpHost = smtpHost;
|
211
|
}
|
212
|
|
213
|
public int getSmtpPort() {
|
214
|
return smtpPort;
|
215
|
}
|
216
|
|
217
|
public void setSmtpPort(final int smtpPort) {
|
218
|
this.smtpPort = smtpPort;
|
219
|
}
|
220
|
|
221
|
public String getSmtpUser() {
|
222
|
return smtpUser;
|
223
|
}
|
224
|
|
225
|
public void setSmtpUser(final String smtpUser) {
|
226
|
this.smtpUser = smtpUser;
|
227
|
}
|
228
|
|
229
|
public String getSmtpPassword() {
|
230
|
return smtpPassword;
|
231
|
}
|
232
|
|
233
|
public void setSmtpPassword(final String smtpPassword) {
|
234
|
this.smtpPassword = smtpPassword;
|
235
|
}
|
236
|
|
237
|
public String getBaseUrl() {
|
238
|
return baseUrl;
|
239
|
}
|
240
|
|
241
|
@Required
|
242
|
public void setBaseUrl(final String baseUrl) {
|
243
|
this.baseUrl = baseUrl;
|
244
|
}
|
245
|
|
246
|
public String getInfrastructure() {
|
247
|
return infrastructure;
|
248
|
}
|
249
|
|
250
|
@Required
|
251
|
public void setInfrastructure(final String infrastructure) {
|
252
|
this.infrastructure = infrastructure;
|
253
|
}
|
254
|
|
255
|
}
|