KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > AttachmentBundle


1 package net.suberic.pooka;
2 import java.util.*;
3 import java.io.IOException JavaDoc;
4 import javax.mail.internet.*;
5 import javax.mail.*;
6
7
8 /**
9  * This class is here for my convenience so that I can have a single
10  * set of attachment parsers that will return any set of information
11  * that I want.
12  */

13 class AttachmentBundle {
14   private Attachment textPart = null;
15   private Vector allAttachments = new Vector();
16   Vector attachmentsAndTextPart = null;
17   InternetHeaders headers = null;
18   Vector headerLines = null;
19
20   /**
21    * Creates an AttachmentBundle.
22    */

23   AttachmentBundle() {
24   }
25
26   /**
27    * Creates an AttachmentBundle from the given MimePart.
28    */

29   AttachmentBundle(MimePart m) throws MessagingException {
30     setHeaderSource(m);
31   }
32
33   /**
34    * Adds all of the entries in the <code>subBundle</code> to this
35    * AttachmentBundle. Also will update the <code>textPart</code>,
36    * if this Bundle doesn't have one yet.
37    */

38   synchronized void addAll(AttachmentBundle subBundle) {
39     if (subBundle.textPart != null)
40       subBundle.textPart.setHeaderSource(subBundle);
41
42     if (textPart == null) {
43       textPart = subBundle.textPart;
44     } else if (textPart instanceof net.suberic.pooka.crypto.CryptoAttachment) {
45       if (subBundle.textPart != null) {
46         textPart = subBundle.textPart;
47       }
48     } else if (subBundle.textPart != null) {
49       allAttachments.add(subBundle.textPart);
50     }
51
52     allAttachments.addAll(subBundle.allAttachments);
53   }
54
55   /**
56    * Adds an Attachment to the AttachmentBundle. Automagically checks the
57    * type of the Attachment and sets it as the text part, if appropriate.
58    */

59   synchronized void addAttachment(Attachment newAttach) {
60     addAttachment(newAttach, newAttach.getMimeType(), true);
61   }
62
63   /**
64    * Adds an Attachment to the AttachmentBundle.
65    *
66    * If allowTextPart is false, then forces the Attachment to
67    * be an attachment, rather than allowing it to be set as
68    * the content.
69    */

70   synchronized void addAttachment(Attachment newAttach, boolean allowTextPart) {
71     addAttachment(newAttach, newAttach.getMimeType(), allowTextPart);
72   }
73
74   /**
75    * Removes the given Attachment.
76    */

77   synchronized void removeAttachment(Attachment delAttach) {
78     if (textPart == delAttach) {
79       textPart = null;
80       Iterator it = allAttachments.iterator();
81       while (textPart == null && it.hasNext()) {
82         Attachment at = (Attachment) it.next();
83         if (at instanceof AlternativeAttachment || at.getMimeType().match("text/")) {
84           textPart = at;
85         }
86       }
87     } else {
88       allAttachments.remove(delAttach);
89     }
90   }
91
92   /**
93    * Adds an Attachment using the given ContentType.
94    */

95   synchronized void addAttachment(Attachment newAttach, ContentType ct) {
96     addAttachment(newAttach, ct, true);
97   }
98
99   /**
100    * Adds an Attachment using the given ContentType.
101    *
102    * If allowTextPart is false, then forces the Attachment to
103    * be an attachment, rather than allowing it to be set as
104    * the content.
105    */

106   synchronized void addAttachment(Attachment newAttach, ContentType ct, boolean allowTextPart) {
107     if (! allowTextPart) {
108       allAttachments.add(newAttach);
109     } else {
110       if ((textPart == null || textPart instanceof net.suberic.pooka.crypto.CryptoAttachment) && (newAttach instanceof AlternativeAttachment || ct.match("text/*"))) {
111         textPart = newAttach;
112       } else if (textPart == null && newAttach instanceof net.suberic.pooka.crypto.CryptoAttachment) {
113         textPart = newAttach;
114         allAttachments.add(newAttach);
115       } else {
116         allAttachments.add(newAttach);
117       }
118     }
119   }
120
121   /**
122    * This gets the Text part of a message. This is useful if you want
123    * to display just the 'body' of the message without the attachments.
124    */

125   public String JavaDoc getTextPart(boolean withHeaders, boolean showFullHeaders, int maxLength, String JavaDoc truncationMessage) throws IOException JavaDoc {
126
127     StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
128
129     if (withHeaders)
130       retVal.append(getHeaderInformation(showFullHeaders, false));
131
132     String JavaDoc text = null;
133     if (textPart != null) {
134       text = textPart.getText(withHeaders, showFullHeaders, maxLength, truncationMessage);
135     }
136
137     if (text != null) {
138       retVal.append(text);
139       return retVal.toString();
140     } else
141       return null;
142   }
143
144   /**
145    * This gets the Html part of a message. This is useful if you want
146    * to display just the 'body' of the message without the attachments.
147    */

148   public String JavaDoc getHtmlPart(boolean withHeaders, boolean showFullHeaders, int maxLength, String JavaDoc truncationMessage) throws IOException JavaDoc {
149     StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
150
151     retVal.append("<html><body>");
152
153     if (withHeaders)
154       retVal.append(getHeaderInformation(showFullHeaders, true));
155
156     if (textPart != null)
157       retVal.append(textPart.getHtml(withHeaders, showFullHeaders, maxLength, truncationMessage));
158
159     retVal.append("</body></html>");
160     return retVal.toString();
161   }
162
163   /**
164    * This returns the Attachments (basically, all the Parts in a Multipart
165    * except for the main body of the message).
166    */

167   public Vector getAttachments() {
168     return new Vector(allAttachments);
169   }
170
171   /**
172    * This returns the Attachments (basically, all the Parts in a Multipart
173    * except for the main body of the message) using the given
174    * messageLength to determine whether or not the main text part is an
175    * attachment or not.
176    */

177   public Vector getAttachments(int maxLength) {
178     if (textPart != null && textPart.getSize() >= maxLength) {
179       if (attachmentsAndTextPart != null)
180         return attachmentsAndTextPart;
181       else {
182         attachmentsAndTextPart = new Vector();
183         attachmentsAndTextPart.add(textPart);
184         attachmentsAndTextPart.addAll(allAttachments);
185         return new Vector(attachmentsAndTextPart);
186       }
187     } else
188       return new Vector(allAttachments);
189   }
190
191   /**
192    * Returns all attachments, including the text part.
193    */

194   public Vector getAttachmentsAndTextPart() {
195     if (attachmentsAndTextPart != null)
196       return new Vector(attachmentsAndTextPart);
197     else {
198       attachmentsAndTextPart = new Vector();
199       attachmentsAndTextPart.add(textPart);
200       attachmentsAndTextPart.addAll(allAttachments);
201       return new Vector(attachmentsAndTextPart);
202     }
203   }
204
205   /**
206    * This method returns the Message Text plus the text inline attachments.
207    * The attachments are separated by the separator flag.
208    */

209   public String JavaDoc getTextAndTextInlines(String JavaDoc separator, boolean withHeaders, boolean showFullHeaders, int maxLength, String JavaDoc truncationMessage) throws IOException JavaDoc {
210     StringBuffer JavaDoc returnValue = new StringBuffer JavaDoc();
211
212     if (withHeaders)
213       returnValue.append(getHeaderInformation(showFullHeaders, false));
214
215     if (textPart != null)
216       returnValue.append(textPart.getText(withHeaders, showFullHeaders, maxLength, truncationMessage));
217
218     if (allAttachments != null && allAttachments.size() > 0) {
219       for (int i = 0; i < allAttachments.size() ; i++) {
220         Attachment attach = (Attachment) allAttachments.elementAt(i);
221         if (attach.isPlainText()) {
222           returnValue.append(separator);
223           returnValue.append(attach.getText(withHeaders, showFullHeaders, maxLength, truncationMessage));
224         }
225       }
226     }
227
228     return returnValue.toString();
229   }
230
231
232   /**
233    * This method returns the Message HTML plus the text inline attachments.
234    * The attachments are separated by the separator flag.
235    */

236   public String JavaDoc getHtmlAndTextInlines(String JavaDoc separator, boolean withHeaders, boolean showFullHeaders, int maxLength, String JavaDoc truncationMessage) throws IOException JavaDoc {
237     StringBuffer JavaDoc returnValue = new StringBuffer JavaDoc();
238
239     returnValue.append("<html><body>");
240
241     if (withHeaders)
242       returnValue.append(getHeaderInformation(showFullHeaders, true));
243
244     if (textPart != null)
245       returnValue.append(textPart.getHtml(withHeaders, showFullHeaders, maxLength, truncationMessage));
246
247     if (allAttachments != null && allAttachments.size() > 0) {
248       for (int i = 0; i < allAttachments.size() ; i++) {
249         Attachment attach = (Attachment) allAttachments.elementAt(i);
250         if (attach.isPlainText()) {
251           returnValue.append(separator);
252           returnValue.append(attach.getText(withHeaders, showFullHeaders, maxLength, truncationMessage));
253         }
254       }
255     }
256
257     returnValue.append("</body></html>");
258     return returnValue.toString();
259   }
260
261   /**
262    * Sets the header source for this AttachmentBundle. This is used when
263    * we print out headers for the Message.
264    */

265   public void setHeaderSource(MimePart headerSource) throws MessagingException {
266     headers = parseHeaders(headerSource.getAllHeaders());
267     headerLines = parseHeaderLines(headerSource.getAllHeaderLines());
268   }
269
270   /**
271    * This returns the formatted header information for a message.
272    */

273   public StringBuffer JavaDoc getHeaderInformation (boolean showFullHeaders, boolean useHtml) {
274     if (headers != null) {
275       StringBuffer JavaDoc headerText = new StringBuffer JavaDoc();
276
277       if (showFullHeaders) {
278         if (useHtml) {
279           Enumeration allHdrs = headers.getAllHeaders();
280           while (allHdrs.hasMoreElements()) {
281             Header nextHeader = (Header) allHdrs.nextElement();
282             headerText.append("<b>" + nextHeader.getName() + ":</b>&nbsp;&nbsp;");
283             headerText.append(MailUtilities.escapeHtml(nextHeader.getValue()));
284
285             headerText.append("<br>\n");
286           }
287         } else {
288           Enumeration allHdrs = headers.getAllHeaderLines();
289           while (allHdrs.hasMoreElements()) {
290             headerText.append(MailUtilities.decodeText((String JavaDoc) allHdrs.nextElement()));
291             headerText.append('\n');
292           }
293         }
294       } else {
295         StringTokenizer tokens = new StringTokenizer(Pooka.getProperty("MessageWindow.Header.DefaultHeaders", "From:To:CC:Date:Subject"), ":");
296         String JavaDoc hdrLabel,currentHeader = null;
297         String JavaDoc hdrValue = null;
298
299         while (tokens.hasMoreTokens()) {
300           currentHeader=tokens.nextToken();
301           hdrLabel = Pooka.getProperty("MessageWindow.Header." + currentHeader + ".label", currentHeader);
302           hdrValue = MailUtilities.decodeText((String JavaDoc) headers.getHeader(Pooka.getProperty("MessageWindow.Header." + currentHeader + ".MIMEHeader", currentHeader), ":"));
303           if (hdrValue != null) {
304             if (useHtml) {
305               headerText.append("<b>" + hdrLabel + ":</b>&nbsp;&nbsp;");
306               headerText.append(MailUtilities.escapeHtml(hdrValue));
307
308               headerText.append("<br>\n");
309             } else {
310               headerText.append(hdrLabel + ": ");
311               headerText.append(hdrValue);
312
313               headerText.append("\n");
314             }
315           }
316         }
317       }
318       if (useHtml) {
319         String JavaDoc separator = Pooka.getProperty("MessageWindow.htmlSeparator", "<hr><br>");
320         headerText.append(separator);
321       } else {
322         String JavaDoc separator = Pooka.getProperty("MessageWindow.separator", "");
323         if (separator.equals(""))
324           headerText.append("\n\n");
325         else
326           headerText.append(separator);
327       }
328
329       return headerText;
330     } else {
331       return new StringBuffer JavaDoc();
332     }
333   }
334
335   /**
336    * Parses the Enumeration of Header objects into a HashMap.
337    */

338   private InternetHeaders parseHeaders(Enumeration pHeaders) {
339     InternetHeaders retVal = new InternetHeaders();
340     while (pHeaders.hasMoreElements()) {
341       Header hdr = (Header) pHeaders.nextElement();
342       retVal.addHeader(hdr.getName(), hdr.getValue());
343     }
344     return retVal;
345   }
346
347   /**
348    * Parses the Enumeration of header lines into a Vector.
349    */

350   private Vector parseHeaderLines(Enumeration pHeaderLines) {
351     Vector retVal = new Vector();
352     while (pHeaderLines.hasMoreElements())
353       retVal.add(pHeaderLines.nextElement());
354     return retVal;
355   }
356
357   /**
358    * Returns whether or not this attachment has an HTML version available.
359    */

360   public boolean containsHtml() {
361     if (textPart != null) {
362       if (textPart instanceof AlternativeAttachment) {
363         return true;
364       } else {
365         return textPart.getMimeType().match("text/html");
366       }
367     } else
368       return false;
369   }
370
371   /**
372    * Returns true if the main content of this message exists only as
373    * HTML.
374    */

375   public boolean isHtml() {
376     if (textPart != null) {
377       if (textPart instanceof AlternativeAttachment)
378         return false;
379       else
380         return (textPart.getMimeType().match("text/html"));
381     } else
382       return false;
383   }
384 }
385
Popular Tags