KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > mailer > AttachTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.mailer;
18
19 import java.io.File JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import javax.activation.DataHandler JavaDoc;
23 import javax.activation.DataSource JavaDoc;
24 import javax.activation.FileDataSource JavaDoc;
25 import javax.mail.MessagingException JavaDoc;
26 import javax.mail.internet.MimeBodyPart JavaDoc;
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
29
30
31 /**
32  * AttachTag - JSP tag <b>Attach</b> is used to set the message in an email.
33  *
34  * <tag>
35  * <name>attach</name>
36  * <tagclass>org.apache.taglibs.mailer.AttachTag</tagclass>
37  * <bodycontent>JSP</bodycontent>
38  * <info>Adds attachments to email.</info>
39  * </tag>
40  *
41  * @author Rich Catlett Jayson Falkner
42  *
43  * @version 1.0
44  *
45  */

46
47 public class AttachTag extends BodyTagSupport JavaDoc {
48
49     /**
50      * mime type of the attachment
51      */

52     private String JavaDoc type = null;
53
54     /**
55      * holds the value of body if the url is to be retrieved from the body of
56      * tag
57      */

58     private String JavaDoc url = null;
59
60     /**
61      * holds the value of body if the path to the file is to be retrieved from
62      * the body of the tag
63      */

64     private String JavaDoc file = null;
65
66     /**
67      * object in which the attachment is stored within the e-mail message
68      */

69     private MimeBodyPart JavaDoc mbp = null;
70
71     /**
72      * parent mail tag
73      */

74     private MailTag myparent = null;
75
76     /**
77      * String containing text in body of tag
78      */

79     private String JavaDoc body;
80
81     /**
82      * implementation of method from the Tag interface that tells the JSP what
83      * to do upon encountering the start tag for this tag set
84      *
85      * @return EVAL_BODY_TAG - integer value telling the JSP engine to evaluate
86      * the body of this tag
87      *
88      * @throws JspException thrown when error occurs in processing the body of
89      * this method
90      *
91      */

92     public int doStartTag() throws JspException JavaDoc {
93
94         myparent = (MailTag)findAncestorWithClass(this, MailTag.class);
95         if (myparent == null) {
96             throw new JspException JavaDoc("Attach tag not nested within mail tag.");
97         }
98         mbp = new MimeBodyPart JavaDoc(); // create the bodypart for this attachment
99
body = null;
100         if (type != null || (file != null && file.length() == 0) ||
101                 (url != null && url.length() == 0) ) {
102             return EVAL_BODY_TAG;
103         }
104         return SKIP_BODY;
105     }
106
107     /**
108      * implementation of the method from the tag interface that tells the JSP
109      * page what to do after the body of this tag
110      *
111      * @throws JspException thrown when an error occurs while processing the
112      * body of this method
113      *
114      * @return SKIP_BODY int telling the tag handler to not evaluate the body
115      * of this tag again
116      *
117      */

118     public int doAfterBody() throws JspException JavaDoc {
119
120         body = bodyContent.getString();
121         bodyContent.clearBody();
122         if (body == null) {
123             body = "";
124         }
125         return SKIP_BODY;
126     }
127
128     /**
129      * implementation of method from the Tag interface that tells the JSP what
130      * to do upon encountering the end tag for this tag set
131      *
132      * @return - integer value telling the JSP engine whether or not to evaluate
133      * the body of this tag
134      *
135      * @throws JspException thrown when error occurs in processing the body of
136      * this method
137      *
138      */

139     public int doEndTag() throws JspException JavaDoc {
140
141         if (type != null) {
142             // the body of the tag is expected to be added as the attachment
143
// create a mimebodypart from the body of the tag
144
try {
145                 mbp.setDataHandler(new DataHandler JavaDoc(body, type));
146             } catch (MessagingException JavaDoc me) {
147                 throw new JspException JavaDoc(
148                     "The attachment named with the mimetype " + type +
149                     " could not be attached.");
150             }
151         } else if (file != null) {
152             if (file.length() == 0) {
153                 body.trim();
154                 // the file name is supposed to come from the body of the tag
155
if (body.length() > 0 ) {
156                     // prepare the file or url resource to be an attachment
157
setFileBodyPart(body);
158                 } else {
159                     // body is empty throw error
160
throw new JspException JavaDoc(
161                         "The file name must be given in the body of this tag.");
162                 }
163             } else {
164                 // create the attachment with the file name in the file
165
// attribute
166
setFileBodyPart(file);
167             }
168         } else if (url != null) {
169             if (url.length() == 0) {
170                 body.trim();
171                 // the url is supposed to come from the body of the tag
172
if (body.length() > 0) {
173                     // prepare the file or url resource to be an attachment
174
setUrlBodyPart(body);
175                 } else {
176                     // body is empty throw error
177
throw new JspException JavaDoc(
178                         "The url must be givenin the body of this tag.");
179                 }
180             } else
181                 // create the attachment with the url in the url attribute
182
setUrlBodyPart(url);
183         }
184
185         // Add the attachment to list of attachments
186
myparent.setBodyParts(mbp);
187         return EVAL_PAGE;
188     }
189
190     /**
191      * set the mime type for this email text or html
192      *
193      * @param value string that is the mime type for this email
194      *
195      */

196     public void setType(String JavaDoc value) {
197         type = value;
198     }
199
200     /**
201      * set the resource named by URL into a mimebodypart so that it can be added
202      * to the list of attachments for this e-mail
203      *
204      * @param value full url including http://, to the resource to be added as
205      * an attachment
206      */

207     public void setUrl(String JavaDoc value) {
208         url = value;
209     }
210
211     /**
212      * set the named file up as an attachment to be added to the list of
213      * attachments for this e-mail
214      *
215      * @param value name of the file to be added as an attachment
216      *
217      */

218     public void setFile(String JavaDoc value) {
219         file = value;
220     }
221
222     /**
223      * wrap the url named attachment in the approiate datahandler and create a
224      * mimebodypart to be added to the list of attachments
225      *
226      * @param value string that represents a URL
227      *
228      */

229     protected void setUrlBodyPart(String JavaDoc value) throws JspException JavaDoc {
230
231 // Added by Jayson Falkner - 5/8/2001
232

233         try {
234             URL JavaDoc url = new URL JavaDoc(value);
235             mbp.setDataHandler(new DataHandler JavaDoc(url));
236             if(url.getFile() != null)
237                 mbp.setFileName(url.getFile());
238             else
239                 mbp.setFileName(value);
240
241         } catch(MalformedURLException JavaDoc e) {
242             throw new JspException JavaDoc("The URL entered as an attachment was " +
243                         "incorrectly formatted please check it and try again.");
244         } catch(MessagingException JavaDoc e) {
245             throw new JspException JavaDoc("The Resource named by " + url + " could not"
246                                    + " be attached.");
247         }
248 // End of added
249
}
250
251     /**
252      * wrap the file attachment in the approiate datahandler and create a
253      * mimebodypart to be added to the list of attachments
254      *
255      * @param value string that represents a file path
256      *
257      */

258     protected void setFileBodyPart(String JavaDoc value) throws JspException JavaDoc {
259
260         // create a real path from the webapplication realative path given as
261
// the name of the file to attach
262
String JavaDoc rpath = pageContext.getServletContext().getRealPath(value);
263
264 // Added by Jayson Falkner - 5/8/2001
265
try {
266             File JavaDoc file = new File JavaDoc(rpath);
267             if (file.exists()) {
268                 DataSource JavaDoc attachment = new FileDataSource JavaDoc(file);
269                 mbp.setDataHandler(new DataHandler JavaDoc(attachment));
270                 mbp.setFileName(file.getName());
271             } else {
272                 // if the file does not exist it is probably an error in the way
273
// the page author is adding the path throw an exception so this
274
// can be discovered and fixed
275
throw new JspException JavaDoc("File " + rpath + " does not exist or " +
276                                        "the path to the file is incorrect.");
277             }
278         } catch(MessagingException JavaDoc e) {
279             throw new JspException JavaDoc("The file named by " + file + " could not be"
280                                    + " attached.");
281         }
282
283 // End of added
284
}
285 }
286
Popular Tags