Work in progress

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/commit/a3f0bc44
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/tree/a3f0bc44
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/diff/a3f0bc44

Branch: refs/heads/master
Commit: a3f0bc44785cf1709811df1dde35f3647939083d
Parents: df058b3
Author: Ralph Goers <[email protected]>
Authored: Fri Dec 29 11:04:36 2017 -0700
Committer: Ralph Goers <[email protected]>
Committed: Fri Dec 29 11:04:36 2017 -0700

----------------------------------------------------------------------
 .../log4j/audit/AbstractEventLogger.java        | 10 +++
 .../log4j/audit/catalog/CatalogManager.java     | 33 +++++++--
 .../log4j/audit/catalog/CatalogManagerImpl.java | 73 +++++++++++++++-----
 .../logging/log4j/audit/dto/AuditDto.java       | 21 ++++++
 .../service/catalog/AuditCatalogManager.java    |  1 -
 .../service/controller/AuditController.java     |  2 +-
 .../service/controller/CatalogController.java   | 20 ++++++
 .../RestResponseEntityExceptionHandler.java     |  2 +
 .../apache/logging/log4j/catalog/api/Event.java |  1 -
 9 files changed, 138 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/a3f0bc44/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java
----------------------------------------------------------------------
diff --git 
a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java
 
b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java
index 858a55d..05fce15 100644
--- 
a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java
+++ 
b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java
@@ -32,6 +32,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import static org.apache.logging.log4j.catalog.api.constant.Constants.*;
+
 /**
  *
  */
@@ -84,6 +86,14 @@ public abstract class AbstractEventLogger {
         logEvent(eventName, attributes, event, defaultAuditExceptionHandler);
     }
 
+    public void logEvent(String eventName, String catalogId, Map<String, 
String> attributes) {
+        Event event = catalogManager.getEvent(eventName, catalogId);
+        if (event == null) {
+            throw new AuditException("Unable to locate definition of audit 
event " + eventName);
+        }
+        logEvent(eventName, attributes, event, defaultAuditExceptionHandler);
+    }
+
     public void logEvent(String eventName, Map<String, String> attributes, 
AuditExceptionHandler exceptionHandler) {
         Event event = catalogManager.getEvent(eventName);
 

http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/a3f0bc44/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManager.java
----------------------------------------------------------------------
diff --git 
a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManager.java
 
b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManager.java
index 112e4e0..be27b3a 100644
--- 
a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManager.java
+++ 
b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManager.java
@@ -18,25 +18,46 @@ package org.apache.logging.log4j.audit.catalog;
 
 import org.apache.logging.log4j.catalog.api.Attribute;
 import org.apache.logging.log4j.catalog.api.Event;
-import org.apache.logging.log4j.catalog.api.EventAttribute;
 
 import java.util.List;
 import java.util.Map;
 
+import static 
org.apache.logging.log4j.catalog.api.constant.Constants.DEFAULT_CATALOG;
+
 /**
  *
  */
 public interface CatalogManager {
 
-    Event getEvent(String eventName);
+    default Event getEvent(String eventName) {
+        return getEvent(eventName, DEFAULT_CATALOG);
+    }
+
+    Event getEvent(String eventName, String catalogId);
+
+    default List<String> getRequiredContextAttributes(String eventName) {
+        return getRequiredContextAttributes(eventName, DEFAULT_CATALOG);
+    }
 
-    List<String> getRequiredContextAttributes(String eventName);
+    List<String> getRequiredContextAttributes(String eventName, String 
catalogId);
 
-    List<String> getAttributeNames(String eventName);
+    default List<String> getAttributeNames(String eventName) {
+        return getAttributeNames(eventName, DEFAULT_CATALOG);
+    }
 
-    Map<String, Attribute> getAttributes(String eventName);
+    List<String> getAttributeNames(String eventName, String catalogId);
+
+    default Map<String, Attribute> getAttributes(String eventName) {
+        return getAttributes(eventName, DEFAULT_CATALOG);
+    }
+
+    Map<String, Attribute> getAttributes(String eventName, String catalogId);
 
     Map<String, Attribute> getRequestContextAttributes();
 
-    Attribute getAttribute(String attributeName);
+    default Attribute getAttribute(String attributeName) {
+        return getAttribute(attributeName, DEFAULT_CATALOG);
+    }
+
+    Attribute getAttribute(String attributeName, String catalogId);
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/a3f0bc44/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java
----------------------------------------------------------------------
diff --git 
a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java
 
b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java
index 4fbf50e..fd503c7 100644
--- 
a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java
+++ 
b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java
@@ -35,6 +35,8 @@ import org.apache.logging.log4j.catalog.api.Event;
 import org.apache.logging.log4j.catalog.api.CatalogReader;
 import org.apache.logging.log4j.catalog.api.EventAttribute;
 
+import static 
org.apache.logging.log4j.catalog.api.constant.Constants.DEFAULT_CATALOG;
+
 /**
  *
  */
@@ -42,14 +44,16 @@ public class CatalogManagerImpl implements CatalogManager {
 
     private static Logger logger = 
LogManager.getLogger(CatalogManagerImpl.class);
 
-    private volatile Map<String, CatalogInfo> infoMap;
+    private volatile Map<String, Map<String, CatalogInfo>> infoMap;
 
     private Map<String, Attribute> requestContextAttributes = new HashMap<>();
 
-    private final Map<String, Attribute> attributeMap = new HashMap<>();
+    private final Map<String, Map<String, Attribute>> attributeMap = new 
HashMap<>();
 
     private static final String REQCTX = "ReqCtx_";
 
+
+
     protected CatalogData catalogData;
 
     public CatalogManagerImpl(CatalogReader catalogReader) {
@@ -61,29 +65,47 @@ public class CatalogManagerImpl implements CatalogManager {
     }
 
     @Override
-    public Event getEvent(String eventName) {
-        CatalogInfo info = infoMap.get(eventName);
+    public Event getEvent(String eventName, String catalogId) {
+        CatalogInfo info = getCatalogInfo(eventName, catalogId);
         return info != null ? info.event : null;
     }
 
     @Override
-    public List<String> getRequiredContextAttributes(String eventName) {
-        return infoMap.get(eventName).requiredContextAttributes;
+    public List<String> getRequiredContextAttributes(String eventName, String 
catalogId) {
+        Map<String, CatalogInfo> catalogMap = infoMap.get(catalogId == null ? 
DEFAULT_CATALOG : catalogId);
+        return catalogMap != null ? 
catalogMap.get(eventName).requiredContextAttributes : null;
     }
 
     @Override
-    public Map<String, Attribute> getAttributes(String eventName) {
-        return infoMap.get(eventName).attributes;
+    public Map<String, Attribute> getAttributes(String eventName, String 
catalogId) {
+        Event event = getEvent(eventName, catalogId);
+        Map<String, Attribute> attributes = new 
HashMap<>(event.getAttributes().size());
+        for (EventAttribute eventAttribute : event.getAttributes()) {
+            Attribute attr = getAttribute(eventAttribute.getName(), 
event.getCatalogId());
+            if (attr != null) {
+                attributes.put(attr.getName(), attr);
+            }
+        }
+        return attributes;
     }
 
     @Override
-    public List<String> getAttributeNames(String eventName) {
-        return infoMap.get(eventName).attributeNames;
+    public List<String> getAttributeNames(String eventName, String catalogId) {
+        return infoMap.get(catalogId == null ? DEFAULT_CATALOG : 
catalogId).get(eventName).attributeNames;
     }
 
     @Override
     public Attribute getAttribute(String name) {
-        return attributeMap.get(name);
+        Map<String, Attribute> attrMap = attributeMap.get(DEFAULT_CATALOG);
+        return attrMap != null ? attrMap.get(name) : null;
+    }
+
+    public Attribute getAttribute(String name, String catalogId) {
+        Map<String, Attribute> attrMap = attributeMap.get(catalogId);
+        if (attrMap == null || !attrMap.containsKey(name)) {
+            attrMap = attributeMap.get(DEFAULT_CATALOG);
+        }
+        return attrMap != null ? attrMap.get(name) : null;
     }
 
     @Override
@@ -91,7 +113,14 @@ public class CatalogManagerImpl implements CatalogManager {
         return requestContextAttributes;
     }
 
-    private Map<String, CatalogInfo> initializeData(CatalogReader 
catalogReader) throws Exception {
+    private CatalogInfo getCatalogInfo(String eventName, String catalogId) {
+        Map<String, CatalogInfo> defaultCatalog = infoMap.get(DEFAULT_CATALOG);
+        Map<String, CatalogInfo> catalog = catalogId != null ? 
infoMap.get(catalogId) : null;
+        return catalog != null && catalog.containsKey(eventName) ? 
catalog.get(eventName) :
+                defaultCatalog.get(eventName);
+    }
+
+    private Map<String, Map<String, CatalogInfo>> initializeData(CatalogReader 
catalogReader) throws Exception {
         String catalog = catalogReader.readCatalog();
         JsonFactory factory = new JsonFactory();
         factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
@@ -101,19 +130,29 @@ public class CatalogManagerImpl implements CatalogManager 
{
             if (attr.isRequestContext()) {
                 requestContextAttributes.put(attr.getName(), attr);
             }
-            attributeMap.put(attr.getName(), attr);
+            Map<String, Attribute> attrMap = 
attributeMap.get(attr.getCatalogId());
+            if (attrMap == null) {
+                attrMap = new HashMap<>();
+                attributeMap.put(attr.getCatalogId(), attrMap);
+            }
+            attrMap.put(attr.getName(), attr);
         }
-        Map<String, CatalogInfo> map = new 
HashMap<>(catalogData.getEvents().size());
+        Map<String, Map<String, CatalogInfo>> map = new HashMap<>();
+        map.put(DEFAULT_CATALOG, new HashMap<>());
         for (Event event : catalogData.getEvents()) {
             CatalogInfo info = new CatalogInfo();
             info.event = event;
+            String catalogId = event.getCatalogId();
+            if (catalogId != null && catalogId.length() > 0 && 
!map.containsKey(catalogId)) {
+                map.put(catalogId, new HashMap<>());
+            }
             List<String> required = new ArrayList<>();
             List<String> names = new ArrayList<>();
             info.attributes = new HashMap<>(names.size());
             if (event.getAttributes() != null) {
                 for (EventAttribute eventAttribute : event.getAttributes()) {
                     String name = eventAttribute.getName();
-                    Attribute attribute = attributeMap.get(name);
+                    Attribute attribute = getAttribute(name, 
event.getCatalogId());
                     info.attributes.put(name, attribute);
                     if (name.indexOf('.') != -1) {
                         name = name.replaceAll("\\.", "");
@@ -135,7 +174,9 @@ public class CatalogManagerImpl implements CatalogManager {
             }
             info.requiredContextAttributes = required;
             info.attributeNames = names;
-            map.put(NamingUtils.getFieldName(event.getName()), info);
+            Map<String, CatalogInfo> catalogMap = catalogId == null ?
+                    map.get(DEFAULT_CATALOG) : map.get(catalogId);
+            catalogMap.put(NamingUtils.getFieldName(event.getName()), info);
         }
         return map;
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/a3f0bc44/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/dto/AuditDto.java
----------------------------------------------------------------------
diff --git 
a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/dto/AuditDto.java
 
b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/dto/AuditDto.java
index ff6eea2..4830658 100644
--- 
a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/dto/AuditDto.java
+++ 
b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/dto/AuditDto.java
@@ -28,6 +28,11 @@ public class AuditDto {
     private String eventName;
 
     /**
+     * The catalog to use. If null the default catalog is used.
+     */
+    private String catalogId;
+
+    /**
      * The RequestContext Map.
      */
     private Map<String, String> requestContextMap;
@@ -54,6 +59,22 @@ public class AuditDto {
     }
 
     /**
+     * Get the Catalog id.
+     * @return the catalog id.
+     */
+    public String getCatalogId() {
+        return catalogId;
+    }
+
+    /**
+     * Set the catalog id.
+     * @param catalogId The catalog id.
+     */
+    public void setCatalogId(String catalogId) {
+        this.catalogId = catalogId;
+    }
+
+    /**
      * Returns the RequestContext data map.
      * @return A Map containing all the RequestContext keys and values.
      */

http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/a3f0bc44/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/catalog/AuditCatalogManager.java
----------------------------------------------------------------------
diff --git 
a/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/catalog/AuditCatalogManager.java
 
b/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/catalog/AuditCatalogManager.java
index 6e11f97..236bd82 100644
--- 
a/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/catalog/AuditCatalogManager.java
+++ 
b/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/catalog/AuditCatalogManager.java
@@ -3,7 +3,6 @@ package org.apache.logging.log4j.audit.service.catalog;
 import javax.annotation.PostConstruct;
 import java.sql.Timestamp;
 import java.time.Instant;
-import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;

http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/a3f0bc44/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/AuditController.java
----------------------------------------------------------------------
diff --git 
a/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/AuditController.java
 
b/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/AuditController.java
index ccd7b73..b934a53 100644
--- 
a/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/AuditController.java
+++ 
b/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/AuditController.java
@@ -53,7 +53,7 @@ public class AuditController {
             for (Map.Entry<String, String> entry : 
auditDto.getRequestContextMap().entrySet()) {
                 ThreadContext.put(entry.getKey(), entry.getValue());
             }
-            auditLogger.logEvent(auditDto.getEventName(), 
auditDto.getProperties());
+            auditLogger.logEvent(auditDto.getEventName(), 
auditDto.getCatalogId(), auditDto.getProperties());
         } finally {
             ThreadContext.clearMap();
         }

http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/a3f0bc44/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/CatalogController.java
----------------------------------------------------------------------
diff --git 
a/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/CatalogController.java
 
b/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/CatalogController.java
index 0459d2f..3ccfaae 100644
--- 
a/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/CatalogController.java
+++ 
b/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/CatalogController.java
@@ -162,6 +162,11 @@ public class CatalogController {
         if (DEFAULT_CATALOG.equals(attribute.getCatalogId())) {
             throw new IllegalArgumentException("The default catalog cannot be 
modified at run time.");
         }
+        Optional<AttributeModel> opt = 
attributeService.getAttribute(attribute.getCatalogId(), attribute.getName());
+        if (opt != null && opt.isPresent()) {
+            throw new IllegalStateException("An attribute named "+ 
attribute.getName() + " in catalog " +
+                    attribute.getCatalogId() + " already exists");
+        }
         AttributeModel model = attributeConverter.convert(attribute);
         model = attributeService.saveAttribute(model);
         return new ResponseEntity<>(attributeModelConverter.convert(model), 
HttpStatus.CREATED);
@@ -233,6 +238,11 @@ public class CatalogController {
         if (DEFAULT_CATALOG.equals(event.getCatalogId())) {
             throw new IllegalArgumentException("The default catalog cannot be 
modified at run time.");
         }
+        Optional<EventModel> opt = eventService.getEvent(event.getCatalogId(), 
event.getName());
+        if (opt != null && opt.isPresent()) {
+            throw new IllegalStateException("An event named "+ event.getName() 
+ " in catalog " +
+                    event.getCatalogId() + " already exists");
+        }
         EventModel model = eventConverter.convert(event);
         model = eventService.saveEvent(model);
         return new ResponseEntity<>(eventModelConverter.convert(model), 
HttpStatus.CREATED);
@@ -301,6 +311,11 @@ public class CatalogController {
         if (DEFAULT_CATALOG.equals(product.getCatalogId())) {
             throw new IllegalArgumentException("The default catalog cannot be 
modified at run time.");
         }
+        Optional<ProductModel> opt = 
productService.getProduct(product.getCatalogId(), product.getName());
+        if (opt != null && opt.isPresent()) {
+            throw new IllegalStateException("A product named "+ 
product.getName() + " in catalog " +
+                    product.getCatalogId() + " already exists");
+        }
         ProductModel model = productConverter.convert(product);
         model = productService.saveProduct(model);
         return new ResponseEntity<>(productModelConverter.convert(model), 
HttpStatus.CREATED);
@@ -369,6 +384,11 @@ public class CatalogController {
         if (DEFAULT_CATALOG.equals(category.getCatalogId())) {
             throw new IllegalArgumentException("The default catalog cannot be 
modified at run time.");
         }
+        Optional<CategoryModel> opt = 
categoryService.getCategory(category.getCatalogId(), category.getName());
+        if (opt != null && opt.isPresent()) {
+            throw new IllegalStateException("A category named "+ 
category.getName() + " in catalog " +
+                    category.getCatalogId() + " already exists");
+        }
         CategoryModel model = categoryConverter.convert(category);
         model = categoryService.saveCategory(model);
         return new ResponseEntity<>(categoryModelConverter.convert(model), 
HttpStatus.CREATED);

http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/a3f0bc44/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/RestResponseEntityExceptionHandler.java
----------------------------------------------------------------------
diff --git 
a/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/RestResponseEntityExceptionHandler.java
 
b/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/RestResponseEntityExceptionHandler.java
index ccb2b15..200c29e 100644
--- 
a/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/RestResponseEntityExceptionHandler.java
+++ 
b/log4j-audit/log4j-audit-war/src/main/java/org/apache/logging/log4j/audit/service/controller/RestResponseEntityExceptionHandler.java
@@ -36,6 +36,8 @@ public class RestResponseEntityExceptionHandler extends 
ResponseEntityExceptionH
     public ResponseEntity<?> handleAnyException(Exception e) {
         if (e instanceof IllegalArgumentException) {
             return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST);
+        } else if (e instanceof IllegalStateException) {
+            return new ResponseEntity(e.getMessage(), HttpStatus.CONFLICT);
         }
         return errorResponse(e, HttpStatus.INTERNAL_SERVER_ERROR);
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/a3f0bc44/log4j-catalog/log4j-catalog-api/src/main/java/org/apache/logging/log4j/catalog/api/Event.java
----------------------------------------------------------------------
diff --git 
a/log4j-catalog/log4j-catalog-api/src/main/java/org/apache/logging/log4j/catalog/api/Event.java
 
b/log4j-catalog/log4j-catalog-api/src/main/java/org/apache/logging/log4j/catalog/api/Event.java
index 8328791..b6b0b01 100644
--- 
a/log4j-catalog/log4j-catalog-api/src/main/java/org/apache/logging/log4j/catalog/api/Event.java
+++ 
b/log4j-catalog/log4j-catalog-api/src/main/java/org/apache/logging/log4j/catalog/api/Event.java
@@ -21,7 +21,6 @@ import java.util.List;
 import java.util.Set;
 
 import com.fasterxml.jackson.annotation.JsonFilter;
-import com.fasterxml.jackson.annotation.JsonProperty;
 
 import static 
org.apache.logging.log4j.catalog.api.constant.Constants.DEFAULT_CATALOG;
 

Reply via email to