Bug 31859 - Add filename based Content-ID to attachments and url based message bodies.
Summary: Add filename based Content-ID to attachments and url based message bodies.
Status: RESOLVED LATER
Alias: None
Product: Taglibs
Classification: Unclassified
Component: Mailer Taglib (show other bugs)
Version: 1.1.0
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL: http://home.insightbb.com/~robertmurp...
Keywords:
Depends on:
Blocks:
 
Reported: 2004-10-23 02:41 UTC by Robert L. Murphy
Modified: 2009-11-29 19:40 UTC (History)
0 users



Attachments
Make the message tag capable of handling a url attribute or a url attribute and a url body (7.51 KB, patch)
2004-10-23 02:47 UTC, Robert L. Murphy
Details | Diff
Add a Content-ID for each attachment based on its source filename (7.51 KB, patch)
2004-10-23 02:48 UTC, Robert L. Murphy
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Robert L. Murphy 2004-10-23 02:41:51 UTC
The two diffs add the folowing:

<mt:message url="http://www.domain.net"/>

<mt:message url="">
   http://www.domain.net
</mt:message>

And, when attaching a file, each attachment will have a Content-ID based on the
filename.



Index: AttachTag.java
===================================================================
--- AttachTag.java	(revision 1)
+++ AttachTag.java	(working copy)
@@ -97,8 +97,13 @@
         }
         mbp = new MimeBodyPart();  // create the bodypart for this attachment
         body = null;
-        if (type != null || (file != null && file.length() == 0) ||
-                (url != null && url.length() == 0) ) {
+        if (
+            type != null 
+        || 
+            (file != null && file.length() == 0) 
+        ||
+            (url != null && url.length() == 0) 
+        ) {
             return EVAL_BODY_TAG;
         }
         return SKIP_BODY;
@@ -233,10 +238,13 @@
         try {
             URL url = new URL(value);
             mbp.setDataHandler(new DataHandler(url));
-            if(url.getFile() != null)
+            if(url.getFile() != null) {
                 mbp.setFileName(url.getFile());
-            else
+                mbp.setContentID(url.getFile());
+            } else {
                 mbp.setFileName(value);
+                mbp.setContentID(value);
+            }
 
         } catch(MalformedURLException e) {
             throw new JspException("The URL entered as an attachment was " +
@@ -268,6 +276,7 @@
                 DataSource attachment = new FileDataSource(file);
                 mbp.setDataHandler(new DataHandler(attachment));
                 mbp.setFileName(file.getName());
+                mbp.setContentID(file.getName());
             } else {
                 // if the file does not exist it is probably an error in the way
                 // the page author is adding the path throw an exception so this
Index: MessageTag.java
===================================================================
--- MessageTag.java	(revision 1)
+++ MessageTag.java	(working copy)
@@ -19,6 +19,11 @@
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.tagext.BodyContent;
 import javax.servlet.jsp.tagext.BodyTagSupport;
+import java.net.URL;
+import javax.mail.internet.MimeBodyPart;
+import javax.activation.DataHandler;
+import java.net.MalformedURLException;
+import javax.mail.MessagingException;
 
 /**
  * MessageTag - JSP tag <b>Message</b> is used to set the message in an e-mail.
@@ -55,11 +60,27 @@
     private String type = "text";
 
     /**
+     * holds the value of body if the url is to be retrieved from the body of
+     * tag
+     */
+    private String url = null;
+    
+    /**
      * character set to be used (default is unspecified)
      */
     private String charset = null;
 
     /**
+     * object in which the attachment is stored within the e-mail message
+     */
+    private MimeBodyPart mbp = null;
+    
+    /**
+     * pointer to the parent tag
+     */
+    private MailTag myparent = null;
+    
+    /**
      *  implementation of the method from the tag interface that tells the JSP
      *  page what to do after the body of this tag
      *
@@ -71,26 +92,81 @@
      *
      */
     public int doAfterBody() throws JspException {
+        BodyContent body = getBodyContent();
+        mbp = new MimeBodyPart();  // create the bodypart for this attachment 
      
+        myparent = (MailTag)findAncestorWithClass(this, MailTag.class);
+        if (url == null) {
+            // parent tag must be a MailTag, gives access to methods in parent
+            if (myparent == null) {
+                throw new JspException("message tag not nested within mail tag");
+            }
+            
+            String message = body.getString();
+            // Clear the body since we only used it as input for the email address
+            body.clearBody();
+            if (message == null) {
+                throw new JspException("The message tag is empty");
+            }
+            myparent.setMessage(message); // set message in the parent tag
+            myparent.setType(type);  // set the mime type of the message
+            myparent.setCharset(charset);  // set the character set of the message
+            
+        } else if (url.length() == 0 && body != null) {
+            String s_body = body.getString();
+            if(s_body != null)
+                s_body = s_body.trim();
+            // the url is supposed to come from the body of the tag
+            if (s_body.length() > 0) {
+                // prepare the file or url resource to be an attachment
+                setUrlBodyPart(s_body);
+            } else {
+                // body is empty throw error
+                throw new JspException(
+                    "The url must be givenin the body of this tag.");
+            }
+        } else
+            // create the attachment with the url in the url attribute
+            setUrlBodyPart(url);
+        return SKIP_BODY;
+    }
 
-	// parent tag must be a MailTag, gives access to methods in parent
-	MailTag myparent = (MailTag)findAncestorWithClass(this, MailTag.class);
-	if (myparent == null) {
-	    throw new JspException("message tag not nested within mail tag");
-        }
+    /**
+     * 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
+     *
+     */
+    protected void setUrlBodyPart(String value) throws JspException {
 
-        BodyContent body = getBodyContent();
-        String message = body.getString();
-        // Clear the body since we only used it as input for the email address
-        body.clearBody();
-        if (message == null) {
-            throw new JspException("The message tag is empty");
+// 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());
+                mbp.setContentID(url.getFile());
+            } else {
+                mbp.setFileName(value);
+                mbp.setContentID(value);
+            }   
+            myparent.setMessage((String)mbp.getContent()); // set message in
the parent tag
+            myparent.setType(type);  // set the mime type of the message
+            myparent.setCharset(charset);  // set the character set of the message
+        } 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 used as the message body.");
+        } catch(java.io.IOException ioe) {
+            throw new JspException("The Resource named by " + url + " could not"
+                                   + " be cast to a String.");
         }
-	myparent.setMessage(message); // set message in the parent tag
-	myparent.setType(type);  // set the mime type of the message
-	myparent.setCharset(charset);  // set the character set of the message
-	return SKIP_BODY;
+// End of added
     }
-
+    
     /**
      * set the mime type for this email text or html
      *
@@ -110,4 +186,15 @@
     public void setCharset(String value) {
 	charset = 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 void setUrl(String value) {
+        url = value;
+    }
 }
Comment 1 Robert L. Murphy 2004-10-23 02:47:51 UTC
Created attachment 13203 [details]
Make the message tag capable of handling a url attribute or a url attribute and a url body
Comment 2 Robert L. Murphy 2004-10-23 02:48:44 UTC
Created attachment 13204 [details]
Add a Content-ID for each attachment based on its source filename
Comment 3 Robert L. Murphy 2004-10-23 02:53:56 UTC
The two patches attached were generated with SubVersion 1.1, don't know if they
are the same as files generated with other diff programs.

Did not include the modified tld, just requires copying the url block from
attach to message.
Comment 4 Henri Yandell 2009-11-29 19:40:21 UTC
Resolving. Taglib has been retired.