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 ma...@apache.org on 2016/07/12 08:51:40 UTC

james-project git commit: JAMES-1797 don't dispose MimeMessageInputStreamSource file too early

Repository: james-project
Updated Branches:
  refs/heads/apache/master [created] c3d7956eb


JAMES-1797 don't dispose MimeMessageInputStreamSource file too early


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

Branch: refs/heads/apache/master
Commit: c3d7956eb260c0232aa0cea9ef1ab3ecf50151d8
Parents: 03be8a46
Author: Matthieu Baechler <ma...@linagora.com>
Authored: Tue Jul 12 10:46:11 2016 +0200
Committer: Matthieu Baechler <ma...@linagora.com>
Committed: Tue Jul 12 10:46:11 2016 +0200

----------------------------------------------------------------------
 server/container/core/pom.xml                   |  5 +-
 .../core/MimeMessageInputStreamSource.java      |  9 ++--
 .../core/MimeMessageInputStreamSourceTest.java  | 54 ++++++++++++++++++++
 .../apache/james/util/ZeroedInputStream.java    | 42 +++++++++++++++
 .../integration/SetMessagesMethodTest.java      |  5 +-
 .../integration/cucumber/UploadStepdefs.java    | 23 +--------
 .../methods/SetMessagesCreationProcessor.java   |  9 +++-
 7 files changed, 116 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/c3d7956e/server/container/core/pom.xml
----------------------------------------------------------------------
diff --git a/server/container/core/pom.xml b/server/container/core/pom.xml
index 98b8a55..eefd411 100644
--- a/server/container/core/pom.xml
+++ b/server/container/core/pom.xml
@@ -48,7 +48,10 @@
             <groupId>org.apache.james</groupId>
             <artifactId>james-server-lifecycle-api</artifactId>
         </dependency>
-
+        <dependency>
+            <groupId>org.apache.james</groupId>
+            <artifactId>james-server-util</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.apache.james</groupId>
             <artifactId>apache-mailet-api</artifactId>

http://git-wip-us.apache.org/repos/asf/james-project/blob/c3d7956e/server/container/core/src/main/java/org/apache/james/core/MimeMessageInputStreamSource.java
----------------------------------------------------------------------
diff --git a/server/container/core/src/main/java/org/apache/james/core/MimeMessageInputStreamSource.java b/server/container/core/src/main/java/org/apache/james/core/MimeMessageInputStreamSource.java
index 8bcaa3e..f1e1b6d 100644
--- a/server/container/core/src/main/java/org/apache/james/core/MimeMessageInputStreamSource.java
+++ b/server/container/core/src/main/java/org/apache/james/core/MimeMessageInputStreamSource.java
@@ -81,16 +81,15 @@ public class MimeMessageInputStreamSource extends MimeMessageSource implements D
             IOUtils.copy(in, out);
             sourceId = key;
         } catch (IOException ioe) {
+            File file = out.getFile();
+            if (file != null) {
+                FileUtils.deleteQuietly(file);
+            }
             throw new MessagingException("Unable to retrieve the data: " + ioe.getMessage(), ioe);
         } finally {
             try {
                 if (out != null) {
                     out.close();
-
-                    File file = out.getFile();
-                    if (file != null) {
-                        FileUtils.forceDelete(file);
-                    }
                 }
             } catch (IOException ioe) {
                 // Ignored - logging unavailable to log this non-fatal error.

http://git-wip-us.apache.org/repos/asf/james-project/blob/c3d7956e/server/container/core/src/test/java/org/apache/james/core/MimeMessageInputStreamSourceTest.java
----------------------------------------------------------------------
diff --git a/server/container/core/src/test/java/org/apache/james/core/MimeMessageInputStreamSourceTest.java b/server/container/core/src/test/java/org/apache/james/core/MimeMessageInputStreamSourceTest.java
new file mode 100644
index 0000000..009d4bf
--- /dev/null
+++ b/server/container/core/src/test/java/org/apache/james/core/MimeMessageInputStreamSourceTest.java
@@ -0,0 +1,54 @@
+/****************************************************************
+ * 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.core;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+
+import javax.mail.MessagingException;
+
+import org.apache.james.util.ZeroedInputStream;
+import org.junit.After;
+import org.junit.Test;
+
+public class MimeMessageInputStreamSourceTest {
+
+    private static final int _1M = 1024*1024;
+    private static final int _10KB = 10*1024;
+    private MimeMessageInputStreamSource testee;
+
+    @After
+    public void tearDown() {
+        testee.dispose();
+    }
+    
+    @Test
+    public void streamWith1MBytesShouldBeReadable() throws MessagingException, IOException {
+        testee = new MimeMessageInputStreamSource("myKey", new ZeroedInputStream(_1M));
+        assertThat(testee.getInputStream()).hasContentEqualTo(new ZeroedInputStream(_1M));
+    }
+    
+    @Test
+    public void streamWith10KBytesShouldBeReadable() throws MessagingException, IOException {
+        testee = new MimeMessageInputStreamSource("myKey", new ZeroedInputStream(_10KB));
+        assertThat(testee.getInputStream()).hasContentEqualTo(new ZeroedInputStream(_10KB));
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/c3d7956e/server/container/util/src/main/java/org/apache/james/util/ZeroedInputStream.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/main/java/org/apache/james/util/ZeroedInputStream.java b/server/container/util/src/main/java/org/apache/james/util/ZeroedInputStream.java
new file mode 100644
index 0000000..f1583c7
--- /dev/null
+++ b/server/container/util/src/main/java/org/apache/james/util/ZeroedInputStream.java
@@ -0,0 +1,42 @@
+/****************************************************************
+ * 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.io.InputStream;
+
+public class ZeroedInputStream extends InputStream {
+    public static final int RETURNED_VALUE = 0;
+
+    private final int max;
+    private int pos;
+
+    public ZeroedInputStream(int max) {
+        this.max = max;
+        this.pos = 0;
+    }
+
+    @Override
+    public int read() {
+        if (pos < max) {
+            pos++;
+            return RETURNED_VALUE;
+        }
+        return -1;
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/c3d7956e/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
index 306c070..2baa59a 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
@@ -57,6 +57,7 @@ import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.model.Attachment;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
+import org.apache.james.util.ZeroedInputStream;
 import org.hamcrest.Matchers;
 import org.junit.After;
 import org.junit.Before;
@@ -64,6 +65,7 @@ import org.junit.Ignore;
 import org.junit.Test;
 
 import com.google.common.base.Charsets;
+import com.google.common.io.ByteStreams;
 import com.jayway.awaitility.Awaitility;
 import com.jayway.awaitility.Duration;
 import com.jayway.awaitility.core.ConditionFactory;
@@ -75,6 +77,7 @@ import com.jayway.restassured.specification.ResponseSpecification;
 
 public abstract class SetMessagesMethodTest {
 
+    private static final int _1MB = 1024*1024;
     private static final String NAME = "[0][0]";
     private static final String ARGUMENTS = "[0][1]";
     private static final String SECOND_NAME = "[1][0]";
@@ -1696,7 +1699,7 @@ public abstract class SetMessagesMethodTest {
         jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "sent");
 
         Attachment attachment = Attachment.builder()
-                .bytes("attachment".getBytes(Charsets.UTF_8))
+                .bytes(ByteStreams.toByteArray(new ZeroedInputStream(_1MB)))
                 .type("application/octet-stream")
                 .build();
         uploadAttachment(attachment);

http://git-wip-us.apache.org/repos/asf/james-project/blob/c3d7956e/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/UploadStepdefs.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/UploadStepdefs.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/UploadStepdefs.java
index 74dbcc0..d05e90d 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/UploadStepdefs.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/UploadStepdefs.java
@@ -20,7 +20,6 @@
 package org.apache.james.jmap.methods.integration.cucumber;
 
 import java.io.BufferedInputStream;
-import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 
@@ -32,6 +31,7 @@ import org.apache.http.client.fluent.Executor;
 import org.apache.http.client.fluent.Request;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.james.jmap.api.access.AccessToken;
+import org.apache.james.util.ZeroedInputStream;
 
 import com.google.common.base.Charsets;
 import com.jayway.jsonpath.DocumentContext;
@@ -148,25 +148,4 @@ public class UploadStepdefs {
         response = request.execute().returnResponse();
         httpAuthorizedStatus();
     }
-
-    public static class ZeroedInputStream extends InputStream {
-        public static final int RETURNED_VALUE = 0;
-
-        private final int max;
-        private int pos;
-
-        public ZeroedInputStream(int max) {
-            this.max = max;
-            this.pos = 0;
-        }
-
-        @Override
-        public int read() {
-            if (pos < max) {
-                pos++;
-                return RETURNED_VALUE;
-            }
-            return -1;
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/c3d7956e/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
index 124658c..b541926 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java
@@ -56,6 +56,7 @@ import org.apache.james.jmap.send.MailFactory;
 import org.apache.james.jmap.send.MailMetadata;
 import org.apache.james.jmap.send.MailSpool;
 import org.apache.james.jmap.utils.SystemMailboxesProvider;
+import org.apache.james.lifecycle.api.LifecycleUtil;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.exception.AttachmentNotFoundException;
 import org.apache.james.mailbox.exception.MailboxException;
@@ -361,8 +362,12 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor {
 
     private void sendMessage(MailboxMessage mailboxMessage, Message jmapMessage, MailboxSession session) throws MessagingException {
         Mail mail = buildMessage(mailboxMessage, jmapMessage);
-        MailMetadata metadata = new MailMetadata(jmapMessage.getId(), session.getUser().getUserName());
-        mailSpool.send(mail, metadata);
+        try {
+            MailMetadata metadata = new MailMetadata(jmapMessage.getId(), session.getUser().getUserName());
+            mailSpool.send(mail, metadata);
+        } finally {
+            LifecycleUtil.dispose(mail);
+        }
     }
 
     private Mail buildMessage(MailboxMessage mailboxMessage, Message jmapMessage) throws MessagingException {


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