Coverage report

  %line %branch
org.apache.turbine.util.mail.MultiPartEmail
0% 
0% 

 1  
 package org.apache.turbine.util.mail;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.net.URL;
 20  
 
 21  
 import java.util.Vector;
 22  
 
 23  
 import javax.activation.DataHandler;
 24  
 import javax.activation.DataSource;
 25  
 import javax.activation.URLDataSource;
 26  
 
 27  
 import javax.mail.MessagingException;
 28  
 import javax.mail.internet.MimeBodyPart;
 29  
 import javax.mail.internet.MimeMultipart;
 30  
 
 31  
 import org.apache.torque.util.Criteria;
 32  
 
 33  
 /**
 34  
  * A multipart email.
 35  
  *
 36  
  * <p>This class is used to send multi-part internet email like
 37  
  * messages with attachments.
 38  
  *
 39  
  * <p>To create a multi-part email, call the default constructor and
 40  
  * then you can call setMsg() to set the message and call the
 41  
  * different attach() methods.
 42  
  *
 43  
  * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
 44  
  * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
 45  
  * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
 46  
  * @author <a href="mailto:unknown">Regis Koenig</a>
 47  
  * @version $Id: MultiPartEmail.java 264148 2005-08-29 14:21:04Z henning $
 48  
  * @deprecated Use org.apache.commons.mail.MultiPartEmail instead.
 49  
  */
 50  
 public class MultiPartEmail extends Email
 51  
 {
 52  
     /** Body portion of the email. */
 53  
     protected MimeMultipart emailBody;
 54  
 
 55  
     /** The message container. */
 56  
     protected MimeBodyPart main;
 57  
 
 58  
     /** The file server if it exists. */
 59  0
     private String fileServer = null;
 60  
 
 61  
     /**
 62  
      * Initialize the multipart email.
 63  
      *
 64  
      * @exception MessagingException.
 65  
      */
 66  
     public MultiPartEmail()
 67  
             throws MessagingException
 68  0
     {
 69  0
         this.init();
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Constructor used to initialize attributes.
 74  
      *
 75  
      * <p>This method uses the criteria object to set the different
 76  
      * fields of the e-mail. The expected fields of the Criteria are:
 77  
      *
 78  
      * <ul>
 79  
      * <li>SENDER_EMAIL</li>
 80  
      * <li>RECEIVER_EMAIL</li>
 81  
      * <li>EMAIL_SUBJECT</li>
 82  
      * <li>EMAIL_BODY</li>
 83  
      * <li>ATTACHMENTS - A Vector of EmailAttachment.</li>
 84  
      * <li>FILE_SERVER - Where the files are located.  If not given,
 85  
      * they are assumed to be local.</li>
 86  
      * </ul>
 87  
      *
 88  
      * Deprecated, since Criteria is deprecated in mail API.
 89  
      *
 90  
      * @param criteria A Criteria.
 91  
      * @exception MessagingException.
 92  
      */
 93  
     public MultiPartEmail(Criteria criteria)
 94  
             throws MessagingException
 95  0
     {
 96  0
         this.init();
 97  0
         this.initCriteria(criteria);
 98  0
     }
 99  
 
 100  
     /**
 101  
      * Initialize the multipart email.
 102  
      *
 103  
      * @exception MessagingException.
 104  
      */
 105  
     protected void init()
 106  
             throws MessagingException
 107  
     {
 108  0
         super.init();
 109  
 
 110  0
         fileServer = null;
 111  
 
 112  
         /* The body of the mail. */
 113  0
         emailBody = new MimeMultipart();
 114  0
         message.setContent(emailBody);
 115  
 
 116  
         /* The main message content. */
 117  0
         main = new MimeBodyPart();
 118  0
         emailBody.addBodyPart(main);
 119  0
     }
 120  
 
 121  
     /**
 122  
      * Uses the criteria to set the fields.
 123  
      *
 124  
      * <p>This method uses the criteria object to set the different
 125  
      * fields of the e-mail.  The expected fields of the Criteria are:
 126  
      *
 127  
      * <ul>
 128  
      * <li>SENDER_EMAIL</li>
 129  
      * <li>RECEIVER_EMAIL</li>
 130  
      * <li>EMAIL_SUBJECT</li>
 131  
      * <li>EMAIL_BODY</li>
 132  
      * <li>ATTACHMENTS - A Vector of EmailAttachment.</li>
 133  
      * <li>FILE_SERVER - Where the files are located.  If not given,
 134  
      * they are assumed to be local.</li>
 135  
      * </ul>
 136  
      *
 137  
      * Deprecated, since the Criteria is deprecated.
 138  
      *
 139  
      * @param criteria A Criteria.
 140  
      * @exception MessagingException.
 141  
      */
 142  
     protected void initCriteria(Criteria criteria)
 143  
             throws MessagingException
 144  
     {
 145  0
         super.initCriteria(criteria);
 146  
 
 147  0
         if (criteria.containsKey(EMAIL_BODY))
 148  
         {
 149  0
             setMsg(criteria.getString(EMAIL_BODY));
 150  
         }
 151  
         else
 152  
         {
 153  0
             setMsg("NO MESSAGE");
 154  
         }
 155  
 
 156  
         Vector attachments;
 157  
 
 158  0
         if (criteria.containsKey(ATTACHMENTS))
 159  
         {
 160  0
             attachments = (Vector) criteria.get(ATTACHMENTS);
 161  
         }
 162  
         else
 163  
         {
 164  0
             attachments = new Vector();
 165  
         }
 166  
 
 167  0
         if (criteria.containsKey(FILE_SERVER))
 168  
         {
 169  0
             fileServer = criteria.getString(FILE_SERVER);
 170  
         }
 171  
 
 172  0
         for (int i = 0; i < attachments.size(); i++)
 173  
         {
 174  0
             EmailAttachment attachment =
 175  
                     (EmailAttachment) attachments.elementAt(i);
 176  0
             attach(attachment);
 177  
         }
 178  0
     }
 179  
 
 180  
     /**
 181  
      * Set the message of the email.
 182  
      *
 183  
      * @param msg A String.
 184  
      * @return An Email.
 185  
      * @exception MessagingException.
 186  
      */
 187  
     public Email setMsg(String msg)
 188  
             throws MessagingException
 189  
     {
 190  0
         if (charset != null)
 191  
         {
 192  0
             main.setText(msg, charset);
 193  
         }
 194  
         else
 195  
         {
 196  0
             main.setText(msg);
 197  
         }
 198  0
         return this;
 199  
     }
 200  
 
 201  
     /**
 202  
      * Attach an EmailAttachement.
 203  
      *
 204  
      * @param attachment An EmailAttachment.
 205  
      * @return A MultiPartEmail.
 206  
      * @exception MessagingException.
 207  
      */
 208  
     public MultiPartEmail attach(EmailAttachment attachment)
 209  
             throws MessagingException
 210  
     {
 211  0
         URL url = attachment.getURL();
 212  0
         if (url == null)
 213  
         {
 214  
             try
 215  
             {
 216  0
                 String file = attachment.getPath();
 217  0
                 url = new URL("file", fileServer, file);
 218  
             }
 219  0
             catch (Exception e)
 220  
             {
 221  0
                 throw new MessagingException("Cannot find file", e);
 222  0
             }
 223  
         }
 224  
 
 225  0
         return attach(url, attachment.getName(),
 226  
                 attachment.getDescription(),
 227  
                 attachment.getDisposition());
 228  
     }
 229  
 
 230  
     /**
 231  
      * Attach a file located by its URL.  The disposition of the file
 232  
      * is set to mixed.
 233  
      *
 234  
      * @param url The URL of the file (may be any valid URL).
 235  
      * @param name The name field for the attachment.
 236  
      * @param description A description for the attachment.
 237  
      * @return A MultiPartEmail.
 238  
      * @exception MessagingException.
 239  
      */
 240  
     public MultiPartEmail attach(URL url, String name, String description)
 241  
             throws MessagingException
 242  
     {
 243  0
         return attach(url, name, description, EmailAttachment.ATTACHMENT);
 244  
     }
 245  
 
 246  
     /**
 247  
      * Attach a file located by its URL.
 248  
      *
 249  
      * @param url The URL of the file (may be any valid URL).
 250  
      * @param name The name field for the attachment.
 251  
      * @param description A description for the attachment.
 252  
      * @param disposition Either mixed or inline.
 253  
      * @return A MultiPartEmail.
 254  
      * @exception MessagingException.
 255  
      */
 256  
     public MultiPartEmail attach(URL url,
 257  
                                  String name,
 258  
                                  String description,
 259  
                                  String disposition)
 260  
             throws MessagingException
 261  
     {
 262  0
         return attach(new URLDataSource(url), name, description, disposition);
 263  
     }
 264  
 
 265  
     /**
 266  
      * Attach a file specified as a DataSource interface.
 267  
      *
 268  
      * @param ds A DataSource interface for the file.
 269  
      * @param name The name field for the attachment.
 270  
      * @param description A description for the attachment.
 271  
      * @return A MultiPartEmail.
 272  
      * @exception MessagingException.
 273  
      */
 274  
     public MultiPartEmail attach(DataSource ds,
 275  
                                  String name,
 276  
                                  String description)
 277  
             throws MessagingException
 278  
     {
 279  0
         return attach(ds, name, description, EmailAttachment.ATTACHMENT);
 280  
     }
 281  
 
 282  
     /**
 283  
      * Attach a file specified as a DataSource interface.
 284  
      *
 285  
      * @param ds A DataSource interface for the file.
 286  
      * @param name The name field for the attachment.
 287  
      * @param description A description for the attachement.
 288  
      * @param disposition Either mixed or inline.
 289  
      * @return A MultiPartEmail.
 290  
      * @exception MessagingException.
 291  
      */
 292  
     public MultiPartEmail attach(DataSource ds,
 293  
                                  String name,
 294  
                                  String description,
 295  
                                  String disposition)
 296  
             throws MessagingException
 297  
     {
 298  0
         MimeBodyPart mbp = new MimeBodyPart();
 299  0
         emailBody.addBodyPart(mbp);
 300  
 
 301  0
         mbp.setDisposition(disposition);
 302  0
         mbp.setFileName(name);
 303  0
         mbp.setDescription(description);
 304  0
         mbp.setDataHandler(new DataHandler(ds));
 305  
 
 306  0
         return this;
 307  
     }
 308  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.