Repository: james-project
Updated Branches:
  refs/heads/master 5883efbfd -> 2e6617f0b


JAMES-2384 When there is no body, getting a NullPointerException


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

Branch: refs/heads/master
Commit: 33ca2a62c6c7f1af8738ef0351f5c90dad8aaec3
Parents: 5883efb
Author: Simon Levesque <[email protected]>
Authored: Mon Apr 23 16:56:05 2018 -0400
Committer: benwa <[email protected]>
Committed: Fri May 4 17:52:36 2018 +0700

----------------------------------------------------------------------
 .../mail/model/openjpa/JPAMailboxMessage.java   | 13 ++++++
 .../model/openjpa/JPAMailboxMessageTest.java    | 45 ++++++++++++++++++++
 2 files changed, 58 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/33ca2a62/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMailboxMessage.java
----------------------------------------------------------------------
diff --git 
a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMailboxMessage.java
 
b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMailboxMessage.java
index 0041018..816380d 100644
--- 
a/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMailboxMessage.java
+++ 
b/mailbox/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMailboxMessage.java
@@ -39,10 +39,14 @@ import org.apache.james.mailbox.jpa.mail.model.JPAMailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
 
+import com.google.common.annotations.VisibleForTesting;
+
 @Entity(name = "MailboxMessage")
 @Table(name = "JAMES_MAIL")
 public class JPAMailboxMessage extends AbstractJPAMailboxMessage {
 
+    private static final byte[] EMPTY_BODY = new byte[] {};
+
     /** The value for the body field. Lazy loaded */
     /** We use a max length to represent 1gb data. Thats prolly overkill, but 
who knows */
     @Basic(optional = false, fetch = FetchType.LAZY)
@@ -61,6 +65,12 @@ public class JPAMailboxMessage extends 
AbstractJPAMailboxMessage {
         
     }
 
+    @VisibleForTesting
+    protected JPAMailboxMessage(byte[] header, byte[] body) {
+        this.header = header;
+        this.body = body;
+    }
+
     public JPAMailboxMessage(JPAMailbox mailbox, Date internalDate, int size, 
Flags flags, SharedInputStream content, int bodyStartOctet, PropertyBuilder 
propertyBuilder) throws MailboxException {
         super(mailbox, internalDate, flags, size, bodyStartOctet, 
propertyBuilder);
         try {
@@ -91,6 +101,9 @@ public class JPAMailboxMessage extends 
AbstractJPAMailboxMessage {
 
     @Override
     public InputStream getBodyContent() throws IOException {
+        if (body == null) {
+            return new ByteArrayInputStream(EMPTY_BODY);
+        }
         return new ByteArrayInputStream(body);
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/33ca2a62/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMailboxMessageTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMailboxMessageTest.java
 
b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMailboxMessageTest.java
new file mode 100644
index 0000000..3c598c7
--- /dev/null
+++ 
b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/mail/model/openjpa/JPAMailboxMessageTest.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.mailbox.jpa.mail.model.openjpa;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+
+public class JPAMailboxMessageTest {
+
+    /**
+     * Even though there should never be a null body, it does happen. See 
JAMES-2384
+     */
+    @Test
+    public void getFullContentShouldReturnOriginalContentWhenBodyFieldIsNull() 
throws Exception {
+
+        // Prepare the message
+        byte[] content = "Subject: the null 
message".getBytes(StandardCharsets.UTF_8);
+        JPAMailboxMessage message = new JPAMailboxMessage(content, null);
+
+        // Get and check
+        
assertThat(IOUtils.toByteArray(message.getFullContent())).containsExactly(content);
+
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to