Closes #7
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 22ec90f..ee203d6 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
@@ -33,7 +33,7 @@
import java.util.Map;
import java.util.Set;
-import static org.apache.logging.log4j.catalog.api.constant.Constants.*;
+import static java.util.Collections.*;
/**
* This class is used to log events generated remotely.
@@ -106,10 +106,15 @@
AuditExceptionHandler exceptionHandler) {
AuditMessage msg = new AuditMessage(eventName, maxLength);
+ if (attributes == null) {
+ attributes = emptyMap();
+ }
+
StringBuilder missingAttributes = new StringBuilder();
StringBuilder errors = new StringBuilder();
- for (EventAttribute eventAttribute : event.getAttributes()) {
+ List<EventAttribute> eventAttributes = event.getAttributes() == null ? emptyList() : event.getAttributes();
+ for (EventAttribute eventAttribute : eventAttributes) {
Attribute attr = catalogManager.getAttribute(eventAttribute.getName(), event.getCatalogId());
if ((!attr.isRequestContext() && (attr.isRequired()) ||
(eventAttribute.isRequired() != null && eventAttribute.isRequired()))) {
diff --git a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java
index c14ca87..2362a50 100644
--- a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java
+++ b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java
@@ -20,10 +20,7 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -92,7 +89,7 @@
Class<?>[] interfaces = new Class<?>[] { intrface };
String eventId = NamingUtils.lowerFirst(intrface.getSimpleName());
- int msgLength = intrface.getAnnotation(MaxLength.class).value();
+ int msgLength = getMaxLength(intrface);
AuditMessage msg = new AuditMessage(eventId, msgLength);
AuditEvent audit = (AuditEvent) Proxy.newProxyInstance(intrface
.getClassLoader(), interfaces, new AuditProxy(msg, intrface));
@@ -100,6 +97,11 @@
return (T) audit;
}
+ private static <T> int getMaxLength(Class<T> intrface) {
+ MaxLength maxLength = intrface.getAnnotation(MaxLength.class);
+ return maxLength == null ? DEFAULT_MAX_LENGTH : maxLength.value();
+ }
+
/**
*
* This method is used to construct and AuditMessage from a set of properties and the Event interface
@@ -123,12 +125,16 @@
validateContextConstraints(intrface, errors);
String eventId = NamingUtils.lowerFirst(intrface.getSimpleName());
- int maxLength = intrface.getAnnotation(MaxLength.class).value();
+ int maxLength = getMaxLength(intrface);
AuditMessage msg = new AuditMessage(eventId, maxLength);
+
+ if (properties == null) {
+ properties = Collections.emptyMap();
+ }
List<Property> props = getProperties(intrface);
Map<String, Property> propertyMap = new HashMap<>();
- for (Property property : props ) {
+ for (Property property : props) {
propertyMap.put(property.name, property);
if (property.isRequired && !properties.containsKey(property.name)) {
if (errors.length() > 0) {
@@ -382,6 +388,11 @@
}
private static void validateContextConstraint(RequestContext constraint, StringBuilder errors) {
+ if (constraint == null) {
+ // the request context is not mandatory
+ return;
+ }
+
String value = ThreadContext.get(constraint.key());
if (value != null) {
validateConstraints(true, constraint.constraints(), constraint.key(), value, errors);
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 538d4ca..1cd9831 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
@@ -36,6 +36,7 @@
import org.apache.logging.log4j.catalog.api.CatalogReader;
import org.apache.logging.log4j.catalog.api.EventAttribute;
+import static java.util.Collections.emptyList;
import static org.apache.logging.log4j.catalog.api.constant.Constants.DEFAULT_CATALOG;
/**
@@ -83,11 +84,12 @@
public Map<String, Attribute> getAttributes(String eventName, String catalogId) {
Event event = getEvent(eventName, catalogId);
if (event == null) {
- logger.warn("No event named {} counld be found in catalog {}", eventName, catalogId);
+ logger.warn("The event named {} could not be found in catalog {}", eventName, catalogId);
return null;
}
- Map<String, Attribute> attributes = new HashMap<>(event.getAttributes().size());
- for (EventAttribute eventAttribute : event.getAttributes()) {
+ List<EventAttribute> eventAttributes = event.getAttributes() == null ? emptyList() : event.getAttributes();
+ Map<String, Attribute> attributes = new HashMap<>(eventAttributes.size());
+ for (EventAttribute eventAttribute : eventAttributes) {
Attribute attr = getAttribute(eventAttribute.getName(), event.getCatalogId());
if (attr != null) {
attributes.put(attr.getName(), attr);
@@ -128,22 +130,27 @@
}
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);
ObjectMapper mapper = new ObjectMapper(factory);
+
+ String catalog = catalogReader.readCatalog();
catalogData = mapper.readValue(catalog, CatalogData.class);
- for (Attribute attr : catalogData.getAttributes()) {
- if (attr.isRequestContext()) {
- requestContextAttributes.put(attr.getName(), attr);
+
+ if (catalogData.getAttributes() != null) {
+ for (Attribute attr : catalogData.getAttributes()) {
+ if (attr.isRequestContext()) {
+ requestContextAttributes.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, Attribute> attrMap = attributeMap.get(attr.getCatalogId());
- if (attrMap == null) {
- attrMap = new HashMap<>();
- attributeMap.put(attr.getCatalogId(), attrMap);
- }
- attrMap.put(attr.getName(), attr);
}
+
Map<String, Map<String, CatalogInfo>> map = new HashMap<>();
map.put(DEFAULT_CATALOG, new HashMap<>());
for (Event event : catalogData.getEvents()) {
diff --git a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/AuditLoggerTest.java b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/AuditLoggerTest.java
index 77ee464..3cfca75 100644
--- a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/AuditLoggerTest.java
+++ b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/AuditLoggerTest.java
@@ -23,6 +23,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.audit.catalog.StringCatalogReader;
import org.apache.logging.log4j.audit.exception.AuditException;
+import org.apache.logging.log4j.catalog.api.CatalogReader;
+import org.apache.logging.log4j.catalog.api.dao.ClassPathCatalogReader;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
@@ -31,6 +33,7 @@
import org.junit.BeforeClass;
import org.junit.Test;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -44,18 +47,15 @@
*/
public class AuditLoggerTest {
- private static AbstractEventLogger auditLogger;
-
- private static CatalogManager catalogManager;
-
+ private static CatalogReader catalogReader;
private static LoggerContext ctx;
private static ListAppender app;
+ private AbstractEventLogger auditLogger;
+
@BeforeClass
public static void setupClass() throws Exception {
- catalogManager = new CatalogManagerImpl(new StringCatalogReader());
- auditLogger = new AuditLogger();
- auditLogger.setCatalogManager(catalogManager);
+ catalogReader = new StringCatalogReader();
ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
for (Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
@@ -67,13 +67,22 @@
assertNotNull("No Appender", app);
}
+ private AbstractEventLogger buildAuditLogger(CatalogReader catalogReader) throws Exception {
+ CatalogManager catalogManager = new CatalogManagerImpl(catalogReader);
+ AuditLogger auditLogger = new AuditLogger();
+ auditLogger.setCatalogManager(catalogManager);
+ return auditLogger;
+ }
+
@Before
public void before() {
app.clear();
}
@Test
- public void testAuditLogger() {
+ public void testAuditLogger() throws Exception {
+ auditLogger = buildAuditLogger(catalogReader);
+
ThreadContext.put("companyId", "12345");
ThreadContext.put("ipAddress", "127.0.0.1");
ThreadContext.put("environment", "dev");
@@ -101,7 +110,9 @@
}
@Test(expected = AuditException.class)
- public void testBadAttribute() {
+ public void testBadAttribute() throws Exception {
+ auditLogger = buildAuditLogger(catalogReader);
+
ThreadContext.put("companyId", "12345");
ThreadContext.put("ipAddress", "127.0.0.1");
ThreadContext.put("environment", "dev");
@@ -113,4 +124,11 @@
properties.put("amount", "111.55");
auditLogger.logEvent("Transfer", properties);
}
+
+ @Test
+ public void testAuditLoggerWithBasicCatalog() throws Exception {
+ auditLogger = buildAuditLogger(new ClassPathCatalogReader(Collections.singletonMap("catalogFile", "basicCatalog.json")));
+
+ auditLogger.logEvent("login", null);
+ }
}
diff --git a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/BaseEventTest.java b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/BaseEventTest.java
new file mode 100644
index 0000000..01aca3e
--- /dev/null
+++ b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/BaseEventTest.java
@@ -0,0 +1,39 @@
+package org.apache.logging.log4j.audit;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.junit.Before;
+import org.junit.BeforeClass;
+
+import java.util.Map;
+
+import static org.junit.Assert.assertNotNull;
+
+public class BaseEventTest {
+
+ protected static LoggerContext ctx;
+ protected static ListAppender app;
+
+ @BeforeClass
+ public static void setupClass() throws Exception {
+ ctx = (LoggerContext) LogManager.getContext(false);
+ Configuration config = ctx.getConfiguration();
+ for (Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
+ if (entry.getKey().equals("List")) {
+ app = (ListAppender) entry.getValue();
+ break;
+ }
+ }
+ assertNotNull("No Appender", app);
+ }
+
+ @Before
+ public void before() {
+ app.clear();
+ ThreadContext.clearMap();
+ }
+}
\ No newline at end of file
diff --git a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/LoginTest.java b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/LoginTest.java
new file mode 100644
index 0000000..190ccd6
--- /dev/null
+++ b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/LoginTest.java
@@ -0,0 +1,34 @@
+package org.apache.logging.log4j.audit;
+
+import org.apache.logging.log4j.audit.event.Login;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class LoginTest extends BaseEventTest {
+ @Test
+ public void testAuditClass() {
+ Login event = LogEventFactory.getEvent(Login.class);
+
+ event.logEvent();
+
+ event.setCompletionStatus("Success");
+
+ event.logEvent();
+
+ List<String> msgs = app.getMessages();
+ assertNotNull("No messages", msgs);
+ assertTrue("No messages", msgs.size() == 2);
+ }
+
+ @Test
+ public void testAuditLog() {
+ LogEventFactory.logEvent(Login.class, null);
+
+ LogEventFactory.logEvent(Login.class, Collections.emptyMap());
+ }
+}
\ No newline at end of file
diff --git a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/TransferTest.java b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/TransferTest.java
index 7c5ce5e..716bb58 100644
--- a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/TransferTest.java
+++ b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/TransferTest.java
@@ -40,29 +40,7 @@
/**
*
*/
-public class TransferTest {
-
- private static LoggerContext ctx;
- private static ListAppender app;
-
- @BeforeClass
- public static void setupClass() throws Exception {
- ctx = (LoggerContext) LogManager.getContext(false);
- Configuration config = ctx.getConfiguration();
- for (Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
- if (entry.getKey().equals("List")) {
- app = (ListAppender) entry.getValue();
- break;
- }
- }
- assertNotNull("No Appender", app);
- }
-
- @Before
- public void before() {
- app.clear();
- ThreadContext.clearMap();
- }
+public class TransferTest extends BaseEventTest {
@Test(expected = ConstraintValidationException.class)
public void testValidationFailure() {
diff --git a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/event/Login.java b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/event/Login.java
new file mode 100644
index 0000000..8a641f4
--- /dev/null
+++ b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/event/Login.java
@@ -0,0 +1,6 @@
+package org.apache.logging.log4j.audit.event;
+
+import org.apache.logging.log4j.audit.AuditEvent;
+
+public interface Login extends AuditEvent {
+}
diff --git a/log4j-audit/log4j-audit-api/src/test/resources/basicCatalog.json b/log4j-audit/log4j-audit-api/src/test/resources/basicCatalog.json
new file mode 100644
index 0000000..297a36b
--- /dev/null
+++ b/log4j-audit/log4j-audit-api/src/test/resources/basicCatalog.json
@@ -0,0 +1,5 @@
+{
+ "events" : [ {
+ "name" : "login"
+ } ]
+}
\ No newline at end of file