Author: davsclaus
Date: Thu Mar 21 10:24:27 2013
New Revision: 1459215

URL: http://svn.apache.org/r1459215
Log:
CAMEL-6154: Fixed potential NPE in mail binding if mail content is empty.

Added:
    
camel/branches/camel-2.10.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPollEnrichNoMailTest.java
      - copied unchanged from r1459214, 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPollEnrichNoMailTest.java
Modified:
    camel/branches/camel-2.10.x/   (props changed)
    
camel/branches/camel-2.10.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
    
camel/branches/camel-2.10.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPollEnrichTest.java

Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1459214

Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: 
camel/branches/camel-2.10.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?rev=1459215&r1=1459214&r2=1459215&view=diff
==============================================================================
--- 
camel/branches/camel-2.10.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
 (original)
+++ 
camel/branches/camel-2.10.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
 Thu Mar 21 10:24:27 2013
@@ -219,14 +219,20 @@ public class MailBinding {
 
         String contentType = determineContentType(configuration, exchange);
 
-        LOG.trace("Using Content-Type {} for BodyPart: {}", contentType, part);
+        if (contentType != null) {
+            LOG.trace("Using Content-Type {} for BodyPart: {}", contentType, 
part);
 
-        // always store content in a byte array data store to avoid various 
content type and charset issues
-        DataSource ds = new 
ByteArrayDataSource(exchange.getIn().getBody(String.class), contentType);
-        part.setDataHandler(new DataHandler(ds));
+            // always store content in a byte array data store to avoid 
various content type and charset issues
+            String data = 
exchange.getContext().getTypeConverter().tryConvertTo(String.class, 
exchange.getIn().getBody());
+            // use empty data if the body was null for some reason (otherwise 
there is a NPE)
+            data = data != null ? data : "";
 
-        // set the content type header afterwards
-        part.setHeader("Content-Type", contentType);
+            DataSource ds = new ByteArrayDataSource(data, contentType);
+            part.setDataHandler(new DataHandler(ds));
+
+            // set the content type header afterwards
+            part.setHeader("Content-Type", contentType);
+        }
 
         return contentType;
     }

Modified: 
camel/branches/camel-2.10.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPollEnrichTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPollEnrichTest.java?rev=1459215&r1=1459214&r2=1459215&view=diff
==============================================================================
--- 
camel/branches/camel-2.10.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPollEnrichTest.java
 (original)
+++ 
camel/branches/camel-2.10.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPollEnrichTest.java
 Thu Mar 21 10:24:27 2013
@@ -28,7 +28,7 @@ import org.junit.Test;
 import org.jvnet.mock_javamail.Mailbox;
 
 /**
- * Unit test for a special corner case with fetchSize=0
+ * Unit test with poll enrich
  */
 public class MailPollEnrichTest extends CamelTestSupport {
 
@@ -51,6 +51,19 @@ public class MailPollEnrichTest extends 
         mock.assertIsSatisfied();
     }
 
+    @Test
+    public void testPollEnrichNullBody() throws Exception {
+        Mailbox mailbox = Mailbox.get("bill@localhost");
+        assertEquals(5, mailbox.size());
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Message 0");
+
+        template.sendBody("direct:start", null);
+
+        mock.assertIsSatisfied();
+    }
+
     private void prepareMailbox() throws Exception {
         // connect to mailbox
         Mailbox.clearAll();


Reply via email to