1 package net.suberic.pooka; 2 import javax.mail.*; 3 import javax.mail.internet.*; 4 import javax.activation.DataHandler ; 5 import java.io.*; 6 import java.util.*; 7 8 public class AlternativeAttachment extends Attachment { 9 DataHandler htmlHandler; 10 11 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 40 public String getHtml(boolean withHeaders, boolean showFullHeaders, int maxLength, String truncationMessage) throws java.io.IOException { 41 StringBuffer retVal = new StringBuffer (); 42 if (withHeaders) 43 retVal.append(getHeaderInformation(showFullHeaders)); 44 45 retVal.append(getHtml(maxLength, truncationMessage)); 46 47 return retVal.toString(); 48 } 49 50 54 String getHtml(int maxLength, String truncationMessage) throws IOException { 55 if (maxLength >= size) { 56 try { 57 String retVal = (String ) htmlHandler.getContent(); 58 return retVal; 59 } catch (UnsupportedEncodingException uee) { 61 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 (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 content; 98 try { 99 content = new String (barray, Pooka.getProperty("Pooka.defaultCharset", "iso-8859-1")); 100 } catch (UnsupportedEncodingException uee) { 101 content = new String (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 |