You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/11/30 02:39:17 UTC

[1/9] james-project git commit: JAMES-2229 Create command will valid mailbox name when only contain delimiter

Repository: james-project
Updated Branches:
  refs/heads/master a546da0ed -> 4135794a1


JAMES-2229 Create command will valid mailbox name when only contain delimiter


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/fd69a44a
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/fd69a44a
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/fd69a44a

Branch: refs/heads/master
Commit: fd69a44a415228798db969a926c6c4f194d550e9
Parents: a546da0
Author: quynhn <qn...@linagora.com>
Authored: Wed Nov 22 14:57:39 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:34:44 2017 +0700

----------------------------------------------------------------------
 .../imap/decode/parser/CreateCommandParser.java | 13 +++
 .../decode/parser/CreateCommandParserTest.java  | 87 ++++++++++++++++++++
 2 files changed, 100 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/fd69a44a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java
----------------------------------------------------------------------
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java
index 7aed344..b4cd72e 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java
@@ -22,6 +22,7 @@ import org.apache.james.imap.api.ImapCommand;
 import org.apache.james.imap.api.ImapConstants;
 import org.apache.james.imap.api.ImapMessage;
 import org.apache.james.imap.api.ImapSessionUtils;
+import org.apache.james.imap.api.display.HumanReadableText;
 import org.apache.james.imap.api.process.ImapSession;
 import org.apache.james.imap.decode.ImapRequestLineReader;
 import org.apache.james.imap.decode.base.AbstractImapCommandParser;
@@ -29,6 +30,9 @@ import org.apache.james.imap.message.request.CreateRequest;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.protocols.imap.DecodingException;
 
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+
 /**
  * Parse CREATE commands
  */
@@ -63,7 +67,16 @@ public class CreateCommandParser extends AbstractImapCommandParser {
             }
         }
         request.eol();
+        assertMailboxNameJustContainDelimiter(mailboxName, mailboxSession.getPathDelimiter());
         return new CreateRequest(command, mailboxName, tag);
     }
 
+    private void assertMailboxNameJustContainDelimiter(String mailboxName, char delimiter) throws DecodingException {
+        Splitter.on(delimiter)
+            .splitToList(mailboxName)
+            .stream()
+            .filter(s -> !Strings.isNullOrEmpty(s))
+            .findAny()
+            .orElseThrow(() -> new DecodingException(HumanReadableText.ILLEGAL_ARGUMENTS, "Invalid mailbox name"));
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/fd69a44a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java
----------------------------------------------------------------------
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java
new file mode 100644
index 0000000..7f6a4f4
--- /dev/null
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java
@@ -0,0 +1,87 @@
+/****************************************************************
+ * 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.imap.decode.parser;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.james.imap.api.ImapCommand;
+import org.apache.james.imap.api.ImapSessionUtils;
+import org.apache.james.imap.api.process.ImapSession;
+import org.apache.james.imap.decode.ImapRequestStreamLineReader;
+import org.apache.james.imap.message.request.CreateRequest;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.mock.MockMailboxSession;
+import org.apache.james.protocols.imap.DecodingException;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.base.Charsets;
+
+public class CreateCommandParserTest {
+    private static final OutputStream outputStream = null;
+    private static final ImapCommand command = ImapCommand.anyStateCommand("Command");
+    private static final String TAG = "A1";
+
+    private ImapSession mockImapSession;
+    private MailboxSession mailboxSession;
+    private CreateCommandParser parser;
+
+    @Before
+    public void setUp() throws Exception {
+        mockImapSession = mock(ImapSession.class);
+        mailboxSession = new MockMailboxSession("userName");
+
+        when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mailboxSession);
+
+        parser = new CreateCommandParser();
+    }
+
+    @Test(expected = DecodingException.class)
+    public void decodeShouldThrowWhenCommandHasEmptyMailbox() throws DecodingException {
+        InputStream inputStream = new ByteArrayInputStream(" \n".getBytes(Charsets.US_ASCII));
+        ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, outputStream);
+
+        parser.decode(command, lineReader, TAG, mockImapSession);
+    }
+
+    @Test(expected = DecodingException.class)
+    public void decodeShouldThrowWhenCommandHasOnlySeparatorMailbox() throws DecodingException {
+        InputStream inputStream = new ByteArrayInputStream("..\n".getBytes(Charsets.US_ASCII));
+        ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, outputStream);
+
+        parser.decode(command, lineReader, TAG, mockImapSession);
+    }
+
+    @Test
+    public void decodeShouldReturnCreateRequestWhenValidMailboxName() throws Exception {
+        InputStream inputStream = new ByteArrayInputStream(".AnyMailbox.\n".getBytes(Charsets.US_ASCII));
+        ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, outputStream);
+
+        CreateRequest imapMessage = (CreateRequest)parser.decode(command, lineReader, TAG, mockImapSession);
+        assertThat(imapMessage.getMailboxName()).isEqualTo(".AnyMailbox");
+    }
+
+}
\ No newline at end of file


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


[7/9] james-project git commit: JAMES-2240 Fix use of MD5 for checksum to index email body

Posted by bt...@apache.org.
JAMES-2240 Fix use of MD5 for checksum to index email body


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4762638a
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4762638a
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4762638a

Branch: refs/heads/master
Commit: 4762638a94b2ce9b85240f8af109298d1db56267
Parents: d6d0e67
Author: Thibaut SAUTEREAU <ts...@linagora.com>
Authored: Tue Nov 28 14:47:28 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:37:29 2017 +0700

----------------------------------------------------------------------
 .../mailrepository/file/MBoxMailRepository.java | 52 ++++++--------------
 1 file changed, 16 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4762638a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java b/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
index 2ea4417..5bb7d7c 100755
--- a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
+++ b/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
@@ -51,8 +51,6 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -72,13 +70,15 @@ import javax.mail.internet.MimeMessage;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.io.FileUtils;
-import org.apache.james.server.core.MailImpl;
 import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.mailrepository.api.MailRepository;
+import org.apache.james.server.core.MailImpl;
 import org.apache.mailet.Mail;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.hash.Hashing;
+
 /**
  * Implementation of a MailRepository using UNIX mbox files.
  * 
@@ -136,8 +136,8 @@ public class MBoxMailRepository implements MailRepository, Configurable {
     private static boolean BUFFERING = true;
 
     /**
-     * The internal list of the emails The key is an adapted MD5 checksum of the
-     * mail
+     * The internal list of emails.
+     * The key is an adapted SHA-256 fingerprint of the email body.
      */
     private Hashtable<String, Long> mList = null;
     /**
@@ -250,20 +250,10 @@ public class MBoxMailRepository implements MailRepository, Configurable {
     }
 
     /**
-     * Generate a hex representation of an MD5 checksum on the emailbody
-     * 
-     * @param emailBody
-     * @return A hex representation of the text
-     * @throws NoSuchAlgorithmException
+     * Generate a hex representation of a SHA-256 checksum on the email body
      */
-    private String generateKeyValue(String emailBody) throws NoSuchAlgorithmException {
-        // MD5 the email body for a reilable (ha ha) key
-        byte[] digArray = MessageDigest.getInstance("MD5").digest(emailBody.getBytes());
-        StringBuilder digest = new StringBuilder();
-        for (byte aDigArray : digArray) {
-            digest.append(Integer.toString(aDigArray, Character.MAX_RADIX).toUpperCase(Locale.US));
-        }
-        return digest.toString();
+    private String generateKeyValue(String emailBody) {
+        return Hashing.sha256().hashUnencodedChars(emailBody).toString();
     }
 
     /**
@@ -440,13 +430,9 @@ public class MBoxMailRepository implements MailRepository, Configurable {
                 }
 
                 public MimeMessage messageAction(String messageSeparator, String bodyText, long messageStart) {
-                    try {
-                        if (key.equals(generateKeyValue(bodyText))) {
-                            LOGGER.debug(this.getClass().getName() + " Located message. Returning MIME message");
-                            return convertTextToMimeMessage(bodyText);
-                        }
-                    } catch (NoSuchAlgorithmException e) {
-                        LOGGER.error("MD5 not supported! ", e);
+                    if (key.equals(generateKeyValue(bodyText))) {
+                        LOGGER.debug(this.getClass().getName() + " Located message. Returning MIME message");
+                        return convertTextToMimeMessage(bodyText);
                     }
                     return null;
                 }
@@ -498,16 +484,12 @@ public class MBoxMailRepository implements MailRepository, Configurable {
                 }
 
                 public MimeMessage messageAction(String messageSeparator, String bodyText, long messageStart) {
-                    try {
-                        String key = generateKeyValue(bodyText);
-                        mList.put(key, messageStart);
-                        if ((LOGGER.isDebugEnabled())) {
-                            LOGGER.debug(this.getClass().getName() + " Key " + key + " at " + messageStart);
-                        }
-
-                    } catch (NoSuchAlgorithmException e) {
-                        LOGGER.error("MD5 not supported! ", e);
+                    String key = generateKeyValue(bodyText);
+                    mList.put(key, messageStart);
+                    if ((LOGGER.isDebugEnabled())) {
+                        LOGGER.debug(this.getClass().getName() + " Key " + key + " at " + messageStart);
                     }
+
                     return null;
                 }
             });
@@ -722,8 +704,6 @@ public class MBoxMailRepository implements MailRepository, Configurable {
                             outputFile.writeBytes(bodyText);
 
                         }
-                    } catch (NoSuchAlgorithmException e) {
-                        LOGGER.error("MD5 not supported! ", e);
                     } catch (IOException e) {
                         LOGGER.error("Unable to write file (General I/O problem) " + mboxFile, e);
                     }


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


[9/9] james-project git commit: JAMES-2240 remove useless commented code blocks

Posted by bt...@apache.org.
JAMES-2240 remove useless commented code blocks


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4135794a
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4135794a
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4135794a

Branch: refs/heads/master
Commit: 4135794a1d705903432fce47491d55b544050715
Parents: 5f07eee
Author: Matthieu Baechler <ma...@apache.org>
Authored: Wed Nov 29 11:52:56 2017 +0100
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:37:29 2017 +0700

----------------------------------------------------------------------
 .../mailrepository/file/MBoxMailRepository.java | 27 --------------------
 1 file changed, 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4135794a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java b/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
index 9e1ca41..b9c3f02 100755
--- a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
+++ b/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
@@ -227,22 +227,6 @@ public class MBoxMailRepository implements MailRepository, Configurable {
             LOGGER.debug("Mime message is null");
         }
 
-        /*
-         * String toAddr = null; try { // Attempt to read the TO field and see
-         * if it errors toAddr =
-         * mimeMessage.getRecipients(javax.mail.Message.RecipientType
-         * .TO).toString(); } catch (Exception e) { // It has errored, so time
-         * for plan B // use the from field I suppose try {
-         * mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO,
-         * mimeMessage.getFrom()); if (LOGGER.isDebugEnabled()) {
-         * StringBuffer logBuffer = new StringBuffer(128)
-         * .append(this.getClass().getName())
-         * .append(" Patching To: field for message ")
-         * .append(" with  From: field");
-         * LOGGER.debug(logBuffer.toString()); } } catch
-         * (MessagingException e1) {
-         * LOGGER.error("Unable to set to: field to from: field", e); } }
-         */
         return mimeMessage;
     }
 
@@ -281,11 +265,6 @@ public class MBoxMailRepository implements MailRepository, Configurable {
                     foundSep = sepMatchPattern.matcher(line).matches();
 
                     if (foundSep && inMessage) {
-                        // if ((DEEP_DEBUG) && (LOGGER.isDebugEnabled())) {
-                        // LOGGER.debug(this.getClass().getName() +
-                        // " Invoking " + messAct.getClass() + " at " +
-                        // prevMessageStart);
-                        // }
                         MimeMessage endResult = messAct.messageAction(previousMessageSeparator, messageBuffer.toString(), prevMessageStart);
                         if (messAct.isComplete()) {
                             // I've got what I want so just exit
@@ -311,12 +290,6 @@ public class MBoxMailRepository implements MailRepository, Configurable {
                     if (c == 10) {
                         foundSep = sepMatchPattern.matcher(line).matches();
                         if (foundSep && inMessage) {
-                            // if ((DEEP_DEBUG) &&
-                            // (LOGGER.isDebugEnabled())) {
-                            // LOGGER.debug(this.getClass().getName() +
-                            // " Invoking " + messAct.getClass() + " at " +
-                            // prevMessageStart);
-                            // }
                             MimeMessage endResult = messAct.messageAction(previousMessageSeparator, messageBuffer.toString(), prevMessageStart);
                             if (messAct.isComplete()) {
                                 // I've got what I want so just exit


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


[3/9] james-project git commit: JAMES-2233 Add request/response logging to webadmin

Posted by bt...@apache.org.
JAMES-2233 Add request/response logging to webadmin


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/449d3088
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/449d3088
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/449d3088

Branch: refs/heads/master
Commit: 449d30881ed437ffec70d495c03c5d5d26170382
Parents: fa760c8
Author: benwa <bt...@linagora.com>
Authored: Thu Nov 23 14:35:39 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:35:51 2017 +0700

----------------------------------------------------------------------
 .../apache/james/webadmin/WebAdminServer.java   |  4 ++
 .../webadmin/mdc/LoggingRequestFilter.java      | 45 +++++++++++++++++++
 .../webadmin/mdc/LoggingResponseFilter.java     | 47 ++++++++++++++++++++
 3 files changed, 96 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/449d3088/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
index d621d8c..e5a47b8 100644
--- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
@@ -29,6 +29,8 @@ import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.metrics.api.MetricFactory;
 import org.apache.james.webadmin.authentication.AuthenticationFilter;
+import org.apache.james.webadmin.mdc.LoggingRequestFilter;
+import org.apache.james.webadmin.mdc.LoggingResponseFilter;
 import org.apache.james.webadmin.mdc.MDCCleanupFilter;
 import org.apache.james.webadmin.mdc.MDCFilter;
 import org.apache.james.webadmin.metric.MetricPostFilter;
@@ -80,6 +82,8 @@ public class WebAdminServer implements Configurable {
 
     private void configureMDC() {
         service.before(new MDCFilter());
+        service.before(new LoggingRequestFilter());
+        service.after(new LoggingResponseFilter());
         service.after(new MDCCleanupFilter());
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/449d3088/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
new file mode 100644
index 0000000..0087c7b
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
@@ -0,0 +1,45 @@
+/****************************************************************
+ * 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.webadmin.mdc;
+
+import java.io.Closeable;
+
+import org.apache.james.util.MDCBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import spark.Filter;
+import spark.Request;
+import spark.Response;
+
+public class LoggingRequestFilter implements Filter {
+    private static final Logger LOGGER = LoggerFactory.getLogger(LoggingRequestFilter.class);
+    private static final String BODY = "body";
+
+    @Override
+    public void handle(Request request, Response response) throws Exception {
+        try (Closeable closeable =
+                 MDCBuilder.create()
+                     .addContext(BODY, request.body())
+                     .build()) {
+            LOGGER.info("Received request");
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/449d3088/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
new file mode 100644
index 0000000..a72e71e
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
@@ -0,0 +1,47 @@
+/****************************************************************
+ * 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.webadmin.mdc;
+
+import java.io.Closeable;
+
+import org.apache.james.util.MDCBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import spark.Filter;
+import spark.Request;
+import spark.Response;
+
+public class LoggingResponseFilter implements Filter {
+    private static final Logger LOGGER = LoggerFactory.getLogger(LoggingResponseFilter.class);
+    private static final String STATUS = "status";
+    private static final String BODY = "body";
+
+    @Override
+    public void handle(Request request, Response response) throws Exception {
+        try (Closeable closeable =
+                 MDCBuilder.create()
+                     .addContext(STATUS, response.status())
+                     .addContext(BODY, response.body())
+                     .build()) {
+            LOGGER.info("Received request");
+        }
+    }
+}


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


[6/9] james-project git commit: JAMES-2239 Bounce email now properly decode header fields

Posted by bt...@apache.org.
JAMES-2239 Bounce email now properly decode header fields


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d6d0e679
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d6d0e679
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d6d0e679

Branch: refs/heads/master
Commit: d6d0e679c64f1af30101b82c64b18eedb1462482
Parents: a585dc2
Author: Luc DUZAN <ld...@linagora.com>
Authored: Tue Nov 28 16:09:12 2017 +0100
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:36:59 2017 +0700

----------------------------------------------------------------------
 .../mailets/redirect/NotifyMailetsMessage.java  | 18 +++-
 .../redirect/NotifyMailetsMessageTest.java      | 98 ++++++++++++++++++++
 2 files changed, 114 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d6d0e679/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java
index b7511e8..1a7e1ee 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java
@@ -19,11 +19,13 @@
 
 package org.apache.james.transport.mailets.redirect;
 
+import java.io.UnsupportedEncodingException;
 import java.util.List;
 import java.util.Optional;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeUtility;
 
 import org.apache.james.core.MailAddress;
 import org.apache.james.transport.util.SizeUtils;
@@ -59,7 +61,8 @@ public class NotifyMailetsMessage {
             .append(LINE_BREAK);
 
         if (message.getSubject() != null) {
-            builder.append("  Subject: " + message.getSubject())
+            builder.append("  Subject: ")
+                .append(safelyDecode(message.getSubject()))
                 .append(LINE_BREAK);
         }
         if (message.getSentDate() != null) {
@@ -110,12 +113,23 @@ public class NotifyMailetsMessage {
         }
     }
 
+    @VisibleForTesting static String safelyDecode(String text) {
+        try {
+            return MimeUtility.decodeText(text);
+        } catch (UnsupportedEncodingException e) {
+            LOGGER.error("Could not decode following value {}", text, e);
+
+            return text;
+        }
+    }
+
     private void appendAddresses(StringBuilder builder, String title, String[] addresses) {
         if (addresses != null) {
             builder.append("  " + title + ": ")
                 .append(LINE_BREAK);
             for (String address : flatten(addresses)) {
-                builder.append(address + " ")
+                builder.append(safelyDecode(address))
+                    .append(" ")
                     .append(LINE_BREAK);
             }
             builder.append(LINE_BREAK);

http://git-wip-us.apache.org/repos/asf/james-project/blob/d6d0e679/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java
index ca6448c..0b4c993 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java
@@ -33,6 +33,7 @@ import javax.mail.MessagingException;
 import javax.mail.Session;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeUtility;
 
 import org.apache.james.core.MailAddress;
 import org.apache.mailet.Mail;
@@ -232,4 +233,101 @@ public class NotifyMailetsMessageTest {
         assertThat(NotifyMailetsMessage.getMessageSizeEstimation(mail))
             .isEqualTo(Optional.of(size));
     }
+
+    @Test
+    public void generateMessageShouldDecodeEncodedSubject() throws Exception {
+        String content = "MIME-Version: 1.0\r\n" +
+            "Subject: =?UTF-8?Q?Cl=c3=b4ture_&_Paie_du_mois?=\r\n" +
+            "Content-Type: text/plain; charset=utf-8\r\n" +
+            "\r\n" +
+            "test\r\n";
+
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(content.getBytes()));
+        FakeMail mail = FakeMail.from(message);
+
+        String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail);
+
+        assertThat(generateMessage).contains("Subject: Clôture & Paie du mois");
+    }
+
+    @Test
+    public void generateMessageShouldDecodeEncodedFrom() throws Exception {
+        String content = "MIME-Version: 1.0\r\n" +
+            "From: =?UTF-8?Q?=F0=9F=90=83@linagora.com?=\r\n" +
+            "Content-Type: text/plain; charset=utf-8\r\n" +
+            "\r\n" +
+            "test\r\n";
+
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(content.getBytes()));
+        FakeMail mail = FakeMail.from(message);
+
+        String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail);
+
+        assertThat(generateMessage).contains("  From: \n" +
+            "🐃@linagora.com");
+    }
+
+    @Test
+    public void generateMessageShouldDecodeEncodedTo() throws Exception {
+        String content = "MIME-Version: 1.0\r\n" +
+            "To: =?UTF-8?Q?=F0=9F=9A=BE@linagora.com?=\r\n" +
+            "Content-Type: text/plain; charset=utf-8\r\n" +
+            "\r\n" +
+            "test\r\n";
+
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(content.getBytes()));
+        FakeMail mail = FakeMail.from(message);
+
+        String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail);
+
+        assertThat(generateMessage).contains("  To: \n" +
+            "🚾@linagora.com");
+    }
+
+    @Test
+    public void generateMessageShouldDecodeEncodedCc() throws Exception {
+        String content = "MIME-Version: 1.0\r\n" +
+            "Cc: =?UTF-8?Q?=F0=9F=9A=B2@linagora.com?=\r\n" +
+            "Content-Type: text/plain; charset=utf-8\r\n" +
+            "\r\n" +
+            "test\r\n";
+
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(content.getBytes()));
+        FakeMail mail = FakeMail.from(message);
+
+        String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail);
+
+        assertThat(generateMessage).contains("  CC: \n" +
+            "🚲@linagora.com");
+    }
+
+    @Test
+    public void safelyDecodeShouldReturnTextNotEncodedUnmodified() throws Exception {
+        String text = "Why not unicode for Llama";
+
+        assertThat(NotifyMailetsMessage.safelyDecode(text))
+            .isEqualTo(text);
+    }
+
+    @Test
+    public void safelyDecodeShouldCorrectlyDecodeQuotedPrintable() throws Exception {
+        assertThat(NotifyMailetsMessage.safelyDecode("=?UTF-8?Q?=E2=99=A5=F0=9F=9A=B2?="))
+            .isEqualTo("♥🚲");
+    }
+
+    @Test
+    public void safelyDecodeShouldReturnInvalidEncodedTextUnmodified() throws Exception {
+        String invalidEncodedText = "=?UTF-8?Q?=E2=99=A5=FX=9F=9A=B2?=";
+
+        assertThat(NotifyMailetsMessage.safelyDecode(invalidEncodedText))
+            .isEqualTo(invalidEncodedText);
+    }
+
+    @Test
+    public void safelyDecodeShouldReturnEncodedTextUnmodifiedWhenUnknownCharset() throws Exception {
+        String encodedTextWithUnknownCharset = "=?UTF-9?Q?=E2=99=A5=F0=9F=9A=B2?=";
+
+        assertThat(NotifyMailetsMessage.safelyDecode(encodedTextWithUnknownCharset))
+            .isEqualTo(encodedTextWithUnknownCharset);
+    }
 }


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


[8/9] james-project git commit: JAMES-2240 remove conditional logging

Posted by bt...@apache.org.
JAMES-2240 remove conditional logging


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/5f07eee7
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/5f07eee7
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/5f07eee7

Branch: refs/heads/master
Commit: 5f07eee7d838e17cbf2c26b17d1085dc02c9f07e
Parents: 4762638
Author: Matthieu Baechler <ma...@apache.org>
Authored: Wed Nov 29 11:51:06 2017 +0100
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:37:29 2017 +0700

----------------------------------------------------------------------
 .../mailrepository/file/MBoxMailRepository.java | 72 ++++----------------
 1 file changed, 14 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/5f07eee7/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java b/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
index 5bb7d7c..9e1ca41 100755
--- a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
+++ b/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
@@ -173,9 +173,7 @@ public class MBoxMailRepository implements MailRepository, Configurable {
             mboxFile = destination.substring("mbox://".length());
         }
 
-        if (LOGGER.isDebugEnabled()) {
-            LOGGER.debug("MBoxMailRepository.destinationURL: " + destination);
-        }
+        LOGGER.debug("MBoxMailRepository.destinationURL: {}", destination);
 
         String checkType = configuration.getString("[@type]");
         if (!(checkType.equals("MAIL") || checkType.equals("SPOOL"))) {
@@ -225,9 +223,8 @@ public class MBoxMailRepository implements MailRepository, Configurable {
             LOGGER.error("Unable to parse mime message!", e);
         }
 
-        if (mimeMessage == null && LOGGER.isDebugEnabled()) {
-            String logBuffer = this.getClass().getName() + " Mime message is null";
-            LOGGER.debug(logBuffer);
+        if (mimeMessage == null) {
+            LOGGER.debug("Mime message is null");
         }
 
         /*
@@ -266,11 +263,7 @@ public class MBoxMailRepository implements MailRepository, Configurable {
      *            The action to take when a message is found
      */
     private MimeMessage parseMboxFile(RandomAccessFile ins, MessageAction messAct) {
-        if ((LOGGER.isDebugEnabled())) {
-            String logBuffer = this.getClass().getName() + " Start parsing " + mboxFile;
-
-            LOGGER.debug(logBuffer);
-        }
+        LOGGER.debug("Start parsing {}", mboxFile);
         try {
 
             Pattern sepMatchPattern = Pattern.compile("^From (.*) (.*):(.*):(.*)$");
@@ -358,11 +351,7 @@ public class MBoxMailRepository implements MailRepository, Configurable {
         } catch (PatternSyntaxException e) {
             LOGGER.error("Bad regex passed " + mboxFile, e);
         } finally {
-            if ((LOGGER.isDebugEnabled())) {
-                String logBuffer = this.getClass().getName() + " Finished parsing " + mboxFile;
-
-                LOGGER.debug(logBuffer);
-            }
+            LOGGER.debug("Finished parsing {}", mboxFile);
         }
         return null;
     }
@@ -404,19 +393,11 @@ public class MBoxMailRepository implements MailRepository, Configurable {
         // Can we find the key first
         if (mList == null || !mList.containsKey(key)) {
             // Not initiailised so no point looking
-            if ((LOGGER.isDebugEnabled())) {
-                String logBuffer = this.getClass().getName() + " mList - key not found " + mboxFile;
-
-                LOGGER.debug(logBuffer);
-            }
+            LOGGER.debug("mList - key not found {}", mboxFile);
             return foundMessage;
         }
         long messageStart = mList.get(key);
-        if ((LOGGER.isDebugEnabled())) {
-            String logBuffer = this.getClass().getName() + " Load message starting at offset " + messageStart + " from file " + mboxFile;
-
-            LOGGER.debug(logBuffer);
-        }
+        LOGGER.debug("Load message starting at offset {} from file {}", messageStart, mboxFile);
         // Now try and find the position in the file
         RandomAccessFile ins = null;
         try {
@@ -444,11 +425,7 @@ public class MBoxMailRepository implements MailRepository, Configurable {
             LOGGER.error("Unable to write file (General I/O problem) " + mboxFile, e);
         } finally {
             if (foundMessage == null) {
-                if ((LOGGER.isDebugEnabled())) {
-                    String logBuffer = this.getClass().getName() + " select - message not found " + mboxFile;
-
-                    LOGGER.debug(logBuffer);
-                }
+                LOGGER.debug("select - message not found {}", mboxFile);
             }
             if (ins != null)
                 try {
@@ -486,9 +463,7 @@ public class MBoxMailRepository implements MailRepository, Configurable {
                 public MimeMessage messageAction(String messageSeparator, String bodyText, long messageStart) {
                     String key = generateKeyValue(bodyText);
                     mList.put(key, messageStart);
-                    if ((LOGGER.isDebugEnabled())) {
-                        LOGGER.debug(this.getClass().getName() + " Key " + key + " at " + messageStart);
-                    }
+                    LOGGER.debug("Key {} at {}", key, messageStart);
 
                     return null;
                 }
@@ -513,12 +488,8 @@ public class MBoxMailRepository implements MailRepository, Configurable {
      * @see org.apache.james.mailrepository.api.MailRepository#store(Mail)
      */
     public void store(Mail mc) {
+        LOGGER.debug("Will store message to file {}", mboxFile);
 
-        if ((LOGGER.isDebugEnabled())) {
-            String logBuffer = this.getClass().getName() + " Will store message to file " + mboxFile;
-
-            LOGGER.debug(logBuffer);
-        }
         this.mList = null;
         // Now make up the from header
         String fromHeader = null;
@@ -564,11 +535,8 @@ public class MBoxMailRepository implements MailRepository, Configurable {
             // correct for it BEFORE we return the iterator.
             findMessage(keys.iterator().next());
         }
-        if ((LOGGER.isDebugEnabled())) {
-            String logBuffer = this.getClass().getName() + " " + keys.size() + " keys to be iterated over.";
 
-            LOGGER.debug(logBuffer);
-        }
+        LOGGER.debug("{} keys to be iterated over.", keys.size());
         if (fifo)
             Collections.sort(keys); // Keys is a HashSet; impose FIFO for apps
                                     // that need it
@@ -591,11 +559,7 @@ public class MBoxMailRepository implements MailRepository, Configurable {
         res = new MailImpl();
         res.setMessage(foundMessage);
         res.setName(key);
-        if ((LOGGER.isDebugEnabled())) {
-            String logBuffer = this.getClass().getName() + " Retrieving entry for key " + key;
-
-            LOGGER.debug(logBuffer);
-        }
+        LOGGER.debug("Retrieving entry for key {}", key);
         return res;
     }
 
@@ -623,11 +587,7 @@ public class MBoxMailRepository implements MailRepository, Configurable {
             // So wait for a file
             while (!mBoxLock.createNewFile() && sleepCount < MAXSLEEPTIMES) {
                 try {
-                    if ((LOGGER.isDebugEnabled())) {
-                        String logBuffer = this.getClass().getName() + " Waiting for lock on file " + mboxFile;
-
-                        LOGGER.debug(logBuffer);
-                    }
+                    LOGGER.debug("Waiting for lock on file {}", mboxFile);
 
                     Thread.sleep(LOCKSLEEPDELAY);
                     sleepCount++;
@@ -661,11 +621,7 @@ public class MBoxMailRepository implements MailRepository, Configurable {
      * @see org.apache.james.mailrepository.api.MailRepository#remove(Collection)
      */
     public void remove(final Collection<Mail> mails) {
-        if ((LOGGER.isDebugEnabled())) {
-            String logBuffer = this.getClass().getName() + " Removing entry for key " + mails;
-
-            LOGGER.debug(logBuffer);
-        }
+        LOGGER.debug("Removing entry for key {}", mails);
         // The plan is as follows:
         // Attempt to locate the message in the file
         // by reading through the


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


[4/9] james-project git commit: JAMES-2233 Add structured logging API and implementation based on MDC

Posted by bt...@apache.org.
JAMES-2233 Add structured logging API and implementation based on MDC


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1cd5c3b5
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1cd5c3b5
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1cd5c3b5

Branch: refs/heads/master
Commit: 1cd5c3b586dfafa81c12a42fb95f228b8d4bd551
Parents: 449d308
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 14:22:38 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:36:14 2017 +0700

----------------------------------------------------------------------
 .../java/org/apache/james/util/MDCBuilder.java  | 11 +++++
 .../apache/james/util/MDCStructuredLogger.java  | 50 ++++++++++++++++++++
 .../org/apache/james/util/StructuredLogger.java | 30 ++++++++++++
 3 files changed, 91 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/1cd5c3b5/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java b/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
index e728518..7490d41 100644
--- a/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
@@ -39,6 +39,10 @@ import com.google.common.collect.ImmutableMap;
 
 public class MDCBuilder {
 
+    public interface VoidOperation {
+        void perform();
+    }
+
     public static <T> T withMdc(MDCBuilder mdcBuilder, Supplier<T> answerSupplier) {
         try (Closeable closeable = mdcBuilder.build()) {
             try {
@@ -52,6 +56,13 @@ public class MDCBuilder {
         }
     }
 
+    public static void withMdc(MDCBuilder mdcBuilder, VoidOperation logOperation) {
+        withMdc(mdcBuilder, () -> {
+            logOperation.perform();
+            return null;
+        });
+    }
+
     public static final String HOST = "host";
     public static final String IP = "ip";
     public static final String PROTOCOL = "protocol";

http://git-wip-us.apache.org/repos/asf/james-project/blob/1cd5c3b5/server/container/util-java8/src/main/java/org/apache/james/util/MDCStructuredLogger.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/MDCStructuredLogger.java b/server/container/util-java8/src/main/java/org/apache/james/util/MDCStructuredLogger.java
new file mode 100644
index 0000000..cf970ea
--- /dev/null
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/MDCStructuredLogger.java
@@ -0,0 +1,50 @@
+/****************************************************************
+ * 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.util;
+
+import java.util.function.Consumer;
+
+import org.slf4j.Logger;
+
+public class MDCStructuredLogger implements StructuredLogger {
+
+    public static MDCStructuredLogger forLogger(Logger logger) {
+        return new MDCStructuredLogger(logger);
+    }
+
+    private final Logger logger;
+    private final MDCBuilder mdcBuilder;
+
+    public MDCStructuredLogger(Logger logger) {
+        this.logger = logger;
+        this.mdcBuilder = MDCBuilder.create();
+    }
+
+    @Override
+    public StructuredLogger addField(String name, Object value) {
+        mdcBuilder.addContext(name, value);
+        return this;
+    }
+
+    @Override
+    public void log(Consumer<Logger> logOperation) {
+        MDCBuilder.withMdc(mdcBuilder, () -> logOperation.accept(logger));
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/1cd5c3b5/server/container/util-java8/src/main/java/org/apache/james/util/StructuredLogger.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/StructuredLogger.java b/server/container/util-java8/src/main/java/org/apache/james/util/StructuredLogger.java
new file mode 100644
index 0000000..f6fcec4
--- /dev/null
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/StructuredLogger.java
@@ -0,0 +1,30 @@
+/****************************************************************
+ * 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.util;
+
+import java.util.function.Consumer;
+
+import org.slf4j.Logger;
+
+public interface StructuredLogger {
+    StructuredLogger addField(String name, Object value);
+
+    void log(Consumer<Logger> logOperation);
+}


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


[2/9] james-project git commit: JAMES-2229 Add imap testing when create mailbox name with only delimiter

Posted by bt...@apache.org.
JAMES-2229 Add imap testing when create mailbox name with only delimiter


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/fa760c87
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/fa760c87
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/fa760c87

Branch: refs/heads/master
Commit: fa760c8714298c3fb1fff249c0966653fabd4f05
Parents: fd69a44
Author: quynhn <qn...@linagora.com>
Authored: Wed Nov 22 14:58:27 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:34:54 2017 +0700

----------------------------------------------------------------------
 .../resources/org/apache/james/imap/scripts/Create.test  |  4 ++++
 .../imap/decode/parser/CreateCommandParserTest.java      | 11 +++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/fa760c87/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/Create.test
----------------------------------------------------------------------
diff --git a/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/Create.test b/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/Create.test
index 87a49d7..67445f7 100644
--- a/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/Create.test
+++ b/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/Create.test
@@ -31,6 +31,10 @@ S: 13 OK CREATE completed.
 # Create quoted
 C: 14 CREATE "Pepe juan"
 S: 14 OK CREATE completed.
+
+C: 15 CREATE ..
+S: 15 BAD CREATE failed. Illegal arguments.
+
 #
 # RFC3501@6.3.3p2
 # When mailbox name is suffixed with hierarchy separator

http://git-wip-us.apache.org/repos/asf/james-project/blob/fa760c87/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java
----------------------------------------------------------------------
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java
index 7f6a4f4..d5d2b6a 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java
@@ -20,6 +20,7 @@
 package org.apache.james.imap.decode.parser;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -59,20 +60,22 @@ public class CreateCommandParserTest {
         parser = new CreateCommandParser();
     }
 
-    @Test(expected = DecodingException.class)
+    @Test
     public void decodeShouldThrowWhenCommandHasEmptyMailbox() throws DecodingException {
         InputStream inputStream = new ByteArrayInputStream(" \n".getBytes(Charsets.US_ASCII));
         ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, outputStream);
 
-        parser.decode(command, lineReader, TAG, mockImapSession);
+        assertThatThrownBy(() -> parser.decode(command, lineReader, TAG, mockImapSession))
+            .isInstanceOf(DecodingException.class);
     }
 
-    @Test(expected = DecodingException.class)
+    @Test
     public void decodeShouldThrowWhenCommandHasOnlySeparatorMailbox() throws DecodingException {
         InputStream inputStream = new ByteArrayInputStream("..\n".getBytes(Charsets.US_ASCII));
         ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, outputStream);
 
-        parser.decode(command, lineReader, TAG, mockImapSession);
+        assertThatThrownBy(() -> parser.decode(command, lineReader, TAG, mockImapSession))
+            .isInstanceOf(DecodingException.class);
     }
 
     @Test


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


[5/9] james-project git commit: JAMES-2233 Use structured logging for WebAdmin request/responses

Posted by bt...@apache.org.
JAMES-2233 Use structured logging for WebAdmin request/responses


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/a585dc24
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/a585dc24
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/a585dc24

Branch: refs/heads/master
Commit: a585dc24072feb6cae9444ee7aa613f30a112b39
Parents: 1cd5c3b
Author: benwa <bt...@linagora.com>
Authored: Tue Nov 28 14:23:04 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:36:15 2017 +0700

----------------------------------------------------------------------
 .../james/webadmin/mdc/LoggingRequestFilter.java   | 15 +++++----------
 .../james/webadmin/mdc/LoggingResponseFilter.java  | 17 ++++++-----------
 2 files changed, 11 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/a585dc24/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
index 0087c7b..08a6e5e 100644
--- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingRequestFilter.java
@@ -19,9 +19,7 @@
 
 package org.apache.james.webadmin.mdc;
 
-import java.io.Closeable;
-
-import org.apache.james.util.MDCBuilder;
+import org.apache.james.util.MDCStructuredLogger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -31,15 +29,12 @@ import spark.Response;
 
 public class LoggingRequestFilter implements Filter {
     private static final Logger LOGGER = LoggerFactory.getLogger(LoggingRequestFilter.class);
-    private static final String BODY = "body";
+    private static final String REQUEST_BODY = "request-body";
 
     @Override
     public void handle(Request request, Response response) throws Exception {
-        try (Closeable closeable =
-                 MDCBuilder.create()
-                     .addContext(BODY, request.body())
-                     .build()) {
-            LOGGER.info("Received request");
-        }
+        MDCStructuredLogger.forLogger(LOGGER)
+            .addField(REQUEST_BODY, request.body())
+            .log(logger -> logger.info("WebAdmin request received"));
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/a585dc24/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
index a72e71e..ebe4ff5 100644
--- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/LoggingResponseFilter.java
@@ -19,9 +19,7 @@
 
 package org.apache.james.webadmin.mdc;
 
-import java.io.Closeable;
-
-import org.apache.james.util.MDCBuilder;
+import org.apache.james.util.MDCStructuredLogger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -32,16 +30,13 @@ import spark.Response;
 public class LoggingResponseFilter implements Filter {
     private static final Logger LOGGER = LoggerFactory.getLogger(LoggingResponseFilter.class);
     private static final String STATUS = "status";
-    private static final String BODY = "body";
+    private static final String RESPONSE_BODY = "response-body";
 
     @Override
     public void handle(Request request, Response response) throws Exception {
-        try (Closeable closeable =
-                 MDCBuilder.create()
-                     .addContext(STATUS, response.status())
-                     .addContext(BODY, response.body())
-                     .build()) {
-            LOGGER.info("Received request");
-        }
+        MDCStructuredLogger.forLogger(LOGGER)
+            .addField(STATUS, response.status())
+            .addField(RESPONSE_BODY, response.body())
+            .log(logger -> logger.info("WebAdmin response received"));
     }
 }


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