You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2009/04/01 17:27:31 UTC

svn commit: r760920 - in /camel/branches/camel-1.x: ./ components/camel-mail/src/main/java/org/apache/camel/component/mail/ components/camel-mail/src/test/java/org/apache/camel/component/mail/ components/camel-xmpp/src/test/resources/

Author: ningjiang
Date: Wed Apr  1 15:27:29 2009
New Revision: 760920

URL: http://svn.apache.org/viewvc?rev=760920&view=rev
Log:
Merged revisions 760909 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r760909 | ningjiang | 2009-04-01 23:00:20 +0800 (Wed, 01 Apr 2009) | 1 line
  
  CAMEL-1506 CAMEL-1507 Applied patch with thanks to Ryan
........

Added:
    camel/branches/camel-1.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMultipartAlternativeTest.java
      - copied unchanged from r760909, camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMultipartAlternativeTest.java
Modified:
    camel/branches/camel-1.x/   (props changed)
    camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
    camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
    camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
    camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java
    camel/branches/camel-1.x/components/camel-xmpp/src/test/resources/   (props changed)

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Apr  1 15:27:29 2009
@@ -1 +1 @@
-/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890
+/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909

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

Modified: camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?rev=760920&r1=760919&r2=760920&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java (original)
+++ camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java Wed Apr  1 15:27:29 2009
@@ -85,15 +85,20 @@
             mimeMessage.setFrom(new InternetAddress(from));
         }
 
-        if (exchange.getIn().hasAttachments()) {
-            appendAttachmentsFromCamel(mimeMessage, exchange.getIn(), endpoint.getConfiguration());
+        // if there is an alternativebody provided, set up a mime multipart alternative message
+        if (hasAlternativeBody(endpoint.getConfiguration(), exchange.getIn())) {
+            createMultipartAlternativeMessage(mimeMessage, exchange.getIn(), endpoint.getConfiguration()); 
         } else {
-            if ("text/html".equals(endpoint.getConfiguration().getContentType())) {
-                DataSource ds = new ByteArrayDataSource(exchange.getIn().getBody(String.class), "text/html");
-                mimeMessage.setDataHandler(new DataHandler(ds));
+            if (exchange.getIn().hasAttachments()) {
+                appendAttachmentsFromCamel(mimeMessage, exchange.getIn(), endpoint.getConfiguration());
             } else {
-                // its just text/plain
-                mimeMessage.setText(exchange.getIn().getBody(String.class));
+                if ("text/html".equals(endpoint.getConfiguration().getContentType())) {                  
+                    DataSource ds = new ByteArrayDataSource(exchange.getIn().getBody(String.class), "text/html");
+                    mimeMessage.setDataHandler(new DataHandler(ds));
+                } else {
+                    // its just text/plain
+                    mimeMessage.setText(exchange.getIn().getBody(String.class));
+                }
             }
         }
     }
@@ -196,16 +201,22 @@
     protected void appendAttachmentsFromCamel(MimeMessage mimeMessage, org.apache.camel.Message camelMessage,
                                               MailConfiguration configuration)
         throws MessagingException {
+        
+        // Put parts in message
+        mimeMessage.setContent(createMixedMultipartAttachments(camelMessage, configuration));
+    }
 
-        // Create a Multipart
-        MimeMultipart multipart = new MimeMultipart();
-
+    private MimeMultipart createMixedMultipartAttachments(org.apache.camel.Message camelMessage, MailConfiguration configuration) throws MessagingException {
         // fill the body with text
+        MimeMultipart multipart = new MimeMultipart();
         multipart.setSubType("mixed");
-        MimeBodyPart textBodyPart = new MimeBodyPart();
-        textBodyPart.setContent(camelMessage.getBody(String.class), configuration.getContentType());
-        multipart.addBodyPart(textBodyPart);
+        addBodyToMultipart(camelMessage, configuration, multipart);
+        String partDisposition = configuration.isUseInlineAttachments() ?  Part.INLINE : Part.ATTACHMENT;
+        addAttachmentsToMultipart(camelMessage, multipart, partDisposition);
+        return multipart;
+    }
 
+    protected void addAttachmentsToMultipart(org.apache.camel.Message camelMessage, MimeMultipart multipart, String partDisposition) throws MessagingException {
         for (Map.Entry<String, DataHandler> entry : camelMessage.getAttachments().entrySet()) {
             String attachmentFilename = entry.getKey();
             DataHandler handler = entry.getValue();
@@ -215,20 +226,66 @@
                     BodyPart messageBodyPart = new MimeBodyPart();
                     // Set the data handler to the attachment
                     messageBodyPart.setDataHandler(handler);
+                    
+                    if (attachmentFilename.toLowerCase().startsWith("cid:")) {
+                    // add a Content-ID header to the attachment
+                        messageBodyPart.addHeader("Content-ID", attachmentFilename.substring(4));
+                    }
                     // Set the filename
                     messageBodyPart.setFileName(attachmentFilename);
                     // Set Disposition
-                    messageBodyPart.setDisposition(Part.ATTACHMENT);
+                    messageBodyPart.setDisposition(partDisposition);
                     // Add part to multipart
                     multipart.addBodyPart(messageBodyPart);
                 }
             }
         }
+    }
+
+    protected void createMultipartAlternativeMessage(MimeMessage mimeMessage, org.apache.camel.Message camelMessage, MailConfiguration configuration)
+        throws MessagingException { 
+
+        MimeMultipart multipartAlternative = new MimeMultipart("alternative");
+        mimeMessage.setContent(multipartAlternative);
+
+        BodyPart plainText = new MimeBodyPart();
+        plainText.setText(getAlternativeBody(configuration, camelMessage));
+        multipartAlternative.addBodyPart(plainText);
+
+        // if there are no attachments, add the body to the same mulitpart message
+        if (!camelMessage.hasAttachments()) {
+            addBodyToMultipart(camelMessage, configuration, multipartAlternative);
+        } else {
+            // if there are attachments, but they aren't set to be inline, add them to
+            // treat them as normal. It will append a multipart-mixed with the attachments and the
+            // body text
+            if (!configuration.isUseInlineAttachments()) {
+                BodyPart mixedAttachments = new MimeBodyPart();
+                mixedAttachments.setContent(createMixedMultipartAttachments(camelMessage, configuration));
+                multipartAlternative.addBodyPart(mixedAttachments);
+                //appendAttachmentsFromCamel(mimeMessage, camelMessage, configuration);
+            } else { // if the attachments are set to be inline, attach them as inline attachments
+                MimeMultipart multipartRelated = new MimeMultipart("related");
+                BodyPart related = new MimeBodyPart();
+
+                related.setContent(multipartRelated);
+                multipartAlternative.addBodyPart(related);
+
+                addBodyToMultipart(camelMessage, configuration, multipartRelated);
+
+                addAttachmentsToMultipart(camelMessage, multipartRelated, Part.INLINE);
+            }
+        }
 
-        // Put parts in message
-        mimeMessage.setContent(multipart);
     }
 
+    protected void addBodyToMultipart(org.apache.camel.Message camelMessage, MailConfiguration configuration, MimeMultipart activeMultipart) throws MessagingException {
+        BodyPart bodyMessage = new MimeBodyPart();
+        bodyMessage.setContent(camelMessage.getBody(String.class), configuration.getContentType());
+        activeMultipart.addBodyPart(bodyMessage);
+    }
+
+
     /**
      * Strategy to allow filtering of attachments which are put on the Mail message
      */
@@ -280,6 +337,15 @@
         return false;
     }
 
+    protected static boolean hasAlternativeBody(MailConfiguration configuration, org.apache.camel.Message camelMessage) {
+        return getAlternativeBody(configuration, camelMessage) != null;
+    }
+
+    protected static String getAlternativeBody(MailConfiguration configuration, org.apache.camel.Message camelMessage) {
+        String alternativeBodyHeader = configuration.getAlternateBodyHeader();
+        return camelMessage.getHeader(alternativeBodyHeader, java.lang.String.class);
+    }
+
     /**
      * Is the given key a mime message recipient header (To, CC or BCC)
      */

Modified: camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java?rev=760920&r1=760919&r2=760920&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java (original)
+++ camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java Wed Apr  1 15:27:29 2009
@@ -37,6 +37,7 @@
 
     public static final String DEFAULT_FOLDER_NAME = "INBOX";
     public static final String DEFAULT_FROM = "camel@localhost";
+    public static final String DEFAULT_ALTERNATE_BODY_HEADER = "mail_alternateBody";
     public static final long DEFAULT_CONNECTION_TIMEOUT = 30000L;
 
     private Properties javaMailProperties;
@@ -59,6 +60,8 @@
     private long connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
     private boolean dummyTrustManager;
     private String contentType = "text/plain";
+    private String alternateBodyHeader = DEFAULT_ALTERNATE_BODY_HEADER;
+    private boolean useInlineAttachments;
 
     public MailConfiguration() {
     }
@@ -403,4 +406,20 @@
     public void setContentType(String contentType) {
         this.contentType = contentType;
     }
+
+    public String getAlternateBodyHeader() {
+        return alternateBodyHeader;
+    }
+
+    public void setAlternateBodyHeader(String alternateBodyHeader) {
+        this.alternateBodyHeader = alternateBodyHeader;
+    }
+
+    public boolean isUseInlineAttachments() {
+        return useInlineAttachments;
+    }
+
+    public void setUseInlineAttachments(boolean useInlineAttachments) {
+        this.useInlineAttachments = useInlineAttachments;
+    }
 }

Modified: camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java?rev=760920&r1=760919&r2=760920&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java (original)
+++ camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java Wed Apr  1 15:27:29 2009
@@ -140,22 +140,31 @@
 
         Object content = message.getContent();
         if (content instanceof Multipart) {
-            // mail with attachment
-            Multipart mp = (Multipart)content;
-            for (int i = 0; i < mp.getCount(); i++) {
-                Part part = mp.getBodyPart(i);
+            extractFromMultipart((Multipart)content, map);
+        }
+    }
+    
+    protected static void extractFromMultipart(Multipart mp, Map<String, DataHandler> map) 
+        throws javax.mail.MessagingException, IOException {
+
+        for (int i = 0; i < mp.getCount(); i++) {
+            Part part = mp.getBodyPart(i);           
+            if (part.isMimeType("multipart/*")) {
+                extractFromMultipart((Multipart)part.getContent(), map);
+            } else {
                 String disposition = part.getDisposition();
                 if (disposition != null) {
                     if (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE)) {
                         // only add named attachments
                         if (part.getFileName() != null) {
-                            // Parts marked with a disposition of Part.ATTACHMENT are clearly attachments
+                            // Parts marked with a disposition of Part.ATTACHMENT
+                            // are clearly attachments
                             CollectionHelper.appendValue(map, part.getFileName(), part.getDataHandler());
                         }
                     }
                 }
             }
         }
-    }
+    }    
 
 }

Modified: camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java?rev=760920&r1=760919&r2=760920&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java (original)
+++ camel/branches/camel-1.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java Wed Apr  1 15:27:29 2009
@@ -135,4 +135,5 @@
 
     }
 
+   
 }

Propchange: camel/branches/camel-1.x/components/camel-xmpp/src/test/resources/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Apr  1 15:27:29 2009
@@ -1 +1 @@
-/camel/trunk/components/camel-xmpp/src/test/resources:757693,757743,757865,758539,758563,758617,758692,758990,759362,759887,759931,760890
+/camel/trunk/components/camel-xmpp/src/test/resources:757693,757743,757865,758539,758563,758617,758692,758990,759362,759887,759931,760890,760909