KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > AttachmentInfo


1 /*
2  * @(#)AttachmentInfo.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
45 import javax.mail.*;
46 import javax.mail.internet.*;
47 import javax.servlet.*;
48 import javax.servlet.http.*;
49
50 /**
51  * Used to store attachment information.
52  */

53 public class AttachmentInfo {
54     private Part part;
55     private int num;
56     
57
58     /**
59      * Returns the attachment's content type.
60      */

61     public String JavaDoc getAttachmentType() throws MessagingException {
62         String JavaDoc contentType;
63         if ((contentType = part.getContentType()) == null)
64             return "invalid part";
65         else
66         return contentType;
67     }
68
69     /**
70      * Returns the attachment's content (if it is plain text).
71      */

72     public String JavaDoc getContent() throws java.io.IOException JavaDoc, MessagingException {
73         if (hasMimeType("text/plain"))
74             return (String JavaDoc)part.getContent();
75         else
76             return "";
77     }
78     
79     /**
80      * Returns the attachment's description.
81      */

82     public String JavaDoc getDescription() throws MessagingException {
83         String JavaDoc description;
84         if ((description = part.getDescription()) != null)
85             return description;
86         else
87             return "";
88     }
89     
90     /**
91      * Returns the attachment's filename.
92      */

93     public String JavaDoc getFilename() throws MessagingException {
94         String JavaDoc filename;
95         if ((filename = part.getFileName()) != null)
96             return filename;
97         else
98             return "";
99     }
100
101     /**
102      * Returns the attachment number.
103      */

104     public String JavaDoc getNum() {
105         return (Integer.toString(num));
106     }
107     
108     /**
109      * Method for checking if the attachment has a description.
110      */

111     public boolean hasDescription() throws MessagingException {
112         return (part.getDescription() != null);
113     }
114     
115     /**
116      * Method for checking if the attachment has a filename.
117      */

118     public boolean hasFilename() throws MessagingException {
119         return (part.getFileName() != null);
120     }
121     
122     /**
123      * Method for checking if the attachment has the desired mime type.
124      */

125     public boolean hasMimeType(String JavaDoc mimeType) throws MessagingException {
126         return part.isMimeType(mimeType);
127     }
128     
129     /**
130      * Method for checking the content disposition.
131      */

132     public boolean isInline() throws MessagingException {
133         if (part.getDisposition() != null)
134             return part.getDisposition().equals(Part.INLINE);
135         else
136             return true;
137     }
138     
139     /**
140      * Method for mapping a message part to this AttachmentInfo class.
141      */

142     public void setPart(int num, Part part)
143         throws MessagingException, ParseException {
144             
145         this.part = part;
146         this.num = num;
147     }
148 }
149
150
Popular Tags