Coverage Report - org.mule.transport.email.MailMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
MailMessageAdapter
22%
6/27
10%
1/10
2.667
 
 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  50
         super(object);
 43  50
     }
 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  50
         Object content = message.getContent();
 52  
 
 53  50
         if (content instanceof Multipart)
 54  
         {
 55  0
             TreeMap attachments = new TreeMap();
 56  0
             MailUtils.getAttachments((Multipart)content, attachments);
 57  
 
 58  0
             logger.debug("Received Multipart message");
 59  0
             int i = 0;
 60  0
             for (Iterator iterator = attachments.entrySet().iterator(); iterator.hasNext();i++)
 61  
             {
 62  0
                 Map.Entry entry = (Map.Entry) iterator.next();
 63  0
                 Part part = (Part)entry.getValue();
 64  0
                 String name = entry.getKey().toString();
 65  0
                 if(i==0)
 66  
                 {
 67  0
                     setMessage(part);
 68  
                 }
 69  
                 else
 70  
                 {
 71  0
                     addAttachment(name, part.getDataHandler());
 72  0
                     addAttachmentHeaders(name, part);
 73  
                 }
 74  
 
 75  
             }
 76  0
         }
 77  
         else
 78  
         {
 79  50
             setMessage(message);
 80  
         }
 81  50
     }
 82  
 
 83  
     protected void addAttachmentHeaders(String name, Part part) throws javax.mail.MessagingException
 84  
     {
 85  0
         Map headers = new HashMap(4);
 86  0
         for (Enumeration e = part.getAllHeaders(); e.hasMoreElements();)
 87  
         {
 88  0
             Header h = (Header)e.nextElement();
 89  0
             headers.put(h.getName(), h.getValue());
 90  0
         }
 91  0
         if (headers.size() > 0)
 92  
         {
 93  0
             setProperty(name + ATTACHMENT_HEADERS_PROPERTY_POSTFIX, headers);
 94  
         }
 95  0
     }
 96  
 
 97  
 }