1 17 package org.apache.james.util.mail.handlers; 18 19 import java.io.BufferedReader ; 20 import java.io.BufferedWriter ; 21 import java.io.IOException ; 22 import java.io.InputStreamReader ; 23 import java.io.OutputStream ; 24 import java.io.OutputStreamWriter ; 25 import java.io.Reader ; 26 import java.io.StringWriter ; 27 import java.io.UnsupportedEncodingException ; 28 import java.io.Writer ; 29 30 import javax.activation.ActivationDataFlavor ; 31 import javax.activation.DataSource ; 32 import javax.mail.MessagingException ; 33 import javax.mail.internet.ContentType ; 34 import javax.mail.internet.MimeUtility ; 35 import javax.mail.internet.ParseException ; 36 37 44 public class message_disposition_notification 45 extends 46 AbstractDataContentHandler 47 { 48 49 52 public message_disposition_notification() 53 { 54 super(); 55 } 56 57 60 protected ActivationDataFlavor computeDataFlavor() 61 { 62 return new ActivationDataFlavor (String .class, 63 "message/disposition-notification", "Message String"); 64 } 65 66 69 protected Object computeContent(DataSource aDataSource) 70 throws MessagingException 71 { 72 String encoding = getCharacterSet(aDataSource.getContentType()); 73 Reader reader = null; 74 Writer writer = new StringWriter (2048); 75 String content = null; 76 try 77 { 78 reader = new BufferedReader (new InputStreamReader (aDataSource 79 .getInputStream(), encoding), 2048); 80 while (reader.ready()) 81 writer.write(reader.read()); 82 writer.flush(); 83 content = writer.toString(); 84 } 85 catch (IllegalArgumentException e) 86 { 87 throw new MessagingException ("Encoding = \"" + encoding + "\"", e); 88 } 89 catch (IOException e) 90 { 91 throw new MessagingException ( 92 "Exception obtaining content from DataSource", e); 93 } 94 finally 95 { 96 try 97 { 98 writer.close(); 99 } 100 catch (IOException e1) 101 { 102 } 104 } 105 return content; 106 } 107 108 112 public void writeTo(Object aPart, String aMimeType, OutputStream aStream) 113 throws IOException 114 { 115 if (!(aPart instanceof String )) 116 throw new IOException ("Type \"" + aPart.getClass().getName() 117 + "\" is not supported."); 118 119 String encoding = getCharacterSet(getDataFlavor().getMimeType()); 120 Writer writer = null; 121 try 122 { 123 writer = new BufferedWriter (new OutputStreamWriter (aStream, 124 encoding), 2048); 125 } 126 catch (IllegalArgumentException e) 127 { 128 throw new UnsupportedEncodingException (encoding); 129 } 130 writer.write((String ) aPart); 131 writer.flush(); 132 } 133 134 protected String getCharacterSet(String aType) 135 { 136 String characterSet = null; 137 try 138 { 139 characterSet = new ContentType (aType).getParameter("charset"); 140 } 141 catch (ParseException e) 142 { 143 } 145 finally 146 { 147 if (null == characterSet) 148 characterSet = "us-ascii"; 149 } 150 return MimeUtility.javaCharset(characterSet); 151 } 152 153 } 154 | Popular Tags |