james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [5/6] james-project git commit: MAILET-99 Refactor LogMessage
Date Mon, 29 Aug 2016 13:38:08 GMT
MAILET-99 Refactor LogMessage


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

Branch: refs/heads/master
Commit: 08eb7ec6d41da8aa9e34e421110faea1cfeead31
Parents: 00ce982
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Thu Aug 25 16:02:02 2016 +0200
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Mon Aug 29 14:54:34 2016 +0200

----------------------------------------------------------------------
 .../james/transport/mailets/LogMessage.java     | 74 +++++++++++---------
 .../james/transport/mailets/LogMessageTest.java | 32 +++++----
 2 files changed, 59 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/08eb7ec6/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java
b/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java
index ba3a35d..c1c065f 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java
@@ -21,15 +21,20 @@
 
 package org.apache.james.transport.mailets;
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.util.Collections;
 import java.util.Enumeration;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.base.GenericMailet;
 
+import com.google.common.io.ByteStreams;
+
 /**
  * Logs Message Headers and/or Body.
  * If the "passThrough" in confs is true the mail will be left untouched in
@@ -49,15 +54,15 @@ public class LogMessage extends GenericMailet {
     private int bodyMax = 0;
     private String comment = null;
 
+    @Override
     public void init() {
         try {
-            passThrough = (getInitParameter("passThrough") == null) ? true : Boolean.valueOf(getInitParameter("passThrough"));
-            headers = (getInitParameter("headers") == null) ? true : Boolean.valueOf(getInitParameter("headers"));
-            body = (getInitParameter("body") == null) ? true : Boolean.valueOf(getInitParameter("body"));
+            passThrough = getInitParameter("passThrough", true);
+            headers = getInitParameter("headers", true);
+            body = getInitParameter("body", true);
             bodyMax = (getInitParameter("maxBody") == null) ? 0 : Integer.parseInt(getInitParameter("maxBody"));
             comment = getInitParameter("comment");
         } catch (Exception e) {
-            // Ignore exception, default to true
         }
     }
 
@@ -66,27 +71,17 @@ public class LogMessage extends GenericMailet {
         return "LogHeaders Mailet";
     }
 
+    @Override
     public void service(Mail mail) {
         log("Logging mail " + mail.getName());
-        if (comment != null) log(comment);
+        logComment();
         try {
-            if (headers) log(getMessageHeaders(mail.getMessage()));
-            if (body) {
-                int len = bodyMax > 0 ? bodyMax : mail.getMessage().getSize();
-                StringBuilder text = new StringBuilder(len);
-                InputStream is = mail.getMessage().getRawInputStream();
-                byte[] buf = new byte[1024];
-                int read;
-                while (text.length() < len && (read = is.read(buf)) > -1) {
-                    text.append(new String(buf, 0, Math.min(read, len - text.length())));
-                }
-                log(text.toString());
-            }
-        }
-        catch (MessagingException e) {
+            MimeMessage message = mail.getMessage();
+            logHeaders(message);
+            logBody(message);
+        } catch (MessagingException e) {
             log("Error logging message.", e);
-        }
-        catch (java.io.IOException e) {
+        } catch (java.io.IOException e) {
             log("Error logging message.", e);
         }
         if (!passThrough) {
@@ -94,19 +89,30 @@ public class LogMessage extends GenericMailet {
         }
     }
 
-    /**
-     * Utility method for obtaining a string representation of a
-     * Message's headers
-     * 
-     * @param message
-     */
-    private String getMessageHeaders(MimeMessage message) throws MessagingException {
-        @SuppressWarnings("unchecked")
-        Enumeration<String> heads = message.getAllHeaderLines();
-        StringBuilder headBuffer = new StringBuilder(1024).append("\n");
-        while(heads.hasMoreElements()) {
-            headBuffer.append(heads.nextElement().toString()).append("\n");
+    private void logComment() {
+        if (comment != null) {
+            log(comment);
         }
-        return headBuffer.toString();
+    }
+
+    @SuppressWarnings("unchecked")
+    private void logHeaders(MimeMessage message) throws MessagingException {
+        if (headers) {
+            log("\n");
+            for (String header : Collections.list((Enumeration<String>) message.getAllHeaderLines()))
{
+                log(header + "\n");
+            }
+        }
+    }
+
+    private void logBody(MimeMessage message) throws MessagingException, IOException {
+        if (body) {
+            InputStream inputStream = ByteStreams.limit(message.getRawInputStream(), lengthToLog(message));
+            log(IOUtils.toString(inputStream));
+        }
+    }
+
+    private int lengthToLog(MimeMessage message) throws MessagingException {
+        return bodyMax > 0 ? bodyMax : message.getSize();
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/08eb7ec6/mailet/standard/src/test/java/org/apache/james/transport/mailets/LogMessageTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/LogMessageTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/LogMessageTest.java
index df3cad8..f26e3db 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/LogMessageTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/LogMessageTest.java
@@ -80,12 +80,18 @@ public class LogMessageTest {
     @Test
     public void serviceShouldFailWhenMailHasNoStream() throws Exception {
         mailet.init(mailetConfig);
-        expectedException.expect(NegativeArraySizeException.class);
 
         MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
         message.setSubject("subject");
         message.setText("This is a fake mail");
         mailet.service(new FakeMail(message));
+
+        verify(logger).info("Logging mail null");
+        verify(logger).info("\n");
+        verify(logger).info("Subject: subject\n");
+        verify(logger).error("Error logging message.");
+        verify(logger).error("No MimeMessage content");
+        verifyNoMoreInteractions(logger);
     }
 
     @Test
@@ -95,9 +101,9 @@ public class LogMessageTest {
         mailet.service(createMail());
 
         verify(logger).info("Logging mail name");
-        verify(logger).info("\n" +
-                "Subject: subject\n" +
-                "Content-Type: text/plain\n");
+        verify(logger).info("\n");
+        verify(logger).info("Subject: subject\n");
+        verify(logger).info("Content-Type: text/plain\n");
         verify(logger).info("This is a fake mail");
         verifyNoMoreInteractions(logger);
     }
@@ -164,9 +170,9 @@ public class LogMessageTest {
         mailet.service(createMail());
 
         verify(logger).info("Logging mail name");
-        verify(logger).info("\n" +
-                "Subject: subject\n" +
-                "Content-Type: text/plain\n");
+        verify(logger).info("\n");
+        verify(logger).info("Subject: subject\n");
+        verify(logger).info("Content-Type: text/plain\n");
         verifyNoMoreInteractions(logger);
     }
 
@@ -178,9 +184,9 @@ public class LogMessageTest {
         mailet.service(createMail());
 
         verify(logger).info("Logging mail name");
-        verify(logger).info("\n" +
-                "Subject: subject\n" +
-                "Content-Type: text/plain\n");
+        verify(logger).info("\n");
+        verify(logger).info("Subject: subject\n");
+        verify(logger).info("Content-Type: text/plain\n");
         verify(logger).info("Th");
         verifyNoMoreInteractions(logger);
     }
@@ -194,9 +200,9 @@ public class LogMessageTest {
 
         verify(logger).info("Logging mail name");
         verify(logger).info("comment");
-        verify(logger).info("\n" +
-                "Subject: subject\n" +
-                "Content-Type: text/plain\n");
+        verify(logger).info("\n");
+        verify(logger).info("Subject: subject\n");
+        verify(logger).info("Content-Type: text/plain\n");
         verify(logger).info("This is a fake mail");
         verifyNoMoreInteractions(logger);
     }


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


Mime
View raw message