Repository: syncope Updated Branches: refs/heads/1_2_X 4a69793e7 -> 188b937d6
[SYNCOPE-1087] Check if notifications are available or audit was requested for the ongoing event before performing any actual read Project: http://git-wip-us.apache.org/repos/asf/syncope/repo Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/188b937d Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/188b937d Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/188b937d Branch: refs/heads/1_2_X Commit: 188b937d64f72d3e0304744fca9542afd25aab38 Parents: 4a69793 Author: Francesco Chicchiriccò <[email protected]> Authored: Mon May 22 10:48:06 2017 +0200 Committer: Francesco Chicchiriccò <[email protected]> Committed: Mon May 22 10:48:06 2017 +0200 ---------------------------------------------------------------------- .../syncope/common/types/AuditLoggerName.java | 4 +- .../apache/syncope/core/audit/AuditManager.java | 46 +++++++++++++- .../core/notification/NotificationManager.java | 41 ++++++++++++- .../core/rest/controller/ControllerHandler.java | 63 ++++++++++++-------- 4 files changed, 123 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/syncope/blob/188b937d/common/src/main/java/org/apache/syncope/common/types/AuditLoggerName.java ---------------------------------------------------------------------- diff --git a/common/src/main/java/org/apache/syncope/common/types/AuditLoggerName.java b/common/src/main/java/org/apache/syncope/common/types/AuditLoggerName.java index 69e2811..18998bd 100644 --- a/common/src/main/java/org/apache/syncope/common/types/AuditLoggerName.java +++ b/common/src/main/java/org/apache/syncope/common/types/AuditLoggerName.java @@ -48,14 +48,14 @@ public class AuditLoggerName extends AbstractBaseBean { @JsonProperty("category") final String category, @JsonProperty("subcategory") final String subcategory, @JsonProperty("event") final String event, - @JsonProperty("result") final Result result) + @JsonProperty("result") final Result condition) throws IllegalArgumentException { this.type = type == null ? AuditElements.EventCategoryType.CUSTOM : type; this.category = category; this.subcategory = subcategory; this.event = event; - this.result = result == null ? Result.SUCCESS : result; + this.result = condition == null ? Result.SUCCESS : condition; } public AuditElements.EventCategoryType getType() { http://git-wip-us.apache.org/repos/asf/syncope/blob/188b937d/core/src/main/java/org/apache/syncope/core/audit/AuditManager.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/syncope/core/audit/AuditManager.java b/core/src/main/java/org/apache/syncope/core/audit/AuditManager.java index 644d1c3..7e373bd 100644 --- a/core/src/main/java/org/apache/syncope/core/audit/AuditManager.java +++ b/core/src/main/java/org/apache/syncope/core/audit/AuditManager.java @@ -41,12 +41,54 @@ public class AuditManager { @Autowired private LoggerDAO loggerDAO; + /** + * Checks if audit is requested matching the provided conditions. + * + * @param type event category type + * @param category event category + * @param subcategory event subcategory + * @param event event + * @return created notification tasks + */ + public boolean auditRequested( + final AuditElements.EventCategoryType type, + final String category, + final String subcategory, + final String event) { + + AuditLoggerName auditLoggerName = new AuditLoggerName(type, category, subcategory, event, Result.SUCCESS); + SyncopeLogger syncopeLogger = loggerDAO.find(auditLoggerName.toLoggerName()); + boolean auditRequested = syncopeLogger != null && syncopeLogger.getLevel() == LoggerLevel.DEBUG; + + if (auditRequested) { + return true; + } + + auditLoggerName = new AuditLoggerName(type, category, subcategory, event, Result.FAILURE); + syncopeLogger = loggerDAO.find(auditLoggerName.toLoggerName()); + auditRequested = syncopeLogger != null && syncopeLogger.getLevel() == LoggerLevel.DEBUG; + + return auditRequested; + } + + /** + * Create notification tasks for each notification matching provided conditions. + * + * @param type event category type + * @param category event category + * @param subcategory event subcategory + * @param event event + * @param condition result value condition. + * @param before object(s) available before the event + * @param output object(s) produced by the event + * @param input object(s) provided to the event + */ public void audit( final AuditElements.EventCategoryType type, final String category, final String subcategory, final String event, - final Result result, + final Result condition, final Object before, final Object output, final Object... input) { @@ -79,7 +121,7 @@ public class AuditManager { AuditLoggerName auditLoggerName = null; try { - auditLoggerName = new AuditLoggerName(type, category, subcategory, event, result); + auditLoggerName = new AuditLoggerName(type, category, subcategory, event, condition); } catch (IllegalArgumentException e) { LOG.error("Invalid audit parameters, aborting...", e); } http://git-wip-us.apache.org/repos/asf/syncope/blob/188b937d/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java b/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java index fead0a1..78ad317 100644 --- a/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java +++ b/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java @@ -260,7 +260,46 @@ public class NotificationManager { } /** - * Create notification tasks for each notification matching the given user id and (some of) tasks performed. + * Checks if notifications are available matching the provided conditions. + * + * @param type event category type + * @param category event category + * @param subcategory event subcategory + * @param event event + * @return created notification tasks + */ + public boolean notificationsAvailable( + final AuditElements.EventCategoryType type, + final String category, + final String subcategory, + final String event) { + + final String successEvent = LoggerEventUtils.buildEvent(type, category, subcategory, event, Result.SUCCESS); + final String failureEvent = LoggerEventUtils.buildEvent(type, category, subcategory, event, Result.FAILURE); + for (Notification notification : notificationDAO.findAll()) { + if (notification.isActive() + && (notification.getEvents().contains(successEvent) + || notification.getEvents().contains(failureEvent))) { + + return true; + } + } + + return false; + } + + /** + * Create notification tasks for each notification matching provided conditions. + * + * @param type event category type + * @param category event category + * @param subcategory event subcategory + * @param event event + * @param condition result value condition. + * @param before object(s) availabile before the event + * @param output object(s) produced by the event + * @param input object(s) provided to the event + * @return created notification tasks */ public List<NotificationTask> createTasks( final AuditElements.EventCategoryType type, http://git-wip-us.apache.org/repos/asf/syncope/blob/188b937d/core/src/main/java/org/apache/syncope/core/rest/controller/ControllerHandler.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/syncope/core/rest/controller/ControllerHandler.java b/core/src/main/java/org/apache/syncope/core/rest/controller/ControllerHandler.java index 8f9349f..3288981 100644 --- a/core/src/main/java/org/apache/syncope/core/rest/controller/ControllerHandler.java +++ b/core/src/main/java/org/apache/syncope/core/rest/controller/ControllerHandler.java @@ -58,7 +58,12 @@ public class ControllerHandler { final String event = joinPoint.getSignature().getName(); - AuditElements.Result result = null; + boolean notificationsAvailable = notificationManager.notificationsAvailable( + AuditElements.EventCategoryType.REST, category, null, event); + boolean auditRequested = auditManager.auditRequested( + AuditElements.EventCategoryType.REST, category, null, event); + + AuditElements.Result condition = null; Object output = null; Object before = null; @@ -66,43 +71,49 @@ public class ControllerHandler { LOG.debug("Before {}.{}({})", clazz.getSimpleName(), event, input == null || input.length == 0 ? "" : Arrays.asList(input)); - try { - before = ((AbstractController) joinPoint.getTarget()).resolveBeanReference(method, input); - } catch (UnresolvedReferenceException ignore) { - LOG.debug("Unresolved bean reference ..."); + if (notificationsAvailable || auditRequested) { + try { + before = ((AbstractController) joinPoint.getTarget()).resolveBeanReference(method, input); + } catch (UnresolvedReferenceException ignore) { + LOG.debug("Unresolved bean reference ..."); + } } output = joinPoint.proceed(); - result = AuditElements.Result.SUCCESS; + condition = AuditElements.Result.SUCCESS; LOG.debug("After returning {}.{}: {}", clazz.getSimpleName(), event, output); return output; } catch (Throwable t) { output = t; - result = AuditElements.Result.FAILURE; + condition = AuditElements.Result.FAILURE; LOG.debug("After throwing {}.{}", clazz.getSimpleName(), event); throw t; } finally { - notificationManager.createTasks( - AuditElements.EventCategoryType.REST, - category, - null, - event, - result, - before, - output, - input); - - auditManager.audit( - AuditElements.EventCategoryType.REST, - category, - null, - event, - result, - before, - output, - input); + if (notificationsAvailable) { + notificationManager.createTasks( + AuditElements.EventCategoryType.REST, + category, + null, + event, + condition, + before, + output, + input); + } + + if (auditRequested) { + auditManager.audit( + AuditElements.EventCategoryType.REST, + category, + null, + event, + condition, + before, + output, + input); + } } } }
