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/07/21 06:48:18 UTC

[james-project] 02/02: JAMES-3613 IMAP should compute transport MDC upon connection

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 e65b36ac823b198150b60c777593a8ac7060b04a
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Wed Jul 14 17:15:32 2021 +0700

    JAMES-3613 IMAP should compute transport MDC upon connection
    
    Later interactions should be able to reuse the initial MDC.
    
    This, for instance, avoid computing the MDC for each following message, but also
    prevents triggering several DNS queries related to the same transport connection.
    
    We do save the initial MDC as a session attachment for later reuse.
    
    Note that subsequent message might update the state of the SMTP connection, eg
    by doing authentication thus we need to refresh the MDC context linked to the
    protocol session on each message.
---
 .../james/imapserver/netty/IMAPMDCContext.java     | 30 ++++++++--------------
 .../netty/ImapChannelUpstreamHandler.java          | 22 ++++++++++++----
 2 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/IMAPMDCContext.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/IMAPMDCContext.java
index 4349841..8e39371 100644
--- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/IMAPMDCContext.java
+++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/IMAPMDCContext.java
@@ -19,7 +19,6 @@
 
 package org.apache.james.imapserver.netty;
 
-import java.io.Closeable;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.util.Optional;
@@ -28,16 +27,14 @@ import org.apache.james.core.Username;
 import org.apache.james.imap.api.process.ImapSession;
 import org.apache.james.util.MDCBuilder;
 import org.jboss.netty.channel.ChannelHandlerContext;
-import org.jboss.netty.channel.ChannelLocal;
 
 public class IMAPMDCContext {
-    public static Closeable from(ChannelHandlerContext ctx, ChannelLocal<Object> attributes) {
+
+    public static MDCBuilder boundMDC(ChannelHandlerContext ctx) {
         return MDCBuilder.create()
-            .addToContext(from(attributes.get(ctx.getChannel())))
             .addToContext(MDCBuilder.PROTOCOL, "IMAP")
             .addToContext(MDCBuilder.IP, retrieveIp(ctx))
-            .addToContext(MDCBuilder.HOST, retrieveHost(ctx))
-            .build();
+            .addToContext(MDCBuilder.HOST, retrieveHost(ctx));
     }
 
     private static String retrieveIp(ChannelHandlerContext ctx) {
@@ -58,18 +55,13 @@ public class IMAPMDCContext {
         return remoteAddress.toString();
     }
 
-    private static MDCBuilder from(Object o) {
-        if (o instanceof ImapSession) {
-            ImapSession imapSession = (ImapSession) o;
-
-            return MDCBuilder.create()
-                .addToContext("sessionId", imapSession.sessionId().asString())
-                .addToContext(MDCBuilder.USER, Optional.ofNullable(imapSession.getUserName())
-                    .map(Username::asString)
-                    .orElse(""))
-                .addToContextIfPresent("selectedMailbox", Optional.ofNullable(imapSession.getSelected())
-                    .map(selectedMailbox -> selectedMailbox.getMailboxId().serialize()));
-        }
-        return MDCBuilder.create();
+    public static MDCBuilder from(ImapSession imapSession) {
+        return MDCBuilder.create()
+            .addToContext("sessionId", imapSession.sessionId().asString())
+            .addToContext(MDCBuilder.USER, Optional.ofNullable(imapSession.getUserName())
+                .map(Username::asString)
+                .orElse(""))
+            .addToContextIfPresent("selectedMailbox", Optional.ofNullable(imapSession.getSelected())
+                .map(selectedMailbox -> selectedMailbox.getMailboxId().serialize()));
     }
 }
diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
index d0b4e59..d1b50c5 100644
--- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
+++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
@@ -36,6 +36,7 @@ import org.apache.james.imap.encode.ImapResponseComposer;
 import org.apache.james.imap.encode.base.ImapResponseComposerImpl;
 import org.apache.james.imap.main.ResponseEncoder;
 import org.apache.james.metrics.api.Metric;
+import org.apache.james.util.MDCBuilder;
 import org.jboss.netty.buffer.ChannelBuffers;
 import org.jboss.netty.channel.Channel;
 import org.jboss.netty.channel.ChannelFutureListener;
@@ -54,6 +55,7 @@ import org.slf4j.LoggerFactory;
  */
 public class ImapChannelUpstreamHandler extends SimpleChannelUpstreamHandler implements NettyConstants {
     private static final Logger LOGGER = LoggerFactory.getLogger(ImapChannelUpstreamHandler.class);
+    public static final String MDC_KEY = "bound_MDC";
 
     private final String hello;
 
@@ -97,15 +99,25 @@ public class ImapChannelUpstreamHandler extends SimpleChannelUpstreamHandler imp
     public void channelBound(final ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
         ImapSession imapsession = new NettyImapSession(ctx.getChannel(), context, enabledCipherSuites, compress, plainAuthDisallowed,
             SessionId.generate());
+        MDCBuilder boundMDC = IMAPMDCContext.boundMDC(ctx);
+        imapsession.setAttribute(MDC_KEY, boundMDC);
         attributes.set(ctx.getChannel(), imapsession);
-        try (Closeable closeable = IMAPMDCContext.from(ctx, attributes)) {
+        try (Closeable closeable = boundMDC.build()) {
             super.channelBound(ctx, e);
         }
     }
 
+    private MDCBuilder mdc(ChannelHandlerContext ctx) {
+        ImapSession session = (ImapSession) attributes.get(ctx.getChannel());
+        MDCBuilder boundMDC = (MDCBuilder) session.getAttribute(MDC_KEY);
+
+        return IMAPMDCContext.from(session)
+            .addToContext(boundMDC);
+    }
+
     @Override
     public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
-        try (Closeable closeable = IMAPMDCContext.from(ctx, attributes)) {
+        try (Closeable closeable = mdc(ctx).build()) {
             InetSocketAddress address = (InetSocketAddress) ctx.getChannel().getRemoteAddress();
             LOGGER.info("Connection closed for {}", address.getAddress().getHostAddress());
 
@@ -123,7 +135,7 @@ public class ImapChannelUpstreamHandler extends SimpleChannelUpstreamHandler imp
 
     @Override
     public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
-        try (Closeable closeable = IMAPMDCContext.from(ctx, attributes)) {
+        try (Closeable closeable = mdc(ctx).build()) {
             InetSocketAddress address = (InetSocketAddress) ctx.getChannel().getRemoteAddress();
             LOGGER.info("Connection established from {}", address.getAddress().getHostAddress());
             imapConnectionsMetric.increment();
@@ -139,7 +151,7 @@ public class ImapChannelUpstreamHandler extends SimpleChannelUpstreamHandler imp
 
     @Override
     public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
-        try (Closeable closeable = IMAPMDCContext.from(ctx, attributes)) {
+        try (Closeable closeable = mdc(ctx).build()) {
             LOGGER.warn("Error while processing imap request", e.getCause());
 
             if (e.getCause() instanceof TooLongFrameException) {
@@ -181,7 +193,7 @@ public class ImapChannelUpstreamHandler extends SimpleChannelUpstreamHandler imp
 
     @Override
     public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
-        try (Closeable closeable = IMAPMDCContext.from(ctx, attributes)) {
+        try (Closeable closeable = mdc(ctx).build()) {
             imapCommandsMetric.increment();
             ImapSession session = (ImapSession) attributes.get(ctx.getChannel());
             ImapResponseComposer response = (ImapResponseComposer) ctx.getAttachment();

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