james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [4/9] james-project git commit: JAMES-2578 FakeMail should still be Serializable
Date Mon, 05 Nov 2018 14:26:51 GMT
JAMES-2578 FakeMail should still be Serializable

This solves failures in FileMailQueue


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

Branch: refs/heads/master
Commit: 8126957e251b29ca3f760b7d2786ec013008181f
Parents: 822f6a3
Author: Benoit Tellier <btellier@linagora.com>
Authored: Fri Nov 2 16:19:23 2018 +0700
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Mon Nov 5 11:58:06 2018 +0100

----------------------------------------------------------------------
 .../org/apache/mailet/base/test/FakeMail.java   | 85 ++++++++++++++++++--
 1 file changed, 78 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/8126957e/mailet/test/src/main/java/org/apache/mailet/base/test/FakeMail.java
----------------------------------------------------------------------
diff --git a/mailet/test/src/main/java/org/apache/mailet/base/test/FakeMail.java b/mailet/test/src/main/java/org/apache/mailet/base/test/FakeMail.java
index 87618e9..8260230 100644
--- a/mailet/test/src/main/java/org/apache/mailet/base/test/FakeMail.java
+++ b/mailet/test/src/main/java/org/apache/mailet/base/test/FakeMail.java
@@ -20,11 +20,14 @@
 
 package org.apache.mailet.base.test;
 
+import java.io.IOException;
+import java.io.OptionalDataException;
 import java.io.Serializable;
 import java.io.UnsupportedEncodingException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -37,6 +40,7 @@ import javax.mail.MessagingException;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
+import javax.mail.internet.ParseException;
 
 import org.apache.james.core.MailAddress;
 import org.apache.james.core.MaybeSender;
@@ -59,7 +63,7 @@ import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
-public class FakeMail implements Mail, Serializable {
+public class FakeMail implements Mail {
 
     private static final String DEFAULT_REMOTE_HOST = "111.222.333.444";
     public static final String DEFAULT_REMOTE_ADDRESS = "127.0.0.1";
@@ -298,12 +302,13 @@ public class FakeMail implements Mail, Serializable {
         return FakeMail.builder().build();
     }
 
-    private static Map<String, Serializable> attributes(Mail mail) {
-        ImmutableMap.Builder<String, Serializable> builder = ImmutableMap.builder();
-        for (String attributeName: ImmutableList.copyOf(mail.getAttributeNames())) {
-            builder.put(attributeName, mail.getAttribute(attributeName));
-        }
-        return builder.build();
+    private static ImmutableMap<AttributeName, Attribute> toAttributeMap(Map<String,
?> attributes) {
+        return attributes.entrySet()
+            .stream()
+            .map(entry -> Attribute.convertToAttribute(entry.getKey(), entry.getValue()))
+            .collect(ImmutableMap.toImmutableMap(
+                Attribute::getName,
+                Function.identity()));
     }
 
     private transient MimeMessage msg;
@@ -511,6 +516,72 @@ public class FakeMail implements Mail, Serializable {
         this.size = size;
     }
 
+    /**
+     * Read the FakeMail from an <code>ObjectInputStream</code>.
+     */
+    @SuppressWarnings("unchecked")
+    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
{
+        try {
+            Object obj = in.readObject();
+            if (obj == null) {
+                sender = null;
+            } else if (obj instanceof String) {
+                sender = new MailAddress((String) obj);
+            } else if (obj instanceof MailAddress) {
+                sender = (MailAddress) obj;
+            }
+        } catch (ParseException pe) {
+            throw new IOException("Error parsing sender address: " + pe.getMessage());
+        }
+        recipients = (Collection<MailAddress>) in.readObject();
+        state = (String) in.readObject();
+        errorMessage = (String) in.readObject();
+        name = (String) in.readObject();
+        remoteHost = (String) in.readObject();
+        remoteAddr = (String) in.readObject();
+        setLastUpdated((Date) in.readObject());
+        // the following is under try/catch to be backwards compatible
+        // with messages created with James version <= 2.2.0a8
+        try {
+            setAttributesRaw((Map<String, Object>) in.readObject());
+        } catch (OptionalDataException ode) {
+            if (ode.eof) {
+                attributes = new HashMap<>();
+            } else {
+                throw ode;
+            }
+        }
+        perRecipientHeaders = (PerRecipientHeaders) in.readObject();
+    }
+
+    public void setAttributesRaw(Map<String, Object> attr) {
+        this.attributes = toAttributeMap(attr);
+    }
+
+    public Map<String, Object> getAttributesRaw() {
+        return attributes.entrySet()
+            .stream()
+            .collect(ImmutableMap.toImmutableMap(
+                a -> a.getKey().asString(),
+                b -> b.getValue().getValue().value()));
+    }
+
+    /**
+     * Write the FakeMail to an <code>ObjectOutputStream</code>.
+     */
+    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
+        out.writeObject(sender);
+        out.writeObject(recipients);
+        out.writeObject(state);
+        out.writeObject(errorMessage);
+        out.writeObject(name);
+        out.writeObject(remoteHost);
+        out.writeObject(remoteAddr);
+        out.writeObject(lastUpdated);
+        out.writeObject(getAttributesRaw());
+        out.writeObject(perRecipientHeaders);
+    }
+
     @Override
     public final boolean equals(Object o) {
         if (o instanceof FakeMail) {


---------------------------------------------------------------------
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