From fcfeaa126e75c11cfb963f02c6f66a53bbca3109 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Thu, 26 Nov 2015 16:46:33 +0000
Subject: [PATCH] OPENDJ-2465 Add support for transactionId in current DJ access and HTTP access loggers

---
 opendj-server-legacy/src/main/java/org/opends/server/loggers/TextAccessLogPublisher.java |   33 +++++++++++++++++++++++++++++++++
 1 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/loggers/TextAccessLogPublisher.java b/opendj-server-legacy/src/main/java/org/opends/server/loggers/TextAccessLogPublisher.java
index 054f21d..c03cd86 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/loggers/TextAccessLogPublisher.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/loggers/TextAccessLogPublisher.java
@@ -26,6 +26,7 @@
  */
 package org.opends.server.loggers;
 
+import static org.opends.messages.LoggerMessages.ERR_COMMON_AUDIT_INVALID_TRANSACTION_ID;
 import static org.opends.messages.ConfigMessages.*;
 import static org.opends.server.util.StaticUtils.*;
 
@@ -47,8 +48,10 @@
 import org.opends.server.admin.std.server.FileBasedAccessLogPublisherCfg;
 import org.opends.server.api.ClientConnection;
 import org.opends.server.api.ExtendedOperationHandler;
+import org.opends.server.controls.TransactionIdControl;
 import org.opends.server.core.*;
 import org.opends.server.types.*;
+import org.opends.server.util.StaticUtils;
 import org.opends.server.util.TimeThread;
 
 /** This class provides the implementation of the access logger used by the directory server. */
@@ -87,6 +90,7 @@
   private boolean isCombinedMode;
   private boolean includeControlOIDs;
   private String timeStampFormat = "dd/MMM/yyyy:HH:mm:ss Z";
+  private ServerContext serverContext;
 
   @Override
   public ConfigChangeResult applyConfigurationChange(FileBasedAccessLogPublisherCfg config)
@@ -204,6 +208,7 @@
   public void initializeLogPublisher(final FileBasedAccessLogPublisherCfg cfg, ServerContext serverContext)
       throws ConfigException, InitializationException
   {
+    this.serverContext = serverContext;
     final File logFile = getLogFile(cfg);
     final FileNamingPolicy fnPolicy = new TimeStampNaming(logFile);
 
@@ -916,6 +921,7 @@
     buffer.append(" conn=").append(operation.getConnectionID());
     buffer.append(" op=").append(operation.getOperationID());
     buffer.append(" msgID=").append(operation.getMessageID());
+    appendTransactionId(operation, buffer);
   }
 
   private void appendModifyDNRequest(final ModifyDNOperation modifyDNOperation,
@@ -944,6 +950,33 @@
     }
   }
 
+  private void appendTransactionId(Operation operation, final StringBuilder buffer)
+  {
+    // In test context, serverContext may be null
+    if (serverContext != null && serverContext.getCommonAudit().shouldTrustTransactionIds())
+    {
+      String transactionId = getTransactionIdFromControl(operation);
+      if (transactionId != null)
+      {
+        buffer.append(" transactionId=").append(transactionId);
+      }
+    }
+  }
+
+  private String getTransactionIdFromControl(Operation operation)
+  {
+    try
+    {
+      TransactionIdControl control = operation.getRequestControl(TransactionIdControl.DECODER);
+      return control != null ? control.getTransactionId() : null;
+    }
+    catch (DirectoryException e)
+    {
+      logger.error(ERR_COMMON_AUDIT_INVALID_TRANSACTION_ID.get(StaticUtils.stackTraceToSingleLineString(e)));
+    }
+    return null;
+  }
+
   private void appendRequestControls(final Operation operation, final StringBuilder buffer)
   {
     appendControls(buffer, " requestControls=", operation.getRequestControls());

--
Gitblit v1.10.0