KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > ListAttachmentsTag


1 /*
2  * @(#)ListAttachmentsTag.java 1.3 02/04/04
3  *
4  * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  *
38  */

39
40 package demo;
41
42 import java.io.*;
43 import java.util.*;
44 import javax.mail.*;
45 import javax.mail.internet.*;
46 import javax.servlet.jsp.*;
47 import javax.servlet.jsp.tagext.*;
48
49 /**
50  * Custom tag for listing message attachments. The scripting variable is only
51  * within the body of the tag.
52  */

53 public class ListAttachmentsTag extends BodyTagSupport {
54     private String JavaDoc messageinfo;
55     private int partNum = 1;
56     private int numParts = 0;
57     private AttachmentInfo attachmentinfo;
58     private MessageInfo messageInfo;
59     private Multipart multipart;
60
61     /**
62      * messageinfo attribute getter method.
63      */

64     public String JavaDoc getMessageinfo() {
65         return messageinfo;
66     }
67     
68     /**
69      * messageinfo attribute setter method.
70      */

71     public void setMessageinfo(String JavaDoc messageinfo) {
72         this.messageinfo = messageinfo;
73     }
74
75     /**
76      * Method for processing the start of the tag.
77      */

78     public int doStartTag() throws JspException {
79         messageInfo = (MessageInfo)pageContext.getAttribute(getMessageinfo());
80         attachmentinfo = new AttachmentInfo();
81         
82         try {
83             multipart = (Multipart)messageInfo.getMessage().getContent();
84             numParts = multipart.getCount();
85         } catch (Exception JavaDoc ex) {
86             throw new JspException(ex.getMessage());
87         }
88
89         getPart();
90
91         return BodyTag.EVAL_BODY_TAG;
92     }
93    
94     /**
95      * Method for processing the body content of the tag.
96      */

97     public int doAfterBody() throws JspException {
98         
99         BodyContent body = getBodyContent();
100         try {
101             body.writeOut(getPreviousOut());
102         } catch (IOException e) {
103             throw new JspTagException("IterationTag: " + e.getMessage());
104         }
105         
106         // clear up so the next time the body content is empty
107
body.clearBody();
108        
109         partNum++;
110         if (partNum < numParts) {
111             getPart();
112             return BodyTag.EVAL_BODY_TAG;
113         } else {
114             return BodyTag.SKIP_BODY;
115         }
116     }
117     
118     /**
119      * Helper method for retrieving message parts.
120      */

121     private void getPart() throws JspException {
122         try {
123             attachmentinfo.setPart(partNum, multipart.getBodyPart(partNum));
124             pageContext.setAttribute(getId(), attachmentinfo);
125         } catch (Exception JavaDoc ex) {
126             throw new JspException(ex.getMessage());
127         }
128     }
129 }
130
131
Popular Tags