Coverage Report - org.mule.transport.email.MailUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MailUtils
0%
0/54
0%
0/38
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.email;
 8  
 
 9  
 import org.mule.config.i18n.CoreMessages;
 10  
 import org.mule.util.StringUtils;
 11  
 
 12  
 import java.io.IOException;
 13  
 import java.io.UnsupportedEncodingException;
 14  
 import java.util.Map;
 15  
 
 16  
 import javax.mail.Address;
 17  
 import javax.mail.MessagingException;
 18  
 import javax.mail.Multipart;
 19  
 import javax.mail.Part;
 20  
 import javax.mail.internet.AddressException;
 21  
 import javax.mail.internet.InternetAddress;
 22  
 import javax.mail.internet.MimeUtility;
 23  
 
 24  
 /**
 25  
  * Contains javax.mail helpers.
 26  
  */
 27  0
 public class MailUtils
 28  
 {
 29  
     public static String internetAddressesToString(InternetAddress[] addresses)
 30  
     {
 31  0
         if (addresses == null || addresses.length == 0)
 32  
         {
 33  0
             return StringUtils.EMPTY;
 34  
         }
 35  
 
 36  0
         StringBuffer buf = new StringBuffer(80);
 37  
 
 38  0
         for (int i = 0; i < addresses.length; i++)
 39  
         {
 40  0
             InternetAddress address = addresses[i];
 41  0
             buf.append(address.getAddress());
 42  
             // all except the last one
 43  0
             if (i < addresses.length - 1)
 44  
             {
 45  0
                 buf.append(", ");
 46  
             }
 47  
         }
 48  
 
 49  0
         return buf.toString();
 50  
     }
 51  
 
 52  
     public static String internetAddressesToString(InternetAddress address)
 53  
     {
 54  0
         return internetAddressesToString(new InternetAddress[]{address});
 55  
     }
 56  
 
 57  
     public static String mailAddressesToString(Address[] addresses)
 58  
     {
 59  0
         if (addresses == null || addresses.length == 0)
 60  
         {
 61  0
             return StringUtils.EMPTY;
 62  
         }
 63  
 
 64  0
         StringBuffer buf = new StringBuffer(80);
 65  
 
 66  0
         for (int i = 0; i < addresses.length; i++)
 67  
         {
 68  0
             Address address = addresses[i];
 69  0
             buf.append(address.toString());
 70  
             // all except the last one
 71  0
             if (i < addresses.length - 1)
 72  
             {
 73  0
                 buf.append(", ");
 74  
             }
 75  
         }
 76  
 
 77  0
         return buf.toString();
 78  
     }
 79  
 
 80  
     public static String mailAddressesToString(Address address)
 81  
     {
 82  0
         return mailAddressesToString(new Address[]{address});
 83  
     }
 84  
 
 85  
     public static InternetAddress[] stringToInternetAddresses(String address) throws AddressException
 86  
     {
 87  0
         if (StringUtils.isNotBlank(address))
 88  
         {
 89  0
             return InternetAddress.parse(address, false);
 90  
         }
 91  
         else
 92  
         {
 93  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull("Email address").toString());
 94  
         }
 95  
     }
 96  
 
 97  
     public static void getAttachments(Multipart content, Map<String, Part> attachments) throws MessagingException, IOException
 98  
     {
 99  0
         int x = 0;
 100  0
         for(int i = 0; i < content.getCount(); i++)
 101  
         {
 102  0
             Part part = content.getBodyPart(i);
 103  0
             if (part.getContentType().indexOf("multipart/mixed") > -1)
 104  
             {
 105  0
                 Multipart m = (Multipart) part.getContent();
 106  0
                 getAttachments(m, attachments);
 107  0
             }
 108  
             else
 109  
             {
 110  
                 String key;
 111  0
                 if (StringUtils.isNotEmpty(part.getDescription()))
 112  
                 {
 113  0
                     key = part.getDescription();
 114  
                 }
 115  0
                 else if (StringUtils.isNotEmpty(part.getFileName()))
 116  
                 {
 117  
                     try
 118  
                     {
 119  0
                         key = MimeUtility.decodeText(part.getFileName());
 120  
                     }
 121  0
                     catch (UnsupportedEncodingException e)
 122  
                     {
 123  0
                         key = part.getFileName();
 124  0
                     }
 125  
                 }
 126  0
                 else if (StringUtils.isNotEmpty(part.getDisposition()))
 127  
                 {
 128  0
                     key = part.getDisposition();
 129  
                 }
 130  
                 else
 131  
                 {
 132  0
                     key = String.valueOf(x++);
 133  
                 }
 134  
 
 135  0
                 key = getAttachmentName(key, attachments);
 136  
 
 137  0
                 attachments.put(key, part);
 138  
             }
 139  
         }
 140  0
     }
 141  
 
 142  
 
 143  
     /**
 144  
      * Check whether an attachment with the same name already exists,
 145  
      * using a counter for override protection.
 146  
      *
 147  
      * @param key A attachment name
 148  
      * @param attachments Map with attachments
 149  
      * @return attachment name
 150  
      */
 151  
     public static String getAttachmentName(String key, Map<String, Part> attachments)
 152  
     {
 153  0
         if (attachments.containsKey(key))
 154  
         {
 155  0
             int x = 0;
 156  0
             while (attachments.containsKey(x + "_" + key))
 157  
             {
 158  0
                 x++;
 159  
             }
 160  0
             return x + "_" + key;
 161  
 
 162  
         } else
 163  
         {
 164  0
             return key;
 165  
         }
 166  
     }
 167  
 
 168  
     /**
 169  
      * Check whether a property name has the format associated with a list
 170  
      * of header values
 171  
      *
 172  
      * @param name A property name
 173  
      * @return true if the name is associated with a list of header values
 174  
      *         (more exactly, if it starts with HEADER_LIST_PREFIX, which gives an
 175  
      *         invalid header name according to RFC822).
 176  
      */
 177  
     public static boolean isListHeader(String name)
 178  
     {
 179  0
         return null != name && name.startsWith(MailMuleMessageFactory.HEADER_LIST_PREFIX);
 180  
     }
 181  
 
 182  
     /**
 183  
      * Convert a header name to the property name associated with a list of
 184  
      * header values (ie prepend the prefix)
 185  
      *
 186  
      * @param header A header name
 187  
      * @return The associated list property name (ie with HEADER_LIST_PREFIX prepended)
 188  
      */
 189  
     public static String toListHeader(String header)
 190  
     {
 191  0
         if (isListHeader(header))
 192  
         {
 193  0
             return header;
 194  
         }
 195  
         else
 196  
         {
 197  0
             return MailMuleMessageFactory.HEADER_LIST_PREFIX + header;
 198  
         }
 199  
     }
 200  
 }