in log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java [102:220]
private void logEvent(String eventName, Map<String, String> attributes, Event event,
AuditExceptionHandler exceptionHandler) {
AuditMessage msg = new AuditMessage(eventName, maxLength);
if (attributes == null) {
attributes = emptyMap();
}
StringBuilder missingAttributes = new StringBuilder();
StringBuilder errors = new StringBuilder();
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()))) {
String name = attr.getName();
if (!attributes.containsKey(name)) {
if (missingAttributes.length() > 0) {
missingAttributes.append(", ");
}
missingAttributes.append(name);
} else {
if (attr.getConstraints() != null && attr.getConstraints().size() > 0) {
validateConstraints(false, attr.getConstraints(), name, attributes.get(name), errors);
}
}
}
}
Map<String, Attribute> attributeMap = catalogManager.getAttributes(eventName, event.getCatalogId());
for (String name : attributes.keySet()) {
if (!attributeMap.containsKey(name) && !name.equals("completionStatus")) {
if (errors.length() > 0) {
errors.append("\n");
}
errors.append("Attribute ").append(name).append(" is not defined for ").append(eventName);
}
}
if (missingAttributes.length() > 0) {
if (errors.length() > 0) {
errors.append("\n");
}
errors.append("Event ").append(eventName).append(" is missing required attribute(s) ").append(missingAttributes.toString());
}
if (errors.length() > 0) {
throw new ConstraintValidationException(errors.toString());
}
List<String> attributeNames = catalogManager.getAttributeNames(eventName, event.getCatalogId());
StringBuilder buf = new StringBuilder();
for (String attribute : attributes.keySet()) {
if (!attributeNames.contains(attribute)) {
if (buf.length() > 0) {
buf.append(", ");
}
buf.append(attribute);
}
}
if (buf.length() > 0) {
throw new ConstraintValidationException("Event " + eventName + " contains invalid attribute(s) " + buf.toString());
}
List<String> reqCtxAttrs = catalogManager.getRequiredContextAttributes(eventName, event.getCatalogId());
if (reqCtxAttrs != null && !reqCtxAttrs.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String attr : reqCtxAttrs) {
if (!ThreadContext.containsKey(attr)) {
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(attr);
}
}
if (sb.length() > 0) {
throw new ConstraintValidationException("Event " + msg.getId().getName() +
" is missing required RequestContextMapping values for " + sb.toString());
}
}
Map<String, Attribute> reqCtxAttributes = catalogManager.getRequestContextAttributes();
for (Map.Entry<String, Attribute> entry : reqCtxAttributes.entrySet()) {
Attribute attribute = entry.getValue();
String attr = entry.getKey();
if (attribute.isRequired() && !ThreadContext.containsKey(attr)) {
if (errors.length() > 0) {
errors.append(", ");
}
errors.append(attr);
}
}
if (errors.length() > 0) {
throw new ConstraintValidationException("Event " + eventName +
" is missing required Thread Context values for " + errors.toString());
}
for (Map.Entry<String, Attribute> entry : reqCtxAttributes.entrySet()) {
Attribute attribute = reqCtxAttributes.get(entry.getKey());
if (!ThreadContext.containsKey(entry.getKey())) {
continue;
}
Set<Constraint> constraintList = attribute.getConstraints();
if (constraintList != null && constraintList.size() > 0) {
validateConstraints(true, constraintList, entry.getKey(), ThreadContext.get(entry.getKey()), errors);
}
}
if (errors.length() > 0) {
throw new ConstraintValidationException("Event " + eventName + " has incorrect data in the Thread Context: " + errors.toString());
}
msg.putAll(attributes);
try {
logEvent(msg);
} catch (Throwable ex) {
if (exceptionHandler == null) {
defaultAuditExceptionHandler.handleException(msg, ex);
} else {
exceptionHandler.handleException(msg, ex);
}
}
}