Coverage Report - org.mule.transport.email.MailUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MailUtils
0%
0/44
0%
0/34
3.25
 
 1  
 /*
 2  
  * $Id: MailUtils.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.config.i18n.CoreMessages;
 14  
 import org.mule.util.StringUtils;
 15  
 
 16  
 import java.io.IOException;
 17  
 import java.util.Map;
 18  
 
 19  
 import javax.mail.Address;
 20  
 import javax.mail.MessagingException;
 21  
 import javax.mail.Multipart;
 22  
 import javax.mail.Part;
 23  
 import javax.mail.internet.AddressException;
 24  
 import javax.mail.internet.InternetAddress;
 25  
 
 26  
 /**
 27  
  * Contains javax.mail helpers.
 28  
  */
 29  0
 public class MailUtils
 30  
 {
 31  
     public static String internetAddressesToString(InternetAddress[] addresses)
 32  
     {
 33  0
         if (addresses == null || addresses.length == 0)
 34  
         {
 35  0
             return StringUtils.EMPTY;
 36  
         }
 37  
 
 38  0
         StringBuffer buf = new StringBuffer(80);
 39  
 
 40  0
         for (int i = 0; i < addresses.length; i++)
 41  
         {
 42  0
             InternetAddress address = addresses[i];
 43  0
             buf.append(address.getAddress());
 44  
             // all except the last one
 45  0
             if (i < addresses.length - 1)
 46  
             {
 47  0
                 buf.append(", ");
 48  
             }
 49  
         }
 50  
 
 51  0
         return buf.toString();
 52  
     }
 53  
 
 54  
     public static String internetAddressesToString(InternetAddress address)
 55  
     {
 56  0
         return internetAddressesToString(new InternetAddress[]{address});
 57  
     }
 58  
 
 59  
     public static String mailAddressesToString(Address[] addresses)
 60  
     {
 61  0
         if (addresses == null || addresses.length == 0)
 62  
         {
 63  0
             return StringUtils.EMPTY;
 64  
         }
 65  
 
 66  0
         StringBuffer buf = new StringBuffer(80);
 67  
 
 68  0
         for (int i = 0; i < addresses.length; i++)
 69  
         {
 70  0
             Address address = addresses[i];
 71  0
             buf.append(address.toString());
 72  
             // all except the last one
 73  0
             if (i < addresses.length - 1)
 74  
             {
 75  0
                 buf.append(", ");
 76  
             }
 77  
         }
 78  
 
 79  0
         return buf.toString();
 80  
     }
 81  
 
 82  
     public static String mailAddressesToString(Address address)
 83  
     {
 84  0
         return mailAddressesToString(new Address[]{address});
 85  
     }
 86  
 
 87  
     public static InternetAddress[] stringToInternetAddresses(String address) throws AddressException
 88  
     {
 89  0
         if (StringUtils.isNotBlank(address))
 90  
         {
 91  0
             return InternetAddress.parse(address, false);
 92  
         }
 93  
         else
 94  
         {
 95  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull("Email address").toString());
 96  
         }
 97  
     }
 98  
 
 99  
     public static void getAttachments(Multipart content, Map attachments) throws MessagingException, IOException
 100  
     {
 101  0
         int x = 0;
 102  0
         for(int i=0; i < content.getCount(); i++)
 103  
         {
 104  0
             Part p = content.getBodyPart(i);
 105  0
             if(p.getContentType().indexOf("multipart/mixed") > -1)
 106  
             {
 107  0
                 Multipart m = (Multipart)p.getContent();
 108  0
                 getAttachments(m, attachments);
 109  0
             }
 110  
             else
 111  
             {
 112  
                 String key;
 113  0
                 if(StringUtils.isNotEmpty(p.getDescription()))
 114  
                 {
 115  0
                     key = p.getDescription();
 116  
                 }
 117  0
                 else if(StringUtils.isNotEmpty(p.getFileName()))
 118  
                 {
 119  0
                     key = p.getFileName();
 120  
                 }
 121  0
                 else if(StringUtils.isNotEmpty(p.getDisposition()))
 122  
                 {
 123  0
                     key = p.getDisposition();
 124  
                 }
 125  
                 else
 126  
                 {
 127  0
                     key = String.valueOf(x++);
 128  
                 }
 129  0
                 attachments.put(key, p);
 130  
             }
 131  
         }
 132  0
     }
 133  
 
 134  
     /**
 135  
      * Check whether a property name has the format associated with a list
 136  
      * of header values
 137  
      *
 138  
      * @param name A property name
 139  
      * @return true if the name is associated with a list of header values
 140  
      *         (more exactly, if it starts with HEADER_LIST_PREFIX, which gives an
 141  
      *         invalid header name according to RFC822).
 142  
      */
 143  
     public static boolean isListHeader(String name)
 144  
     {
 145  0
         return null != name && name.startsWith(MailMuleMessageFactory.HEADER_LIST_PREFIX);
 146  
     }
 147  
 
 148  
     /**
 149  
      * Convert a header name to the property name associated with a list of
 150  
      * header values (ie prepend the prefix)
 151  
      *
 152  
      * @param header A header name
 153  
      * @return The associated list property name (ie with HEADER_LIST_PREFIX prepended)
 154  
      */
 155  
     public static String toListHeader(String header)
 156  
     {
 157  0
         if (isListHeader(header))
 158  
         {
 159  0
             return header;
 160  
         }
 161  
         else
 162  
         {
 163  0
             return MailMuleMessageFactory.HEADER_LIST_PREFIX + header;
 164  
         }
 165  
     }
 166  
 }