This is an automated email from the ASF dual-hosted git repository.
ilgrosso pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/syncope.git
The following commit(s) were added to refs/heads/master by this push:
new d5e3caab95 Notification code improvements
d5e3caab95 is described below
commit d5e3caab952ce2a70c4b35e3ef1f2c865ece8f6d
Author: Francesco Chicchiriccò <[email protected]>
AuthorDate: Tue Oct 15 14:27:41 2024 +0200
Notification code improvements
---
.../implementations/MyRecipientsProvider.groovy | 4 +-
.../api/notification/RecipientsProvider.java | 4 +-
.../java/job/AbstractSchedTaskJobDelegate.java | 6 +-
.../AbstractNotificationJobDelegate.java | 21 ++---
.../notification/DefaultNotificationManager.java | 97 ++++++++++------------
.../TestNotificationRecipientsProvider.java | 8 +-
.../syncope/fit/core/NotificationTaskITCase.java | 2 +-
7 files changed, 72 insertions(+), 70 deletions(-)
diff --git
a/client/idrepo/console/src/main/resources/org/apache/syncope/client/console/implementations/MyRecipientsProvider.groovy
b/client/idrepo/console/src/main/resources/org/apache/syncope/client/console/implementations/MyRecipientsProvider.groovy
index 1fe53ff713..39dfc7ea44 100644
---
a/client/idrepo/console/src/main/resources/org/apache/syncope/client/console/implementations/MyRecipientsProvider.groovy
+++
b/client/idrepo/console/src/main/resources/org/apache/syncope/client/console/implementations/MyRecipientsProvider.groovy
@@ -17,7 +17,9 @@
* under the License.
*/
import groovy.transform.CompileStatic
+import java.util.Map
import java.util.Set
+import org.apache.syncope.core.persistence.api.entity.Any
import org.apache.syncope.core.persistence.api.entity.Notification
import org.apache.syncope.core.provisioning.api.notification.RecipientsProvider
@@ -25,7 +27,7 @@ import
org.apache.syncope.core.provisioning.api.notification.RecipientsProvider
class MyRecipientsProvider implements RecipientsProvider {
@Override
- Set<String> provideRecipients(Notification notification) {
+ Set<String> provideRecipients(Notification notification, Any<?> any,
Map<String, Object> jexlVars) {
return Set.of();
}
}
diff --git
a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/notification/RecipientsProvider.java
b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/notification/RecipientsProvider.java
index 6b3cef3cb2..f18feb3919 100644
---
a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/notification/RecipientsProvider.java
+++
b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/notification/RecipientsProvider.java
@@ -18,11 +18,13 @@
*/
package org.apache.syncope.core.provisioning.api.notification;
+import java.util.Map;
import java.util.Set;
+import org.apache.syncope.core.persistence.api.entity.Any;
import org.apache.syncope.core.persistence.api.entity.Notification;
@FunctionalInterface
public interface RecipientsProvider {
- Set<String> provideRecipients(Notification notification);
+ Set<String> provideRecipients(Notification notification, Any<?> any,
Map<String, Object> jexlVars);
}
diff --git
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/AbstractSchedTaskJobDelegate.java
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/AbstractSchedTaskJobDelegate.java
index aed9012a89..d77ac1d9f9 100644
---
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/AbstractSchedTaskJobDelegate.java
+++
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/AbstractSchedTaskJobDelegate.java
@@ -154,7 +154,7 @@ public abstract class AbstractSchedTaskJobDelegate<T
extends SchedTask> implemen
OpEvent.CategoryType.TASK,
this.getClass().getSimpleName(),
null,
- this.getClass().getSimpleName(), // searching for before
object is too much expensive ...
+ this.getClass().getSimpleName(),
result,
task,
execution);
@@ -165,10 +165,10 @@ public abstract class AbstractSchedTaskJobDelegate<T
extends SchedTask> implemen
OpEvent.CategoryType.TASK,
task.getClass().getSimpleName(),
null,
- null, // searching for before object is too much expensive ...
+ this.getClass().getSimpleName(),
result,
task,
- null);
+ execution);
if (manageOperationId) {
MDC.remove(Job.OPERATION_ID);
diff --git
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/notification/AbstractNotificationJobDelegate.java
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/notification/AbstractNotificationJobDelegate.java
index fa206ba607..c6d5846c2e 100644
---
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/notification/AbstractNotificationJobDelegate.java
+++
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/notification/AbstractNotificationJobDelegate.java
@@ -89,12 +89,13 @@ public abstract class AbstractNotificationJobDelegate
implements NotificationJob
if (StringUtils.isBlank(task.getSubject()) ||
task.getRecipients().isEmpty()
|| StringUtils.isBlank(task.getHtmlBody()) ||
StringUtils.isBlank(task.getTextBody())) {
- String message = "Could not fetch all required information for
sending e-mails:\n"
- + task.getRecipients() + '\n'
- + task.getSender() + '\n'
- + task.getSubject() + '\n'
- + task.getHtmlBody() + '\n'
- + task.getTextBody();
+ String message = "Could not fetch all required information for
sending out notifications:"
+ + "\nFrom: " + task.getSender()
+ + "\nTo: " + task.getRecipients()
+ + "\nSubject: " + task.getSubject()
+ + "\nHTML body:\n" + task.getHtmlBody()
+ + "\nText body:\n" + task.getTextBody()
+ + "\n";
LOG.error(message);
execution.setStatus(NotificationJob.Status.NOT_SENT.name());
@@ -104,12 +105,8 @@ public abstract class AbstractNotificationJobDelegate
implements NotificationJob
execution.setMessage(message);
}
} else {
- LOG.debug("About to send notifications:\n{}\n{}\n{}\n{}\n{}",
- task.getRecipients(),
- task.getSender(),
- task.getSubject(),
- task.getHtmlBody(),
- task.getTextBody());
+ LOG.debug("About to send notifications:\nFrom: {}\nTo:
{}\nSubject: {}\nHTML body:\n{}\nText body:\n{}\n",
+ task.getSender(), task.getRecipients(), task.getSubject(),
task.getHtmlBody(), task.getTextBody());
setStatus("Sending notifications to " + task.getRecipients());
diff --git
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/notification/DefaultNotificationManager.java
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/notification/DefaultNotificationManager.java
index d6b88d1589..10493861ab 100644
---
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/notification/DefaultNotificationManager.java
+++
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/notification/DefaultNotificationManager.java
@@ -181,17 +181,16 @@ public class DefaultNotificationManager implements
NotificationManager {
final Any<?> any,
final Map<String, Object> jexlVars) {
- if (any != null) {
- virAttrHandler.getValues(any);
- }
+ jexlVars.put("syncopeConf",
confParamOps.list(SyncopeConstants.MASTER_DOMAIN));
+ jexlVars.put("events", notification.getEvents());
+
+ Optional.ofNullable(any).ifPresent(virAttrHandler::getValues);
List<User> recipients = new ArrayList<>();
- if (notification.getRecipientsFIQL() != null) {
- recipients.addAll(anySearchDAO.<User>search(
- SearchCondConverter.convert(searchCondVisitor,
notification.getRecipientsFIQL()),
- List.of(), AnyTypeKind.USER));
- }
+ Optional.ofNullable(notification.getRecipientsFIQL()).
+ ifPresent(fiql -> recipients.addAll(anySearchDAO.<User>search(
+ SearchCondConverter.convert(searchCondVisitor, fiql),
List.of(), AnyTypeKind.USER)));
if (notification.isSelfAsRecipient() && any instanceof User) {
recipients.add((User) any);
@@ -202,47 +201,44 @@ public class DefaultNotificationManager implements
NotificationManager {
recipients.forEach(recipient -> {
virAttrHandler.getValues(recipient);
- String email =
getRecipientEmail(notification.getRecipientAttrName(), recipient);
- if (email == null) {
- LOG.warn("{} cannot be notified: {} not found", recipient,
notification.getRecipientAttrName());
- } else {
- recipientEmails.add(email);
- recipientTOs.add(userDataBinder.getUserTO(recipient, true));
- }
+
Optional.ofNullable(getRecipientEmail(notification.getRecipientAttrName(),
recipient)).
+ ifPresentOrElse(
+ email -> {
+ recipientEmails.add(email);
+
recipientTOs.add(userDataBinder.getUserTO(recipient, true));
+ },
+ () -> LOG.warn("{} cannot be notified: {} not
found",
+ recipient,
notification.getRecipientAttrName()));
});
+ jexlVars.put("recipients", recipientTOs);
- if (notification.getStaticRecipients() != null) {
- recipientEmails.addAll(notification.getStaticRecipients());
- }
+
Optional.ofNullable(notification.getStaticRecipients()).ifPresent(recipientEmails::addAll);
- if (notification.getRecipientsProvider() != null) {
+
Optional.ofNullable(notification.getRecipientsProvider()).ifPresent(impl -> {
try {
RecipientsProvider recipientsProvider =
ImplementationManager.build(
- notification.getRecipientsProvider(),
+ impl,
() -> perContextRecipientsProvider.orElse(null),
instance -> perContextRecipientsProvider =
Optional.of(instance));
-
recipientEmails.addAll(recipientsProvider.provideRecipients(notification));
+
recipientEmails.addAll(recipientsProvider.provideRecipients(notification, any,
jexlVars));
} catch (Exception e) {
LOG.error("While building {}",
notification.getRecipientsProvider(), e);
}
- }
+ });
- jexlVars.put("recipients", recipientTOs);
- jexlVars.put("syncopeConf",
confParamOps.list(SyncopeConstants.MASTER_DOMAIN));
- jexlVars.put("events", notification.getEvents());
JexlContext ctx = new MapContext(jexlVars);
NotificationTask task =
entityFactory.newEntity(NotificationTask.class);
task.setNotification(notification);
- if (any != null) {
- task.setEntityKey(any.getKey());
- task.setAnyTypeKind(any.getType().getKind());
- }
+ Optional.ofNullable(any).ifPresent(a -> {
+ task.setEntityKey(a.getKey());
+ task.setAnyTypeKind(a.getType().getKind());
+ });
task.setTraceLevel(notification.getTraceLevel());
task.getRecipients().addAll(recipientEmails);
task.setSender(notification.getSender());
- task.setSubject(notification.getSubject());
+ task.setSubject(JexlUtils.evaluateTemplate(notification.getSubject(),
ctx));
if
(StringUtils.isNotBlank(notification.getTemplate().getTextTemplate())) {
task.setTextBody(JexlUtils.evaluateTemplate(notification.getTemplate().getTextTemplate(),
ctx));
@@ -296,8 +292,6 @@ public class DefaultNotificationManager implements
NotificationManager {
final Object output,
final Object... input) {
- String currentEvent = OpEvent.toString(type, category, subcategory,
op, outcome);
-
Optional<? extends Any<?>> any = Optional.empty();
if (before instanceof UserTO userTO) {
@@ -341,6 +335,8 @@ public class DefaultNotificationManager implements
NotificationManager {
}
if (notification.isActive()) {
+ String currentEvent = OpEvent.toString(type, category,
subcategory, op, outcome);
+
if (!notification.getEvents().contains(currentEvent)) {
LOG.debug("No events found about {}", any);
} else if (anyType == null || any.isEmpty()
@@ -350,31 +346,31 @@ public class DefaultNotificationManager implements
NotificationManager {
LOG.debug("Creating notification task for event {} about
{}", currentEvent, any);
- Map<String, Object> model = new HashMap<>();
- model.put("who", who);
- model.put("type", type);
- model.put("category", category);
- model.put("subcategory", subcategory);
- model.put("event", op);
- model.put("condition", outcome);
- model.put("before", before);
- model.put("output", output);
- model.put("input", input);
+ Map<String, Object> jexlVars = new HashMap<>();
+ jexlVars.put("who", who);
+ jexlVars.put("type", type);
+ jexlVars.put("category", category);
+ jexlVars.put("subcategory", subcategory);
+ jexlVars.put("event", op);
+ jexlVars.put("condition", outcome);
+ jexlVars.put("before", before);
+ jexlVars.put("output", output);
+ jexlVars.put("input", input);
any.ifPresent(a -> {
switch (a) {
case User user ->
- model.put("user",
userDataBinder.getUserTO(user, true));
+ jexlVars.put("user",
userDataBinder.getUserTO(user, true));
case Group group ->
- model.put("group",
groupDataBinder.getGroupTO(group, true));
+ jexlVars.put("group",
groupDataBinder.getGroupTO(group, true));
case AnyObject anyObject ->
- model.put("anyObject",
anyObjectDataBinder.getAnyObjectTO(anyObject, true));
+ jexlVars.put("anyObject",
anyObjectDataBinder.getAnyObjectTO(anyObject, true));
default -> {
}
}
});
- NotificationTask notificationTask =
getNotificationTask(notification, any.orElse(null), model);
+ NotificationTask notificationTask =
getNotificationTask(notification, any.orElse(null), jexlVars);
notificationTask = taskDAO.save(notificationTask);
notifications.add(notificationTask);
}
@@ -399,11 +395,10 @@ public class DefaultNotificationManager implements
NotificationManager {
if ("username".equals(intAttrName.getField())) {
email = user.getUsername();
} else if (intAttrName.getSchemaType() != null) {
- UMembership membership = intAttrName.getMembershipOfGroup() == null
- ? null
- : groupDAO.findByName(intAttrName.getMembershipOfGroup()).
- flatMap(group ->
user.getMembership(group.getKey())).
- orElse(null);
+ UMembership membership =
Optional.ofNullable(intAttrName.getMembershipOfGroup()).
+ flatMap(groupDAO::findByName).
+ flatMap(group -> user.getMembership(group.getKey())).
+ orElse(null);
switch (intAttrName.getSchemaType()) {
case PLAIN -> {
diff --git
a/fit/core-reference/src/main/java/org/apache/syncope/fit/core/reference/TestNotificationRecipientsProvider.java
b/fit/core-reference/src/main/java/org/apache/syncope/fit/core/reference/TestNotificationRecipientsProvider.java
index b45d84d5ef..2b17548133 100644
---
a/fit/core-reference/src/main/java/org/apache/syncope/fit/core/reference/TestNotificationRecipientsProvider.java
+++
b/fit/core-reference/src/main/java/org/apache/syncope/fit/core/reference/TestNotificationRecipientsProvider.java
@@ -18,7 +18,9 @@
*/
package org.apache.syncope.fit.core.reference;
+import java.util.Map;
import java.util.Set;
+import org.apache.syncope.core.persistence.api.entity.Any;
import org.apache.syncope.core.persistence.api.entity.Notification;
import
org.apache.syncope.core.provisioning.api.notification.RecipientsProvider;
import org.springframework.transaction.annotation.Transactional;
@@ -27,7 +29,11 @@ public class TestNotificationRecipientsProvider implements
RecipientsProvider {
@Transactional(readOnly = true)
@Override
- public Set<String> provideRecipients(final Notification notification) {
+ public Set<String> provideRecipients(
+ final Notification notification,
+ final Any<?> any,
+ final Map<String, Object> jexlVars) {
+
return Set.of(getClass().getSimpleName() + "@syncope.apache.org");
}
}
diff --git
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/NotificationTaskITCase.java
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/NotificationTaskITCase.java
index 357f57fe9a..4637d8a806 100644
---
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/NotificationTaskITCase.java
+++
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/NotificationTaskITCase.java
@@ -273,7 +273,7 @@ public class NotificationTaskITCase extends
AbstractNotificationTaskITCase {
assertNotNull(taskTO);
assertNotNull(taskTO.getNotification());
assertTrue(taskTO.getRecipients().containsAll(
- new
TestNotificationRecipientsProvider().provideRecipients(null)));
+ new
TestNotificationRecipientsProvider().provideRecipients(null, null, null)));
execNotificationTask(TASK_SERVICE, taskTO.getKey(), MAX_WAIT_SECONDS);