KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka;
2 import javax.mail.*;
3 import javax.mail.internet.*;
4 import javax.activation.DataHandler JavaDoc;
5 import java.io.*;
6 import java.util.*;
7
8 public class AlternativeAttachment extends Attachment {
9   DataHandler JavaDoc htmlHandler;
10   
11   /**
12    * Creates an Attachment out of two MimeBodyParts, where one is the
13    * text content and the other is the HTML content.
14    */

15   public AlternativeAttachment(MimeBodyPart textPart, MimeBodyPart htmlPart) throws MessagingException {
16     super(textPart);
17     htmlHandler = htmlPart.getDataHandler();
18   }
19   
20   public boolean isText() {
21     return true;
22   }
23   
24   public boolean isPlainText() {
25     return true;
26   }
27   
28   public boolean isHtml() {
29     return true;
30   }
31   
32   /**
33    * Returns the html of the Attachment, up to maxLength bytes. If
34    * the content is truncated, then append the truncationMessage at the
35    * end of the content displayed.
36    *
37    * If withHeaders is set, then show the Headers to go with this message.
38    * If showFullHeaders is also set, then show all the headers.
39    */

40   public String JavaDoc getHtml(boolean withHeaders, boolean showFullHeaders, int maxLength, String JavaDoc truncationMessage) throws java.io.IOException JavaDoc {
41     StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
42     if (withHeaders)
43       retVal.append(getHeaderInformation(showFullHeaders));
44     
45     retVal.append(getHtml(maxLength, truncationMessage));
46     
47     return retVal.toString();
48   }
49   
50   /**
51    * Returns the Html content of this message, up to maxLength
52    * bytes.
53    */

54   String JavaDoc getHtml(int maxLength, String JavaDoc truncationMessage) throws IOException {
55     if (maxLength >= size) {
56       try {
57     String JavaDoc retVal = (String JavaDoc) htmlHandler.getContent();
58     return retVal;
59     //return (String) htmlHandler.getContent();
60
} catch (UnsupportedEncodingException uee) {
61     /**
62      * Just read the InputStream directly into a byte array and
63      * hope for the best. :)
64      */

65     
66     InputStream is = htmlHandler.getInputStream();
67     ByteArrayOutputStream bos = new ByteArrayOutputStream();
68     int b;
69     while ((b = is.read()) != -1)
70       bos.write(b);
71     byte[] barray = bos.toByteArray();
72     return new String JavaDoc(barray, Pooka.getProperty("Pooka.defaultCharset", "iso-8859-1"));
73       }
74     } else {
75       int written = 0;
76       InputStream decodedIS = null;
77       ByteArrayOutputStream outStream = new ByteArrayOutputStream();
78       
79       decodedIS = getInputStream();
80       
81       int b=0;
82       byte[] buf = new byte[16384];
83     
84       b = decodedIS.read(buf);
85       while (b != -1 && written < maxLength) {
86     if (b <= (maxLength - written)) {
87       outStream.write(buf, 0, b);
88       written = written + b;
89     } else {
90       outStream.write(buf, 0, (maxLength - written));
91       written = maxLength;
92     }
93     b = decodedIS.read(buf);
94       }
95       
96       byte[] barray = outStream.toByteArray();
97       String JavaDoc content;
98       try {
99     content = new String JavaDoc(barray, Pooka.getProperty("Pooka.defaultCharset", "iso-8859-1"));
100       } catch (UnsupportedEncodingException uee) {
101     content = new String JavaDoc(barray, Pooka.getProperty("Pooka.defaultCharset", "iso-8859-1"));
102       }
103       
104       return content + "\n" + truncationMessage + "\n";
105     }
106     
107   }
108   
109 }
110
111
Popular Tags