You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2017/05/22 07:40:02 UTC

[2/2] syncope git commit: [SYNCOPE-1087] Check if notifications are available or audit was requested for the ongoing event before performing any actual read

[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/5ab65808
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/5ab65808
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/5ab65808

Branch: refs/heads/master
Commit: 5ab65808ec565ef50b970e96be011edfce8eabff
Parents: 9b7abc1
Author: Francesco Chicchiriccò <il...@apache.org>
Authored: Mon May 22 09:39:39 2017 +0200
Committer: Francesco Chicchiriccò <il...@apache.org>
Committed: Mon May 22 09:39:51 2017 +0200

----------------------------------------------------------------------
 .../common/lib/types/AuditLoggerName.java       |  8 +--
 .../core/logic/LogicInvocationHandler.java      | 64 ++++++++++++--------
 .../core/provisioning/api/AuditManager.java     | 29 ++++++++-
 .../api/notification/NotificationManager.java   | 19 +++++-
 .../provisioning/java/AuditManagerImpl.java     | 39 +++++++++++-
 .../notification/NotificationManagerImpl.java   | 22 +++++++
 6 files changed, 145 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/5ab65808/common/lib/src/main/java/org/apache/syncope/common/lib/types/AuditLoggerName.java
----------------------------------------------------------------------
diff --git a/common/lib/src/main/java/org/apache/syncope/common/lib/types/AuditLoggerName.java b/common/lib/src/main/java/org/apache/syncope/common/lib/types/AuditLoggerName.java
index 6988062..9c071b8 100644
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/types/AuditLoggerName.java
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/types/AuditLoggerName.java
@@ -164,7 +164,7 @@ public class AuditLoggerName extends AbstractBaseBean {
      * @param category event category.
      * @param subcategory event subcategory.
      * @param event event.
-     * @param resultValueCondition result value condition.
+     * @param condition result value condition.
      * @return event string.
      */
     public static String buildEvent(
@@ -172,7 +172,7 @@ public class AuditLoggerName extends AbstractBaseBean {
             final String category,
             final String subcategory,
             final String event,
-            final AuditElements.Result resultValueCondition) {
+            final AuditElements.Result condition) {
 
         final StringBuilder eventBuilder = new StringBuilder();
 
@@ -198,9 +198,9 @@ public class AuditLoggerName extends AbstractBaseBean {
         }
         eventBuilder.append(']');
 
-        if (resultValueCondition != null) {
+        if (condition != null) {
             eventBuilder.append(":[").
-                    append(resultValueCondition).
+                    append(condition).
                     append(']');
         }
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/5ab65808/core/logic/src/main/java/org/apache/syncope/core/logic/LogicInvocationHandler.java
----------------------------------------------------------------------
diff --git a/core/logic/src/main/java/org/apache/syncope/core/logic/LogicInvocationHandler.java b/core/logic/src/main/java/org/apache/syncope/core/logic/LogicInvocationHandler.java
index 7dd2801..87eda20 100644
--- a/core/logic/src/main/java/org/apache/syncope/core/logic/LogicInvocationHandler.java
+++ b/core/logic/src/main/java/org/apache/syncope/core/logic/LogicInvocationHandler.java
@@ -20,6 +20,7 @@ package org.apache.syncope.core.logic;
 
 import java.lang.reflect.Method;
 import java.util.Arrays;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.syncope.common.lib.types.AuditElements;
 import org.apache.syncope.core.provisioning.api.AuditManager;
 import org.apache.syncope.core.provisioning.api.notification.NotificationManager;
@@ -55,49 +56,60 @@ public class LogicInvocationHandler {
 
         String event = joinPoint.getSignature().getName();
 
-        AuditElements.Result result = null;
+        boolean notificationsAvailable = notificationManager.notificationsAvailable(
+                AuditElements.EventCategoryType.LOGIC, category, null, event);
+        boolean auditRequested = auditManager.auditRequested(
+                AuditElements.EventCategoryType.LOGIC, category, null, event);
+
+        AuditElements.Result condition = null;
         Object output = null;
         Object before = null;
 
         try {
             LOG.debug("Before {}.{}({})", clazz.getSimpleName(), event,
-                    input == null || input.length == 0 ? "" : Arrays.asList(input));
-
-            try {
-                before = ((AbstractLogic) joinPoint.getTarget()).resolveBeanReference(method, input);
-            } catch (UnresolvedReferenceException ignore) {
-                LOG.debug("Unresolved bean reference ...");
+                    input == null || input.length == 0 ? StringUtils.EMPTY : Arrays.asList(input));
+
+            if (notificationsAvailable || auditRequested) {
+                try {
+                    before = ((AbstractLogic) 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.LOGIC,
-                    category,
-                    null,
-                    event,
-                    result,
-                    before,
-                    output,
-                    input);
-
-            auditManager.audit(AuditElements.EventCategoryType.LOGIC,
-                    category,
-                    null,
-                    event,
-                    result,
-                    before,
-                    output,
-                    input);
+            if (notificationsAvailable) {
+                notificationManager.createTasks(AuditElements.EventCategoryType.LOGIC,
+                        category,
+                        null,
+                        event,
+                        condition,
+                        before,
+                        output,
+                        input);
+            }
+
+            if (auditRequested) {
+                auditManager.audit(AuditElements.EventCategoryType.LOGIC,
+                        category,
+                        null,
+                        event,
+                        condition,
+                        before,
+                        output,
+                        input);
+            }
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/5ab65808/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/AuditManager.java
----------------------------------------------------------------------
diff --git a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/AuditManager.java b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/AuditManager.java
index 58b0303..333d415 100644
--- a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/AuditManager.java
+++ b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/AuditManager.java
@@ -22,12 +22,39 @@ import org.apache.syncope.common.lib.types.AuditElements;
 
 public interface AuditManager {
 
+    /**
+     * 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
+     */
+    boolean auditRequested(
+            AuditElements.EventCategoryType type,
+            String category,
+            String subcategory,
+            String event);
+
+    /**
+     * 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
+     */
     void audit(
             AuditElements.EventCategoryType type,
             String category,
             String subcategory,
             String event,
-            AuditElements.Result result,
+            AuditElements.Result condition,
             Object before,
             Object output,
             Object... input);

http://git-wip-us.apache.org/repos/asf/syncope/blob/5ab65808/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/notification/NotificationManager.java
----------------------------------------------------------------------
diff --git a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/notification/NotificationManager.java b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/notification/NotificationManager.java
index 65d9412..a297960 100644
--- a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/notification/NotificationManager.java
+++ b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/notification/NotificationManager.java
@@ -40,13 +40,28 @@ public interface NotificationManager {
     long countExecutionsWithStatus(final String taskKey, final String status);
 
     /**
+     * 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
+     */
+    boolean notificationsAvailable(
+            AuditElements.EventCategoryType type,
+            String category,
+            String subcategory,
+            String event);
+
+    /**
      * 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 result event result
+     * @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
@@ -57,7 +72,7 @@ public interface NotificationManager {
             String category,
             String subcategory,
             String event,
-            AuditElements.Result result,
+            AuditElements.Result condition,
             Object before,
             Object output,
             Object... input);

http://git-wip-us.apache.org/repos/asf/syncope/blob/5ab65808/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/AuditManagerImpl.java
----------------------------------------------------------------------
diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/AuditManagerImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/AuditManagerImpl.java
index b692472..3a1dda4 100644
--- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/AuditManagerImpl.java
+++ b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/AuditManagerImpl.java
@@ -33,6 +33,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
 
+@Transactional(readOnly = true)
 @Component
 public class AuditManagerImpl implements AuditManager {
 
@@ -43,14 +44,46 @@ public class AuditManagerImpl implements AuditManager {
         return LoggerType.AUDIT.getPrefix() + "." + domain;
     }
 
-    @Transactional(readOnly = true)
+    @Override
+    public boolean auditRequested(
+            final AuditElements.EventCategoryType type,
+            final String category,
+            final String subcategory,
+            final String event) {
+
+        AuditEntry auditEntry = new AuditEntry(
+                AuthContextUtils.getUsername(),
+                new AuditLoggerName(type, category, subcategory, event, Result.SUCCESS),
+                null,
+                null,
+                null);
+        org.apache.syncope.core.persistence.api.entity.Logger syncopeLogger =
+                loggerDAO.find(auditEntry.getLogger().toLoggerName());
+        boolean auditRequested = syncopeLogger != null && syncopeLogger.getLevel() == LoggerLevel.DEBUG;
+
+        if (auditRequested) {
+            return true;
+        }
+
+        auditEntry = new AuditEntry(
+                AuthContextUtils.getUsername(),
+                new AuditLoggerName(type, category, subcategory, event, Result.FAILURE),
+                null,
+                null,
+                null);
+        syncopeLogger = loggerDAO.find(auditEntry.getLogger().toLoggerName());
+        auditRequested = syncopeLogger != null && syncopeLogger.getLevel() == LoggerLevel.DEBUG;
+
+        return auditRequested;
+    }
+
     @Override
     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) {
@@ -62,7 +95,7 @@ public class AuditManagerImpl implements AuditManager {
 
         AuditEntry auditEntry = new AuditEntry(
                 AuthContextUtils.getUsername(),
-                new AuditLoggerName(type, category, subcategory, event, result),
+                new AuditLoggerName(type, category, subcategory, event, condition),
                 before,
                 throwable == null ? output : throwable.getMessage(),
                 input);

http://git-wip-us.apache.org/repos/asf/syncope/blob/5ab65808/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/notification/NotificationManagerImpl.java
----------------------------------------------------------------------
diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/notification/NotificationManagerImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/notification/NotificationManagerImpl.java
index 1a021ae..06df8d6 100644
--- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/notification/NotificationManagerImpl.java
+++ b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/notification/NotificationManagerImpl.java
@@ -26,6 +26,8 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import org.apache.commons.collections4.IterableUtils;
+import org.apache.commons.collections4.Predicate;
 import org.apache.commons.jexl3.MapContext;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.tuple.Pair;
@@ -255,6 +257,26 @@ public class NotificationManagerImpl implements NotificationManager {
     }
 
     @Override
+    public boolean notificationsAvailable(
+            final AuditElements.EventCategoryType type,
+            final String category,
+            final String subcategory,
+            final String event) {
+
+        final String successEvent = AuditLoggerName.buildEvent(type, category, subcategory, event, Result.SUCCESS);
+        final String failureEvent = AuditLoggerName.buildEvent(type, category, subcategory, event, Result.FAILURE);
+        return IterableUtils.matchesAny(notificationDAO.findAll(), new Predicate<Notification>() {
+
+            @Override
+            public boolean evaluate(final Notification notification) {
+                return notification.isActive()
+                        && (notification.getEvents().contains(successEvent)
+                        || notification.getEvents().contains(failureEvent));
+            }
+        });
+    }
+
+    @Override
     public List<NotificationTask> createTasks(
             final AuditElements.EventCategoryType type,
             final String category,