Coverage Report - org.mule.providers.email.MailUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MailUtils
0%
0/39
0%
0/12
3.667
 
 1  
 /*
 2  
  * $Id: MailUtils.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.providers.email;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.util.StringUtils;
 15  
 
 16  
 import java.util.Map;
 17  
 import java.io.IOException;
 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  
 
 32  
     public static String internetAddressesToString(InternetAddress[] addresses)
 33  
     {
 34  0
         if (addresses == null || addresses.length == 0)
 35  
         {
 36  0
             return StringUtils.EMPTY;
 37  
         }
 38  
 
 39  0
         StringBuffer buf = new StringBuffer(80);
 40  
 
 41  0
         for (int i = 0; i < addresses.length; i++)
 42  
         {
 43  0
             InternetAddress address = addresses[i];
 44  0
             buf.append(address.getAddress());
 45  
             // all except the last one
 46  0
             if (i < addresses.length - 1)
 47  
             {
 48  0
                 buf.append(", ");
 49  
             }
 50  
         }
 51  
 
 52  0
         return buf.toString();
 53  
     }
 54  
 
 55  
     public static String internetAddressesToString(InternetAddress address)
 56  
     {
 57  0
         return internetAddressesToString(new InternetAddress[]{address});
 58  
     }
 59  
 
 60  
     public static String mailAddressesToString(Address[] addresses)
 61  
     {
 62  0
         if (addresses == null || addresses.length == 0)
 63  
         {
 64  0
             return StringUtils.EMPTY;
 65  
         }
 66  
 
 67  0
         StringBuffer buf = new StringBuffer(80);
 68  
 
 69  0
         for (int i = 0; i < addresses.length; i++)
 70  
         {
 71  0
             Address address = addresses[i];
 72  0
             buf.append(address.toString());
 73  
             // all except the last one
 74  0
             if (i < addresses.length - 1)
 75  
             {
 76  0
                 buf.append(", ");
 77  
             }
 78  
         }
 79  
 
 80  0
         return buf.toString();
 81  
     }
 82  
 
 83  
     public static String mailAddressesToString(Address address)
 84  
     {
 85  0
         return mailAddressesToString(new Address[]{address});
 86  
     }
 87  
 
 88  
     public static InternetAddress[] stringToInternetAddresses(String address) throws AddressException
 89  
     {
 90  0
         if (StringUtils.isNotBlank(address))
 91  
         {
 92  0
             return InternetAddress.parse(address, false);
 93  
         }
 94  
         else
 95  
         {
 96  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull("Email address").toString());
 97  
         }
 98  
     }
 99  
 
 100  
     public static void getAttachments(Multipart content, Map attachments) throws MessagingException, IOException
 101  
     {
 102  0
         int x = 0;
 103  0
         for(int i=0; i < content.getCount(); i++)
 104  
         {
 105  0
             Part p = content.getBodyPart(i);
 106  0
             if(p.getContentType().indexOf("multipart/mixed") > -1)
 107  
             {
 108  0
                 Multipart m = (Multipart)p.getContent();
 109  0
                 getAttachments(m, attachments);
 110  
             }
 111  
             else
 112  
             {
 113  
                 String key;
 114  0
                 if(StringUtils.isNotEmpty(p.getDescription()))
 115  
                 {
 116  0
                     key = p.getDescription();
 117  
                 }
 118  0
                 else if(StringUtils.isNotEmpty(p.getFileName()))
 119  
                 {
 120  0
                     key = p.getFileName();
 121  
                 }
 122  0
                 else if(StringUtils.isNotEmpty(p.getDisposition()))
 123  
                 {
 124  0
                     key = p.getDisposition();
 125  
                 }
 126  
                 else
 127  
                 {
 128  0
                     key = String.valueOf(x++);
 129  
                 }
 130  0
                 attachments.put(key, p);
 131  
             }
 132  
         }
 133  0
     }
 134  
 }