catlett     01/05/21 11:22:47

  Added:       mailer/src/org/apache/taglibs/mailer AttachTag.java
  Log:
  support for mail attachments added
  
  Revision  Changes    Path
  1.1                  
jakarta-taglibs/mailer/src/org/apache/taglibs/mailer/AttachTag.java
  
  Index: AttachTag.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-taglibs/mailer/src/org/apache/taglibs/mailer/AttachTag.java,v 1.1 
2001/05/21 18:22:44 catlett Exp $
   * $Revision: 1.1 $
   * $Date: 2001/05/21 18:22:44 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.taglibs.mailer;
  
  import java.util.*;
  import java.net.URL;
  import java.net.MalformedURLException;
  import java.io.File;
  import javax.servlet.jsp.*;
  import javax.servlet.jsp.tagext.*;
  import javax.activation.*;
  import javax.mail.MessagingException;
  import javax.mail.internet.*;
  
  
  /**
   * AttachTag - JSP tag <b>Attach</b> is used to set the message in an email.
   *
   * <tag>
   *        <name>attach</name>
   *      <tagclass>org.apache.taglibs.mailer.AttachTag</tagclass>
   *      <bodycontent>JSP</bodycontent>
   *      <info>Adds attachments to email.</info>
   * </tag>
   *
   * @author Rich Catlett Jayson Falkner
   *
   * @version 1.0
   *
   */
  
  public class AttachTag extends BodyTagSupport {
  
      /**
       * mime type of the attachment
       */
      private String type = null;
      /**
       * holds the value of body if the url is to be retrieved from the body of
       * tag
       */
      private String url = null;
      /**
       * holds the value of body if the path to the file is to be retrieved from
       * the body of the tag
       */
      private String file = null;
      /**
       * object in which the attachment is stored within the e-mail message
       */
      MimeBodyPart mbp = new MimeBodyPart();
  
      /**
       *  implementation of the method from the tag interface that tells the JSP
       *  page what to do after the body of this tag
       *
       *  @throws JSPException  thrown when an error occurs while processing the
       *                        body of this method
       *
       *  @return SKIP_BODY  int telling the tag handler to not evaluate the body
       *                     of this tag again
       *
       */
      public final int doAfterBody() throws JspException {
  
        // parent tag must be a MailTag, gives access to methods in parent
        MailTag myparent = 
(MailTag)javax.servlet.jsp.tagext.TagSupport.findAncestorWithClass(this, 
MailTag.class);
          String body;  // the body for this email
  
        if (myparent == null)
            throw new JspException("Message tag not nested within mail tag.");
          else {
            if (type != null) {
                // the body of the tag is expected to be added as the attachment
                if ((body = bodyContent.getString()) != null) {
  
                    // create a mimebodypart from the body of the tag
                    try {
                        mbp.setDataHandler(new DataHandler(body.trim(), type));
                    } catch (MessagingException me) {
                        throw new JspException("The attachment named with this "
                                               + "mimetype " + type + " could "
                                               + "not be attached.");
                    }
                } else
                    throw new JspException("A mimetype must be entered for "
                                           + "the body of the tag.");
  
            } else if (file != null) {
                if (file.equalsIgnoreCase("")) {
                    // the file name is supposed to come from the body of the tag
                    if ((body = bodyContent.getString()) != null)
                        // prepare the file or url resource to be an attachment
                        setFileBodyPart(body.trim());
                    else
                        // body is empty throw error
                        throw new JspException("The file name must be given"
                                           + " in the body of this tag.");
                } else
                    // create the attachment with the file name in the file
                    // attribute
                    setFileBodyPart(file);
  
            } else if (url != null) {
                if (url.equalsIgnoreCase("")) {
                    // the url is supposed to come from the body of the tag
                    if ((body = bodyContent.getString()) != null)
                        // prepare the file or url resource to be an attachment
                        setUrlBodyPart(body.trim());
                    else
                        // body is empty throw error
                        throw new JspException("The url must be given"
                                           + " in the body of this tag.");
                } else
                    // create the attachment with the url in the url attribute
                    setUrlBodyPart(url);
            }
        }
  
        // Add the attachment to list of attachments
        myparent.setBodyParts(mbp);
  
        return SKIP_BODY;
      }
  
      /**
       * set the mime type for this email text or html
       *
       * @param value  string that is the mime type for this email
       *
       */
      public final void setType(String value) {
        type = value;
      }
  
      /**
       * set the resource named by URL into a mimebodypart so that it can be added
       * to the list of attachments for this e-mail
       *
       * @param value  full url including http://, to the resource to be added as
       *               an attachment
       */
      public final void setUrl(String value) {
        url = value;
      }
  
      /**
       * set the named file up as an attachment to be added to the list of
       * attachments for this e-mail
       *
       * @param value  name of the file to be added as an attachment
       *
       */
      public final void setFile(String value) {
        file = value;
      }
  
      /**
       * wrap the url named attachment in the approiate datahandler and create a
       * mimebodypart to be added to the list of attachments
       *
       * @param value  string that represents a URL
       *
       * @returns = MimeBodyPart that is added to the list of attachments
       *
       */
      private final void setUrlBodyPart(String value) throws JspException {
  
  // Added by Jayson Falkner - 5/8/2001
  
        try {
            URL url = new URL(value);
            mbp.setDataHandler(new DataHandler(url));
            if(url.getFile() != null)
                mbp.setFileName(url.getFile());
            else
                mbp.setFileName(value);
  
        } catch(MalformedURLException e) {
            throw new JspException("The URL entered as an attachment was " +
                        "incorrectly formatted please check it and try again.");
        } catch(MessagingException e) {
            throw new JspException("The Resource named by " + url + " could not "
                                   + "be attached.");
        }
  // End of added
      }
  
      /**
       * wrap the file attachment in the approiate datahandler and create a
       * mimebodypart to be added to the list of attachments
       *
       * @param value  string that represents a file path
       *
       * @returns = MimeBodyPart that is added to the list of attachments
       *
       */
      private final void setFileBodyPart(String value)  throws JspException {
  
        // create a real path from the webapplication realative path given as
        // the name of the file to attach
        String rpath = pageContext.getServletContext().getRealPath(value);
  
  // Added by Jayson Falkner - 5/8/2001
        try {
            File file = new File(rpath);
            DataSource attachment = new FileDataSource(file);
            mbp.setDataHandler(new DataHandler(attachment));
            mbp.setFileName(file.getName());
        } catch(MessagingException e) {
            throw new JspException("The file named by " + file + " could not be "
                                   + "attached.");
        }
  
  // End of added
      }
  }
  
  
  

Reply via email to