View Javadoc

1   /*
2    * $Id: MailMessageAdapter.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.email;
12  
13  import org.mule.api.MessagingException;
14  import org.mule.transport.AbstractMessageAdapter;
15  
16  import java.util.Enumeration;
17  import java.util.HashMap;
18  import java.util.Iterator;
19  import java.util.Map;
20  import java.util.TreeMap;
21  
22  import javax.mail.Header;
23  import javax.mail.Message;
24  import javax.mail.Multipart;
25  import javax.mail.Part;
26  
27  /**
28   * <code>MailMessageAdapter</code> is a wrapper for a javax.mail.Message that 
29   * separates multi-part mail messages, storing all but the first part as attachments 
30   * to the underlying {@link AbstractMessageAdapter}.  Alternatively, you can use
31   * {@link SimpleMailMessageAdapter}, which stores the message as a single
32   * entity.
33   */
34  public class MailMessageAdapter extends SimpleMailMessageAdapter
35  {
36  
37      private static final long serialVersionUID = -6013198455030918360L;
38      public static final String ATTACHMENT_HEADERS_PROPERTY_POSTFIX = "Headers";
39  
40      public MailMessageAdapter(Object object) throws MessagingException
41      {
42          super(object);
43      }
44  
45      /**
46       * Store only the first body part directly; add further parts as attachments.
47       */
48      // @Override
49      protected void handleMessage(Message message) throws Exception
50      {
51          Object content = message.getContent();
52  
53          if (content instanceof Multipart)
54          {
55              TreeMap attachments = new TreeMap();
56              MailUtils.getAttachments((Multipart)content, attachments);
57  
58              logger.debug("Received Multipart message");
59              int i = 0;
60              for (Iterator iterator = attachments.entrySet().iterator(); iterator.hasNext();i++)
61              {
62                  Map.Entry entry = (Map.Entry) iterator.next();
63                  Part part = (Part)entry.getValue();
64                  String name = entry.getKey().toString();
65                  if(i==0)
66                  {
67                      setMessage(part);
68                  }
69                  else
70                  {
71                      addAttachment(name, part.getDataHandler());
72                      addAttachmentHeaders(name, part);
73                  }
74  
75              }
76          }
77          else
78          {
79              setMessage(message);
80          }
81      }
82  
83      protected void addAttachmentHeaders(String name, Part part) throws javax.mail.MessagingException
84      {
85          Map headers = new HashMap(4);
86          for (Enumeration e = part.getAllHeaders(); e.hasMoreElements();)
87          {
88              Header h = (Header)e.nextElement();
89              headers.put(h.getName(), h.getValue());
90          }
91          if (headers.size() > 0)
92          {
93              setProperty(name + ATTACHMENT_HEADERS_PROPERTY_POSTFIX, headers);
94          }
95      }
96  
97  }