forked from bimberlabinternal/LabDevKitModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotificationServiceImpl.java
More file actions
692 lines (614 loc) · 24.3 KB
/
NotificationServiceImpl.java
File metadata and controls
692 lines (614 loc) · 24.3 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
* Copyright (c) 2012 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.labkey.ldk.notification;
import jakarta.mail.Address;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import org.labkey.api.audit.AuditLogService;
import org.labkey.api.audit.provider.SiteSettingsAuditProvider.SiteSettingsAuditEvent;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.PropertyManager;
import org.labkey.api.data.PropertyManager.WritablePropertyMap;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.Table;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
import org.labkey.api.ldk.notification.Notification;
import org.labkey.api.ldk.notification.NotificationService;
import org.labkey.api.query.FieldKey;
import org.labkey.api.security.Group;
import org.labkey.api.security.MemberType;
import org.labkey.api.security.SecurityManager;
import org.labkey.api.security.User;
import org.labkey.api.security.UserManager;
import org.labkey.api.security.UserPrincipal;
import org.labkey.api.security.ValidEmail;
import org.labkey.api.security.permissions.AdminPermission;
import org.labkey.api.util.ConfigurationException;
import org.labkey.api.util.ExceptionUtil;
import org.labkey.api.util.MailHelper;
import org.labkey.api.util.PageFlowUtil;
import org.labkey.ldk.LDKSchema;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* User: bbimber
* Date: 7/14/12
* Time: 2:49 PM
*/
public class NotificationServiceImpl extends NotificationService
{
private final static Logger _log = LogManager.getLogger(NotificationServiceImpl.class);
public final static String TIMESTAMP_PROPERTY_DOMAIN = "org.labkey.ldk.notifications.timestamp";
public final static String STATUS_PROPERTY_DOMAIN = "org.labkey.ldk.notifications.status";
public final static String CONFIG_PROPERTY_DOMAIN = "org.labkey.ldk.notifications.config";
private static final String ENABLED_PROP = "serviceEnabled";
private static final String RETURN_EMAIL = "returnEmail";
private static final String USER_PROP = "user";
private final List<Notification> _notifications = new ArrayList<>();
private final Map<Notification, Trigger> _triggerMap = new HashMap<>();
public static NotificationServiceImpl get()
{
return (NotificationServiceImpl) NotificationService.get();
}
public NotificationServiceImpl()
{
}
private Set<Container> getEnabledContainers()
{
Set<Container> containers = new HashSet<>();
PropertyManager.PropertyEntry[] entries = PropertyManager.findPropertyEntries(null, null, STATUS_PROPERTY_DOMAIN, null);
for (PropertyManager.PropertyEntry e : entries)
{
if (e.getObjectId() != null && Boolean.parseBoolean(e.getValue()))
{
Container c = ContainerManager.getForId(e.getObjectId());
if (c != null)
containers.add(c);
}
}
return containers;
}
@Override
public void registerNotification(Notification notification)
{
_log.info("Registering notification: " + notification.getName());
_notifications.add(notification);
try
{
schedule(notification);
}
catch (SchedulerException e)
{
throw new RuntimeException(e);
}
}
public void schedule(Notification n) throws SchedulerException
{
if (n.getCronString() == null)
return; //allows for alerts that are only used on-demand
JobDetail job = JobBuilder.newJob(NotificationJob.class)
.withIdentity(n.getClass().getCanonicalName(), getClass().getCanonicalName())
.usingJobData("notificationName", NotificationServiceImpl.get().getKey(n))
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(n.getClass().getCanonicalName(), getClass().getCanonicalName())
.withSchedule(CronScheduleBuilder.cronSchedule(n.getCronString()))
.forJob(job)
.build();
StdSchedulerFactory.getDefaultScheduler().scheduleJob(job, trigger);
_triggerMap.put(n, trigger);
}
private static String getConfigProperty(Container c, String key)
{
return PropertyManager.getProperties(c, CONFIG_PROPERTY_DOMAIN).get(key);
}
@Override
public boolean isServiceEnabled()
{
String prop = getConfigProperty(ContainerManager.getRoot(), ENABLED_PROP);
if (prop == null)
return false;
return Boolean.parseBoolean(prop);
}
public void setServiceEnabled(User u, Boolean status)
{
boolean oldStatus = isServiceEnabled();
WritablePropertyMap pm = PropertyManager.getWritableProperties(ContainerManager.getRoot(), NotificationServiceImpl.CONFIG_PROPERTY_DOMAIN, true);
pm.put(ENABLED_PROP, status.toString());
pm.save();
if (status != oldStatus)
{
SiteSettingsAuditEvent event = new SiteSettingsAuditEvent(ContainerManager.getRoot(),
"Notification service has been " + (status ? "enabled" : "disabled") + ".");
AuditLogService.get().addEvent(u, event);
}
}
@Override
public Set<Notification> getNotifications(Container c, boolean includeAll)
{
Set<Notification> notifications = new HashSet<>();
if (c.equals(ContainerManager.getRoot()))
{
notifications.addAll(_notifications);
}
else
{
for (Notification n : _notifications)
{
if (includeAll)
{
notifications.add(n);
}
else if (n.isAvailable(c))
{
notifications.add(n);
}
}
}
return Collections.unmodifiableSet(notifications);
}
@Override
public Notification getNotification(String key)
{
for (Notification n : _notifications)
{
if (getKey(n).equals(key))
return n;
}
return null;
}
@Override
public Address getReturnEmail(Container c)
{
String email = getConfigProperty(c, RETURN_EMAIL);
return email == null ? null : getAddress(email);
}
public void setReturnEmail(Container c, User u, String returnEmail)
{
try
{
if(returnEmail != null)
{
ValidEmail email = new ValidEmail(returnEmail);
String previous = getConfigProperty(c, RETURN_EMAIL);
WritablePropertyMap pm = PropertyManager.getWritableProperties(c, NotificationServiceImpl.CONFIG_PROPERTY_DOMAIN, true);
pm.put(RETURN_EMAIL, email.getEmailAddress());
pm.save();
if(!returnEmail.equals(previous))
{
SiteSettingsAuditEvent event = new SiteSettingsAuditEvent(c, "Notification reply-to email updated.");
String before = previous == null ? "" : previous;
String after = email.getEmailAddress();
String html = "<table><tr><td class='labkey-form-label'>Reply-to email</td><td>" + PageFlowUtil.filter(before) + " » " + PageFlowUtil.filter(after) + "</td></tr></table>";
event.setChanges(html);
AuditLogService.get().addEvent(u, event);
}
}
}
catch (ValidEmail.InvalidEmailException e)
{
throw new ConfigurationException(e.getMessage());
}
}
public User getUser(Container c)
{
String user = getConfigProperty(c, USER_PROP);
if (StringUtils.isEmpty(user))
return null;
return UserManager.getUser(Integer.parseInt(user));
}
public void setUser(Container c, User u, Integer userId)
{
if(userId != null)
{
User previousUser = getUser(c);
WritablePropertyMap pm = PropertyManager.getWritableProperties(c, NotificationServiceImpl.CONFIG_PROPERTY_DOMAIN, true);
pm.put(USER_PROP, String.valueOf(userId));
pm.save();
if (previousUser == null || (previousUser.getUserId() != userId))
{
User newUser = UserManager.getUser(userId);
String before = previousUser == null ? "" : previousUser.getEmail();
String after = newUser == null ? "" : newUser.getEmail();
SiteSettingsAuditEvent event = new SiteSettingsAuditEvent(c, "Notification service user updated.");
String html = "<table><tr><td class='labkey-form-label'>Service user</td><td>" + PageFlowUtil.filter(before) + " » " + PageFlowUtil.filter(after) + "</td></tr></table>";
event.setChanges(html);
AuditLogService.get().addEvent(u, event);
}
}
}
private Address getAddress(String email)
{
try
{
ValidEmail ve = new ValidEmail(email);
return ve.getAddress();
}
catch (ValidEmail.InvalidEmailException e)
{
throw new ConfigurationException(e.getMessage());
}
}
public JSONObject getJson(Notification n, Container c, User u)
{
JSONObject json = new JSONObject();
json.put("name", n.getName());
json.put("description", n.getDescription());
json.put("active", isActive(n, c));
json.put("available", n.isAvailable(c));
json.put("schedule", n.getScheduleDescription());
json.put("cronString", n.getCronString());
long lastRun = getLastRun(n);
json.put("lastRun", lastRun);
json.put("lastRunDate", lastRun == 0 ? null : new Date(lastRun));
String duration = lastRun == 0 ? "" : DurationFormatUtils.formatDurationWords(System.currentTimeMillis() - lastRun, true, true);
json.put("durationString", duration);
json.put("key", getKey(n));
json.put("category", n.getCategory());
json.put("nextFireTime", getNextFireTime(n));
//find subscriptions for the current user
List<UserPrincipal> ups = new ArrayList<>();
for (UserPrincipal up : NotificationServiceImpl.get().getRecipients(n, c))
{
if (up instanceof Group g)
{
Set<UserPrincipal> members = SecurityManager.getAllGroupMembers(g, MemberType.ALL_GROUPS_AND_USERS);
if (members.contains(u))
ups.add(up);
}
else
{
if (up.getUserId() == u.getUserId())
ups.add(up);
}
}
List<Map<String, Object>> subscriptions = new ArrayList<>();
for (UserPrincipal up : ups)
{
Map<String, Object> map = new HashMap<>();
map.put("userId", up.getUserId());
map.put("type", up.getType());
map.put("name", up.getName());
subscriptions.add(map);
}
json.put("subscriptions", subscriptions);
return json;
}
public String getKey(Notification n)
{
return n.getClass().getCanonicalName();
}
@Override
public long getLastRun(Notification n)
{
Map<String, String> m = PropertyManager.getProperties(NotificationServiceImpl.TIMESTAMP_PROPERTY_DOMAIN);
String value = m.get(getKey(n));
if (value != null)
{
return Long.parseLong(value);
}
return 0;
}
public Date getLastRunDate(Notification n)
{
long lastRun = getLastRun(n);
return lastRun == 0 ? null : new Date(lastRun);
}
public void setLastRun(Notification n, Long lastRun)
{
WritablePropertyMap pm = PropertyManager.getWritableProperties(NotificationServiceImpl.TIMESTAMP_PROPERTY_DOMAIN, true);
pm.put(getKey(n), lastRun.toString());
pm.save();
}
@Override
public boolean isActive(Notification n, Container c)
{
Map<String, String> m = PropertyManager.getProperties(c, NotificationServiceImpl.STATUS_PROPERTY_DOMAIN);
String value = m.get(getKey(n));
if (value != null)
{
return Boolean.parseBoolean(value);
}
return false;
}
public void setActive(Notification n, Container c, User u, boolean active)
{
boolean old = isActive(n, c);
WritablePropertyMap pm = PropertyManager.getWritableProperties(c, NotificationServiceImpl.STATUS_PROPERTY_DOMAIN, true);
pm.put(getKey(n), active ? String.valueOf(active) : null);
pm.save();
if (old != active)
{
String action = active ? "enabled" : "disabled";
String comment = "Notification '" + n.getName() + "' has been " + action + ".";
SiteSettingsAuditEvent event = new SiteSettingsAuditEvent(c, comment);
AuditLogService.get().addEvent(u, event);
}
}
@Override
@NotNull
public List<Address> getEmailsForPrincipal(UserPrincipal user) throws ValidEmail.InvalidEmailException
{
if (user instanceof User)
{
if (!user.isActive())
{
//_log.error("an inactive user is a notification recipient: " + user.getName());
return Collections.emptyList();
}
ValidEmail validEmail = new ValidEmail(((User)user).getEmail());
return Collections.singletonList(validEmail.getAddress());
}
else
{
Group group = ((Group)user);
if (group != null)
{
if (group.isSystemGroup())
throw new IllegalArgumentException("Invalid group ID: site groups are not allowed");
//NOTE: this could include inactive users as members, so we filter them out
Set<User> members = SecurityManager.getAllGroupMembers(group, MemberType.ACTIVE_USERS);
List<Address> addresses = new ArrayList<>();
for (User u : members)
{
//note: unlike user, dont log error since inactive users retain group membership
if (!u.isActive())
{
continue;
}
ValidEmail validEmail = new ValidEmail(u.getEmail());
addresses.add(validEmail.getAddress());
}
return addresses;
}
else
throw new IllegalArgumentException("Unable to resolve principalId");
}
}
public String getMessage(Notification n, Container c) throws IOException, MessagingException
{
if (!n.isAvailable(c))
return null;
User u = getUser(c);
if (u == null)
return null;
MimeMessage message = n.createMessage(c, u);
if (message != null)
{
Map<String, String> map = MailHelper.getBodyParts(message);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : map.entrySet())
{
if ("text/html".equals(entry.getKey()))
{
sb.append(entry.getValue());
}
else if (entry.getKey().startsWith("text/"))
{
sb.append(PageFlowUtil.filter(entry.getValue()));
}
else
{
sb.append("Attached file. MIME ContentType: " + entry.getKey() + " (not rendered here)");
}
sb.append("<br/><br/><br/>");
}
return sb.toString();
}
return null;
}
@Override
public User getUser(Notification n, Container c)
{
return getUser(c);
}
@Override
public Set<UserPrincipal> getRecipients(Notification n, Container c)
{
final Set<UserPrincipal> recipients = new HashSet<>();
TableInfo t = LDKSchema.getTable(LDKSchema.TABLE_NOTIFICATION_RECIPIENTS);
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("notificationtype"), n.getName(), CompareType.EQUAL);
filter.addCondition(FieldKey.fromString("container"), c.getId(), CompareType.EQUAL);
TableSelector ts = new TableSelector(t, Collections.singleton("recipient"), filter, null);
if (ts.getRowCount() > 0)
{
ts.forEach(new TableSelector.ForEachBlock<>()
{
@Override
public void exec(ResultSet rs) throws SQLException
{
int userId = rs.getInt("recipient");
UserPrincipal u = SecurityManager.getPrincipal(userId);
if (u != null)
recipients.add(u);
else
_log.error("unknown user registered with " + n.getName() + "notification: " + userId);
}
});
}
return recipients;
}
public List<Address> getEmails(Notification n, Container c)
{
List<Address> addresses = new ArrayList<>();
for (UserPrincipal u : getRecipients(n, c))
{
try
{
addresses.addAll(getEmailsForPrincipal(u));
}
catch (ValidEmail.InvalidEmailException e)
{
_log.error(e);
}
}
return addresses;
}
public Set<Container> getActiveContainers(Notification n)
{
Set<Container> containers = new HashSet<>();
for (Container c : getEnabledContainers())
{
if (isActive(n, c))
{
containers.add(c);
}
}
return containers;
}
@Override
public Date getNextFireTime(Notification n)
{
Trigger trigger = _triggerMap.get(n);
if (trigger == null)
return null;
return trigger.getFireTimeAfter(new Date());
}
public void updateSubscriptions(Container c, User u, Notification n, @Nullable List<UserPrincipal> toAdd, @Nullable List<UserPrincipal> toRemove)
{
TableInfo ti = LDKSchema.getTable(LDKSchema.TABLE_NOTIFICATION_RECIPIENTS);
List<UserPrincipal> actuallyAdded = new ArrayList<>();
List<UserPrincipal> actuallyRemoved = new ArrayList<>();
if (toAdd != null)
{
for (UserPrincipal up : toAdd)
{
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("container"), c.getId(), CompareType.EQUAL);
filter.addCondition(FieldKey.fromString("recipient"), up.getUserId());
filter.addCondition(FieldKey.fromString("notificationtype"), n.getName());
TableSelector ts = new TableSelector(ti, filter, null);
if (ts.getRowCount() == 0)
{
Map<String, Object> row = new HashMap<>();
row.put("container", c.getId());
row.put("recipient", up.getUserId());
row.put("notificationtype", n.getName());
row.put("createdby", u.getUserId());
row.put("created", new Date());
Table.insert(u, ti, row);
actuallyAdded.add(up);
}
}
}
if (toRemove != null)
{
for (UserPrincipal up : toRemove)
{
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("container"), c.getId(), CompareType.EQUAL);
filter.addCondition(FieldKey.fromString("recipient"), up.getUserId());
filter.addCondition(FieldKey.fromString("notificationtype"), n.getName());
Table.delete(ti, filter);
actuallyRemoved.add(up);
}
}
// Audit event
if ((!actuallyAdded.isEmpty() || !actuallyRemoved.isEmpty()) && u != null)
{
StringBuilder html = new StringBuilder();
if (!actuallyAdded.isEmpty())
{
html.append("<div class='labkey-form-label'>Added recipients</div><ul>");
for (UserPrincipal up : actuallyAdded)
{
html.append("<li>").append(PageFlowUtil.filter(up.getName())).append(" (ID: ").append(up.getUserId()).append(")</li>");
}
html.append("</ul>");
}
if (!actuallyRemoved.isEmpty())
{
html.append("<div class='labkey-form-label'>Removed recipients</div><ul>");
for (UserPrincipal up : actuallyRemoved)
{
html.append("<li>").append(PageFlowUtil.filter(up.getName())).append(" (ID: ").append(up.getUserId()).append(")</li>");
}
html.append("</ul>");
}
SiteSettingsAuditEvent event = new SiteSettingsAuditEvent(c, "Updated notification subscriptions for '" + n.getName() + "'.");
event.setChanges(html.toString());
AuditLogService.get().addEvent(u, event);
}
}
public void runForContainer(Notification notification, Container c)
{
User u = NotificationService.get().getUser(notification, c);
if (u == null)
{
_log.error("Invalid user when trying to run notification " + notification.getName() + " for container: " + c.getPath());
return;
}
if (!c.hasPermission(u, AdminPermission.class))
{
_log.error("Error running " + notification.getName() + ". User " + u.getEmail() + " does not have admin permissions on container: " + c.getPath());
return;
}
if (!NotificationServiceImpl.get().isActive(notification, c))
{
_log.error("Error running " + notification.getName() + " in container : " + c.getPath() + ". Notification is inactive, but a task was still scheduled");
return;
}
List<Address> recipients = NotificationServiceImpl.get().getEmails(notification, c);
if (recipients.isEmpty())
{
_log.info("Notification: " + notification.getName() + " has no recipients, skipping");
return;
}
try
{
MimeMessage mail = notification.createMessage(c, u);
if (mail == null)
{
_log.info("Notification " + notification.getName() + " did not produce a message, will not send email");
return;
}
_log.info("Sending message for notification: " + notification.getName() + " to " + recipients.size() + " recipients");
mail.setFrom(NotificationServiceImpl.get().getReturnEmail(c));
mail.setSubject(notification.getEmailSubject(c));
mail.addRecipients(Message.RecipientType.TO, recipients.toArray(new Address[0]));
MailHelper.send(mail, u, c);
}
catch (Exception e)
{
ExceptionUtil.logExceptionToMothership(null, e);
}
}
}