-
Notifications
You must be signed in to change notification settings - Fork 221
/
NotificationTypeEmail.java
190 lines (177 loc) · 7.99 KB
/
NotificationTypeEmail.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.notification;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.jumpmind.extension.IBuiltInExtensionPoint;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.ext.ISymmetricEngineAware;
import org.jumpmind.symmetric.model.IncomingBatch;
import org.jumpmind.symmetric.model.MonitorEvent;
import org.jumpmind.symmetric.model.Notification;
import org.jumpmind.symmetric.model.Notification.EmailExpression;
import org.jumpmind.symmetric.model.OutgoingBatch;
import org.jumpmind.symmetric.monitor.BatchErrorWrapper;
import org.jumpmind.symmetric.util.SymmetricUtils;
import org.jumpmind.util.FormatUtils;
import org.jumpmind.util.LogSummary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class NotificationTypeEmail implements INotificationType, ISymmetricEngineAware, IBuiltInExtensionPoint {
private final Logger log = LoggerFactory.getLogger(getClass());
protected ISymmetricEngine engine;
public void notify(Notification notification, List<MonitorEvent> monitorEvents) {
Map<String, String> eventListReplacements = SymmetricUtils.getReplacementsForMonitorEventList(engine,
monitorEvents);
EmailExpression expression = notification.getEmailExpression();
String subject = FormatUtils.replaceTokens(expression.getSubject(), eventListReplacements, true);
Map<String, String> templateMap = expression.getTemplateMap();
StringBuilder text = new StringBuilder();
if (!StringUtils.isBlank(expression.getBodyBefore())) {
text.append(FormatUtils.replaceTokens(expression.getBodyBefore(), eventListReplacements, true) + "\n");
}
for (MonitorEvent event : monitorEvents) {
try {
Map<String, String> eventReplacements = SymmetricUtils.getReplacementsForMonitorEvent(engine, event);
if (event.getType().equals("log")) {
eventReplacements.put("eventDetails", getLogDetails(event));
} else if (event.getType().equals("batchError")) {
eventReplacements.put("eventDetails", getBatchDetails(event));
} else if (event.getType().equals("offlineNodes")) {
eventReplacements.put("eventDetails", getOfflineDetails(event));
}
if (monitorEvents.indexOf(event) > 0) {
text.append("\n");
}
if (event.isResolved()) {
text.append(FormatUtils.replaceTokens(expression.getResolved(), eventReplacements, true));
continue;
} else {
text.append(FormatUtils.replaceTokens(expression.getUnresolved(), eventReplacements, true));
}
String template = templateMap.get(event.getType());
if (template == null) {
template = templateMap.get("default");
}
if (template != null) {
text.append("\n" + FormatUtils.replaceTokens(template, eventReplacements, true));
}
} catch (Exception e) {
log.debug("", e);
}
}
if (!StringUtils.isBlank(expression.getBodyAfter())) {
text.append("\n" + FormatUtils.replaceTokens(expression.getBodyAfter(), eventListReplacements, true));
}
String recipients = String.join(",", expression.getEmails());
if (recipients != null) {
log.info("Sending email with subject '" + subject + "' to " + recipients);
engine.getMailService().sendEmail(subject, text.toString(), recipients);
} else {
log.warn("Notification " + notification.getNotificationId() + " has no email recipients configured.");
}
}
protected static String getOfflineDetails(MonitorEvent event) throws IOException {
StringBuilder stackTrace = new StringBuilder();
stackTrace.append("\n");
for (String node : deserializeOfflineNodes(event)) {
stackTrace.append("Node ").append(node).append(" is offline.").append("\n");
}
return stackTrace.toString();
}
protected static String getBatchDetails(MonitorEvent event) throws IOException {
StringBuilder stackTrace = new StringBuilder();
BatchErrorWrapper errors = deserializeBatches(event);
if (errors != null) {
List<OutgoingBatch> outErrors = errors.getOutgoingErrors();
for (OutgoingBatch b : outErrors) {
stackTrace.append("The outgoing batch ").append(b.getNodeBatchId());
stackTrace.append(" failed: ").append(b.getSqlMessage()).append("\n");
}
List<IncomingBatch> inErrors = errors.getIncomingErrors();
for (IncomingBatch b : inErrors) {
stackTrace.append("The incoming batch ").append(b.getNodeBatchId());
stackTrace.append(" failed: ").append(b.getSqlMessage()).append("\n");
}
}
return stackTrace.toString();
}
protected static String getLogDetails(MonitorEvent event) throws IOException {
StringBuilder stackTrace = new StringBuilder();
int count = 0;
for (LogSummary summary : deserializeLogSummary(event)) {
if (summary.getMessage() != null) {
stackTrace.append(summary.getMessage());
count++;
}
if (summary.getStackTrace() != null) {
stackTrace.append(summary.getStackTrace());
count++;
}
}
if (count > 0) {
stackTrace.append("\n");
}
return stackTrace.toString();
}
protected static List<String> deserializeOfflineNodes(MonitorEvent event) throws IOException {
List<String> nodes = null;
if (event.getDetails() != null) {
nodes = new Gson().fromJson(event.getDetails(), new TypeToken<List<String>>() {
}.getType());
}
if (nodes == null) {
nodes = Collections.emptyList();
}
return nodes;
}
protected static BatchErrorWrapper deserializeBatches(MonitorEvent event) {
BatchErrorWrapper batches = null;
if (event.getDetails() != null) {
batches = new Gson().fromJson(event.getDetails(), BatchErrorWrapper.class);
}
return batches;
}
protected static List<LogSummary> deserializeLogSummary(MonitorEvent event) throws IOException {
List<LogSummary> summaries = null;
if (event.getDetails() != null) {
summaries = new Gson().fromJson(event.getDetails(), new TypeToken<List<LogSummary>>() {
}.getType());
}
if (summaries == null) {
summaries = Collections.emptyList();
}
return summaries;
}
@Override
public String getName() {
return "email";
}
@Override
public void setSymmetricEngine(ISymmetricEngine engine) {
this.engine = engine;
}
}