This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 0810b62757648a98ece211550ea0fc9d4cf4828b Author: Benoit Tellier <[email protected]> AuthorDate: Thu May 28 11:44:05 2020 +0700 JAMES-3196 Add an IMAP SessionId to correlate logs --- .../apache/james/imap/api/process/ImapSession.java | 50 ++++++++++++++++++++++ .../apache/james/imap/encode/FakeImapSession.java | 7 +++ .../james/imapserver/netty/IMAPMDCContext.java | 1 + .../netty/ImapChannelUpstreamHandler.java | 6 ++- .../james/imapserver/netty/NettyImapSession.java | 9 +++- 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/protocols/imap/src/main/java/org/apache/james/imap/api/process/ImapSession.java b/protocols/imap/src/main/java/org/apache/james/imap/api/process/ImapSession.java index e8769f9..57ec639 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/api/process/ImapSession.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/api/process/ImapSession.java @@ -19,8 +19,10 @@ package org.apache.james.imap.api.process; +import java.util.Objects; import java.util.Optional; +import org.apache.commons.text.RandomStringGenerator; import org.apache.james.core.Username; import org.apache.james.imap.api.ImapSessionState; import org.apache.james.mailbox.MailboxSession; @@ -33,9 +35,57 @@ import org.apache.james.mailbox.MailboxSession; * @version $Revision: 109034 $ */ public interface ImapSession { + class SessionId { + private static final RandomStringGenerator RANDOM_STRING_GENERATOR = new RandomStringGenerator.Builder() + .withinRange('a', 'z') + .build(); + private static final int LENGTH = 12; + + public static SessionId generate() { + return new SessionId("SID-" + RANDOM_STRING_GENERATOR.generate(LENGTH)); + } + + private final String value; + + private SessionId(String value) { + this.value = value; + } + + public String asString() { + return value; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof SessionId) { + SessionId sessionId = (SessionId) o; + + return Objects.equals(this.value, sessionId.value); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(value); + } + + @Override + public String toString() { + return asString(); + } + } + String MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY = "org.apache.james.api.imap.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY"; /** + * @return a unique identifier for this session. + * + * One of its usage is log correlation. + */ + SessionId sessionId(); + + /** * Logs out the session. Marks the connection for closure; */ void logout(); diff --git a/protocols/imap/src/main/java/org/apache/james/imap/encode/FakeImapSession.java b/protocols/imap/src/main/java/org/apache/james/imap/encode/FakeImapSession.java index bd7d053..e2c375e 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/encode/FakeImapSession.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/encode/FakeImapSession.java @@ -33,12 +33,19 @@ public class FakeImapSession implements ImapSession { private SelectedMailbox selectedMailbox = null; private final Map<String, Object> attributesByKey; + private final SessionId sessionId; public FakeImapSession() { + this.sessionId = SessionId.generate(); this.attributesByKey = new ConcurrentHashMap<>(); } @Override + public SessionId sessionId() { + return sessionId; + } + + @Override public void logout() { closeMailbox(); state = ImapSessionState.LOGOUT; 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 56042a7..fe2c26a 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 @@ -64,6 +64,7 @@ public class IMAPMDCContext { ImapSession imapSession = (ImapSession) o; return MDCBuilder.create() + .addContext("sessionId", imapSession.sessionId().asString()) .addContext(MDCBuilder.USER, Optional.ofNullable(imapSession.getUserName()) .map(Username::asString)) .addContext(from(Optional.ofNullable(imapSession.getSelected()))); 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 8f475c9..d1f699d 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 @@ -29,6 +29,7 @@ import org.apache.james.imap.api.ImapMessage; import org.apache.james.imap.api.ImapSessionState; import org.apache.james.imap.api.process.ImapProcessor; import org.apache.james.imap.api.process.ImapSession; +import org.apache.james.imap.api.process.ImapSession.SessionId; import org.apache.james.imap.encode.ImapEncoder; import org.apache.james.imap.encode.ImapResponseComposer; import org.apache.james.imap.encode.base.ImapResponseComposerImpl; @@ -93,9 +94,10 @@ public class ImapChannelUpstreamHandler extends SimpleChannelUpstreamHandler imp @Override public void channelBound(final ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { + ImapSession imapsession = new NettyImapSession(ctx.getChannel(), context, enabledCipherSuites, compress, plainAuthDisallowed, + SessionId.generate()); + attributes.set(ctx.getChannel(), imapsession); try (Closeable closeable = IMAPMDCContext.from(ctx, attributes)) { - ImapSession imapsession = new NettyImapSession(ctx.getChannel(), context, enabledCipherSuites, compress, plainAuthDisallowed); - attributes.set(ctx.getChannel(), imapsession); super.channelBound(ctx, e); } } diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java index 18b74bf..ea5d670 100644 --- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java +++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java @@ -43,13 +43,20 @@ public class NettyImapSession implements ImapSession, NettyConstants { private final Channel channel; private int handlerCount; private final boolean plainAuthDisallowed; + private final SessionId sessionId; - public NettyImapSession(Channel channel, SSLContext sslContext, String[] enabledCipherSuites, boolean compress, boolean plainAuthDisallowed) { + public NettyImapSession(Channel channel, SSLContext sslContext, String[] enabledCipherSuites, boolean compress, boolean plainAuthDisallowed, SessionId sessionId) { this.channel = channel; this.sslContext = sslContext; this.enabledCipherSuites = enabledCipherSuites; this.compress = compress; this.plainAuthDisallowed = plainAuthDisallowed; + this.sessionId = sessionId; + } + + @Override + public SessionId sessionId() { + return sessionId; } @Override --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
