You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2021/02/25 09:06:50 UTC

[james-project] 02/12: JAMES-3504 MDC context for POP3 commands

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 69d7f22b9965be146b61633f0f24745a2365b812
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Tue Feb 23 11:53:56 2021 +0700

    JAMES-3504 MDC context for POP3 commands
    
    Includes:
     - Request arguments (except for auth)
     - Session context (state, mailbox)
     - Action performed
---
 protocols/pop3/pom.xml                             |   4 +
 .../pop3/core/AbstractPassCmdHandler.java          |  12 ++-
 .../james/protocols/pop3/core/CapaCmdHandler.java  |   8 ++
 .../james/protocols/pop3/core/DeleCmdHandler.java  |   9 ++
 .../james/protocols/pop3/core/ListCmdHandler.java  |  13 ++-
 .../{NoopCmdHandler.java => MDCConstants.java}     | 111 ++++++++++-----------
 .../james/protocols/pop3/core/NoopCmdHandler.java  |   9 ++
 .../james/protocols/pop3/core/QuitCmdHandler.java  |   9 ++
 .../james/protocols/pop3/core/RetrCmdHandler.java  |  10 ++
 .../james/protocols/pop3/core/RsetCmdHandler.java  |  11 +-
 .../james/protocols/pop3/core/StatCmdHandler.java  |   9 ++
 .../james/protocols/pop3/core/StlsCmdHandler.java  |   9 ++
 .../james/protocols/pop3/core/TopCmdHandler.java   |  15 ++-
 .../james/protocols/pop3/core/UidlCmdHandler.java  |  14 ++-
 .../protocols/pop3/core/UnknownCmdHandler.java     |  11 ++
 .../james/protocols/pop3/core/UserCmdHandler.java  |  10 ++
 .../james/pop3server/core/PassCmdHandler.java      |  12 ++-
 17 files changed, 207 insertions(+), 69 deletions(-)

diff --git a/protocols/pop3/pom.xml b/protocols/pop3/pom.xml
index 47fc277..f77786e 100644
--- a/protocols/pop3/pom.xml
+++ b/protocols/pop3/pom.xml
@@ -35,6 +35,10 @@
     <dependencies>
         <dependency>
             <groupId>${james.groupId}</groupId>
+            <artifactId>james-server-util</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
             <artifactId>testing-base</artifactId>
             <scope>test</scope>
         </dependency>
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java
index 9358025..cd6bd61 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java
@@ -28,6 +28,7 @@ import org.apache.james.protocols.api.Response;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.mailbox.Mailbox;
+import org.apache.james.util.MDCBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,6 +49,14 @@ public abstract class AbstractPassCmdHandler extends RsetCmdHandler {
      */
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "AUTH")
+                .addContext(MDCConstants.withSession(session)),
+            () -> doAuth(session, request));
+    }
+
+    private Response doAuth(POP3Session session, Request request) {
         String parameters = request.getArgument();
         if (session.getHandlerState() == POP3Session.AUTHENTICATION_USERSET && parameters != null) {
             return doAuth(session, session.getUsername(), parameters);
@@ -56,8 +65,7 @@ public abstract class AbstractPassCmdHandler extends RsetCmdHandler {
             return AUTH_FAILED;
         }
     }
-    
-    
+
     /**
      * Authenticate a user and return the {@link Response}
      */
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/CapaCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/CapaCmdHandler.java
index fe6a335..b902f6e 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/CapaCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/CapaCmdHandler.java
@@ -31,6 +31,7 @@ import org.apache.james.protocols.api.handler.ExtensibleHandler;
 import org.apache.james.protocols.api.handler.WiringException;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -44,6 +45,13 @@ public class CapaCmdHandler implements CommandHandler<POP3Session>, ExtensibleHa
 
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "CAPA")
+                .addContext(MDCConstants.withSession(session)),
+            () -> capa(session));
+    }
+
+    private Response capa(POP3Session session) {
         POP3Response response = new POP3Response(POP3Response.OK_RESPONSE, "Capability list follows");
 
         for (CapaCapability capabilities : caps) {
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/DeleCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/DeleCmdHandler.java
index ce499d7..d32624a 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/DeleCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/DeleCmdHandler.java
@@ -30,6 +30,7 @@ import org.apache.james.protocols.api.handler.CommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.mailbox.MessageMetaData;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -49,6 +50,14 @@ public class DeleCmdHandler implements CommandHandler<POP3Session> {
     @Override
     @SuppressWarnings("unchecked")
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "DELE")
+                .addContext(MDCConstants.withSession(session))
+                .addContext(MDCConstants.forRequest(request)),
+            () -> delete(session, request));
+    }
+
+    private Response delete(POP3Session session, Request request) {
         if (session.getHandlerState() == POP3Session.TRANSACTION) {
             int num = 0;
             try {
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/ListCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/ListCmdHandler.java
index b706146..4c16b54 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/ListCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/ListCmdHandler.java
@@ -30,6 +30,7 @@ import org.apache.james.protocols.api.handler.CommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.mailbox.MessageMetaData;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
@@ -55,6 +56,14 @@ public class ListCmdHandler implements CommandHandler<POP3Session> {
     @Override
     @SuppressWarnings("unchecked")
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "LIST")
+                .addContext(MDCConstants.withSession(session))
+                .addContext(MDCConstants.forRequest(request)),
+            () -> list(session, request));
+    }
+
+    private Response list(POP3Session session, Request request) {
         String parameters = request.getArgument();
         List<MessageMetaData> uidList = session.getAttachment(POP3Session.UID_LIST, State.Transaction).orElse(ImmutableList.of());
         List<String> deletedUidList = session.getAttachment(POP3Session.DELETED_UID_LIST, State.Transaction).orElse(ImmutableList.of());
@@ -89,13 +98,13 @@ public class ListCmdHandler implements CommandHandler<POP3Session> {
                 int num = 0;
                 try {
                     num = Integer.parseInt(parameters);
-                    
+
                     MessageMetaData data = MessageMetaDataUtils.getMetaData(session, num);
                     if (data == null) {
                         StringBuilder responseBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
                         return  new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
                     }
-                    
+
                     if (!deletedUidList.contains(data.getUid())) {
                         StringBuilder responseBuffer = new StringBuilder(64).append(num).append(" ").append(data.getSize());
                         response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/MDCConstants.java
similarity index 56%
copy from protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java
copy to protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/MDCConstants.java
index 11012c6..de4f9af 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/MDCConstants.java
@@ -1,57 +1,54 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one   *
- * or more contributor license agreements.  See the NOTICE file *
- * distributed with this work for additional information        *
- * regarding copyright ownership.  The ASF licenses this file   *
- * to you under the Apache License, Version 2.0 (the            *
- * "License"); you may not use this file except in compliance   *
- * with the License.  You may obtain a copy of the License at   *
- *                                                              *
- *   http://www.apache.org/licenses/LICENSE-2.0                 *
- *                                                              *
- * Unless required by applicable law or agreed to in writing,   *
- * software distributed under the License is distributed on an  *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
- * KIND, either express or implied.  See the License for the    *
- * specific language governing permissions and limitations      *
- * under the License.                                           *
- ****************************************************************/
-
-package org.apache.james.protocols.pop3.core;
-
-import java.util.Collection;
-
-import org.apache.james.protocols.api.Request;
-import org.apache.james.protocols.api.Response;
-import org.apache.james.protocols.api.handler.CommandHandler;
-import org.apache.james.protocols.pop3.POP3Response;
-import org.apache.james.protocols.pop3.POP3Session;
-
-import com.google.common.collect.ImmutableSet;
-
-/**
- * Handles NOOP command
- */
-public class NoopCmdHandler implements CommandHandler<POP3Session> {
-
-    private static final Collection<String> COMMANDS = ImmutableSet.of("NOOP");
-
-    /**
-     * Handler method called upon receipt of a NOOP command. Like all good
-     * NOOPs, does nothing much.
-     */
-    @Override
-    public Response onCommand(POP3Session session, Request request) {
-        if (session.getHandlerState() == POP3Session.TRANSACTION) {
-            return POP3Response.OK;
-        } else {
-            return POP3Response.ERR;
-        }
-    }
-
-    @Override
-    public Collection<String> getImplCommands() {
-        return COMMANDS;
-    }
-
-}
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.protocols.pop3.core;
+
+import java.util.Optional;
+
+import org.apache.james.protocols.api.Request;
+import org.apache.james.protocols.pop3.POP3Session;
+import org.apache.james.protocols.pop3.mailbox.Mailbox;
+import org.apache.james.util.MDCBuilder;
+
+import com.github.fge.lambdas.Throwing;
+
+public interface MDCConstants {
+    String MAILBOX = "mailbox";
+    String ARGUMENT = "argument";
+    String STATE = "state";
+
+    static MDCBuilder withMailbox(POP3Session session) {
+        return Optional.ofNullable(session.getUserMailbox())
+            .map(Throwing.function(Mailbox::getIdentifier).sneakyThrow())
+            .map(id -> MDCBuilder.create().addContext(MAILBOX, id))
+            .orElse(MDCBuilder.create());
+    }
+
+    static MDCBuilder forRequest(Request request) {
+        return Optional.ofNullable(request.getArgument())
+            .map(argument -> MDCBuilder.create().addContext(ARGUMENT, argument))
+            .orElse(MDCBuilder.create());
+    }
+
+    static MDCBuilder withSession(POP3Session session) {
+        return MDCBuilder.create()
+            .addContext(withMailbox(session))
+            .addContext(STATE, session.getHandlerState());
+    }
+}
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java
index 11012c6..747797f 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java
@@ -26,6 +26,7 @@ import org.apache.james.protocols.api.Response;
 import org.apache.james.protocols.api.handler.CommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -42,6 +43,14 @@ public class NoopCmdHandler implements CommandHandler<POP3Session> {
      */
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "NOOP")
+                .addContext(MDCConstants.withSession(session)),
+            () -> noop(session));
+    }
+
+    private Response noop(POP3Session session) {
         if (session.getHandlerState() == POP3Session.TRANSACTION) {
             return POP3Response.OK;
         } else {
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/QuitCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/QuitCmdHandler.java
index 345dd0b..b213ac8 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/QuitCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/QuitCmdHandler.java
@@ -30,6 +30,7 @@ import org.apache.james.protocols.api.handler.CommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.mailbox.Mailbox;
+import org.apache.james.util.MDCBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -61,6 +62,14 @@ public class QuitCmdHandler implements CommandHandler<POP3Session> {
      */
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "QUIT")
+                .addContext(MDCConstants.withSession(session)),
+            () -> quit(session));
+    }
+
+    private Response quit(POP3Session session) {
         Response response = null;
         if (session.getHandlerState() == POP3Session.AUTHENTICATION_READY || session.getHandlerState() == POP3Session.AUTHENTICATION_USERSET) {
             return SIGN_OFF;
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java
index 99d050c..0167317 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java
@@ -32,6 +32,7 @@ import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.POP3StreamResponse;
 import org.apache.james.protocols.pop3.mailbox.MessageMetaData;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
@@ -54,6 +55,15 @@ public class RetrCmdHandler implements CommandHandler<POP3Session> {
     @Override
     @SuppressWarnings("unchecked")
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "RETR")
+                .addContext(MDCConstants.withSession(session))
+                .addContext(MDCConstants.forRequest(request)),
+            () -> retr(session, request));
+    }
+
+    private Response retr(POP3Session session, Request request) {
         POP3Response response = null;
         String parameters = request.getArgument();
         if (session.getHandlerState() == POP3Session.TRANSACTION) {
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RsetCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RsetCmdHandler.java
index f88ec0b..87f2aa1 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RsetCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RsetCmdHandler.java
@@ -31,6 +31,7 @@ import org.apache.james.protocols.api.handler.CommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.mailbox.MessageMetaData;
+import org.apache.james.util.MDCBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -49,13 +50,21 @@ public class RsetCmdHandler implements CommandHandler<POP3Session> {
      */
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "RSET")
+                .addContext(MDCConstants.withSession(session)),
+            () -> rset(session));
+
+    }
+
+    private Response rset(POP3Session session) {
         if (session.getHandlerState() == POP3Session.TRANSACTION) {
             stat(session);
             return POP3Response.OK;
         } else {
             return POP3Response.ERR;
         }
-        
     }
 
     /**
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StatCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StatCmdHandler.java
index f942124..a2fbc74 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StatCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StatCmdHandler.java
@@ -30,6 +30,7 @@ import org.apache.james.protocols.api.handler.CommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.mailbox.MessageMetaData;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
@@ -46,6 +47,14 @@ public class StatCmdHandler implements CommandHandler<POP3Session> {
      */
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "STAT")
+                .addContext(MDCConstants.withSession(session)),
+            () -> stat(session));
+    }
+
+    private Response stat(POP3Session session) {
         if (session.getHandlerState() == POP3Session.TRANSACTION) {
 
             List<MessageMetaData> uidList = session.getAttachment(POP3Session.UID_LIST, State.Transaction).orElse(ImmutableList.of());
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StlsCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StlsCmdHandler.java
index 28c03180..d84f853 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StlsCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StlsCmdHandler.java
@@ -29,6 +29,7 @@ import org.apache.james.protocols.api.handler.CommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.POP3StartTlsResponse;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -45,6 +46,14 @@ public class StlsCmdHandler implements CommandHandler<POP3Session>, CapaCapabili
 
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "START_TLS")
+                .addContext(MDCConstants.withSession(session)),
+            () -> stls(session));
+    }
+
+    private Response stls(POP3Session session) {
         // check if starttls is supported, the state is the right one and it was
         // not started before
         if (session.isStartTLSSupported() && session.getHandlerState() == POP3Session.AUTHENTICATION_READY && session.isTLSStarted() == false) {
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java
index 5931a2b..7ffb13f 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java
@@ -34,6 +34,7 @@ import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.POP3StreamResponse;
 import org.apache.james.protocols.pop3.mailbox.MessageMetaData;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
@@ -58,6 +59,15 @@ public class TopCmdHandler extends RetrCmdHandler implements CapaCapability {
     @SuppressWarnings("unchecked")
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "TOP")
+                .addContext(MDCConstants.withSession(session))
+                .addContext(MDCConstants.forRequest(request)),
+            () -> top(session, request));
+    }
+
+    private Response top(POP3Session session, Request request) {
         String parameters = request.getArgument();
         if (parameters == null) {
             return SYNTAX_ERROR;
@@ -81,13 +91,13 @@ public class TopCmdHandler extends RetrCmdHandler implements CapaCapability {
                 return SYNTAX_ERROR;
             }
             try {
-                
+
                 MessageMetaData data = MessageMetaDataUtils.getMetaData(session, num);
                 if (data == null) {
                     StringBuilder responseBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist.");
                     return  new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString());
                 }
-                
+
                 List<String> deletedUidList = session.getAttachment(POP3Session.DELETED_UID_LIST, State.Transaction).orElse(ImmutableList.of());
 
                 String uid = data.getUid();
@@ -109,7 +119,6 @@ public class TopCmdHandler extends RetrCmdHandler implements CapaCapability {
         } else {
             return POP3Response.ERR;
         }
-
     }
 
     @Override
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UidlCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UidlCmdHandler.java
index 4155a65..161934d 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UidlCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UidlCmdHandler.java
@@ -32,6 +32,7 @@ import org.apache.james.protocols.api.handler.CommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.mailbox.MessageMetaData;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
@@ -49,6 +50,15 @@ public class UidlCmdHandler implements CommandHandler<POP3Session>, CapaCapabili
      */
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "UIDL")
+                .addContext(MDCConstants.withSession(session))
+                .addContext(MDCConstants.forRequest(request)),
+            () -> uidl(session, request));
+    }
+
+    private Response uidl(POP3Session session, Request request) {
         POP3Response response = null;
         String parameters = request.getArgument();
         if (session.getHandlerState() == POP3Session.TRANSACTION) {
@@ -72,7 +82,7 @@ public class UidlCmdHandler implements CommandHandler<POP3Session>, CapaCapabili
                     int num = 0;
                     try {
                         num = Integer.parseInt(parameters);
-                        
+
                         MessageMetaData metadata = MessageMetaDataUtils.getMetaData(session, num);
 
                         if (metadata == null) {
@@ -98,7 +108,7 @@ public class UidlCmdHandler implements CommandHandler<POP3Session>, CapaCapabili
             } catch (IOException e) {
                 return POP3Response.ERR;
             }
-            
+
         } else {
             return POP3Response.ERR;
         }
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UnknownCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UnknownCmdHandler.java
index 6193a31..7ec03f4 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UnknownCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UnknownCmdHandler.java
@@ -24,17 +24,28 @@ import org.apache.james.protocols.api.Response;
 import org.apache.james.protocols.api.handler.UnknownCommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
+import org.apache.james.util.MDCBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Default command handler for handling unknown commands
  */
 public class UnknownCmdHandler extends UnknownCommandHandler<POP3Session> {
+    private static final Logger LOGGER = LoggerFactory.getLogger(UnknownCmdHandler.class);
+
     /**
      * Handler method called upon receipt of an unrecognized command. Returns an
      * error response and logs the command.
      */
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, request.getCommand())
+                .addContext(MDCConstants.withSession(session))
+                .addContext(MDCConstants.forRequest(request)),
+            () -> LOGGER.info("Unknown command received"));
         return POP3Response.ERR;
     }
 }
diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UserCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UserCmdHandler.java
index c4ded58..fe6e79a 100644
--- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UserCmdHandler.java
+++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UserCmdHandler.java
@@ -28,6 +28,7 @@ import org.apache.james.protocols.api.Response;
 import org.apache.james.protocols.api.handler.CommandHandler;
 import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
+import org.apache.james.util.MDCBuilder;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -45,6 +46,15 @@ public class UserCmdHandler implements CommandHandler<POP3Session>, CapaCapabili
      */
     @Override
     public Response onCommand(POP3Session session, Request request) {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.ACTION, "USER")
+                .addContext(MDCConstants.withSession(session))
+                .addContext(MDCConstants.forRequest(request)),
+            () -> user(session, request));
+    }
+
+    private Response user(POP3Session session, Request request) {
         String parameters = request.getArgument();
         if (session.getHandlerState() == POP3Session.AUTHENTICATION_READY && parameters != null) {
             session.setUsername(Username.of(parameters));
diff --git a/server/protocols/protocols-pop3/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java b/server/protocols/protocols-pop3/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java
index 47a1945..eb3f944 100644
--- a/server/protocols/protocols-pop3/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java
+++ b/server/protocols/protocols-pop3/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java
@@ -40,9 +40,12 @@ import org.apache.james.protocols.pop3.POP3Response;
 import org.apache.james.protocols.pop3.POP3Session;
 import org.apache.james.protocols.pop3.core.AbstractPassCmdHandler;
 import org.apache.james.protocols.pop3.mailbox.Mailbox;
+import org.apache.james.util.MDCBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.github.fge.lambdas.Throwing;
+
 import reactor.core.publisher.Mono;
 
 /**
@@ -71,6 +74,13 @@ public class PassCmdHandler extends AbstractPassCmdHandler  {
 
     @Override
     protected Mailbox auth(POP3Session session, Username username, String password) throws Exception {
+        return MDCBuilder.withMdc(
+            MDCBuilder.create()
+                .addContext(MDCBuilder.USER, username.asString()),
+            Throwing.supplier(() -> auth(session, password)).sneakyThrow());
+    }
+
+    private Mailbox auth(POP3Session session, String password) throws IOException {
         MailboxSession mSession = null;
         try {
             mSession = manager.login(session.getUsername(), password);
@@ -93,7 +103,5 @@ public class PassCmdHandler extends AbstractPassCmdHandler  {
                 manager.endProcessingRequest(mSession);
             }
         }
-
     }
-
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org