1 19 20 package gnu.mail.handler; 21 22 import java.awt.datatransfer.DataFlavor ; 23 import java.awt.datatransfer.UnsupportedFlavorException ; 24 import java.io.*; 25 import java.util.Properties ; 26 import javax.activation.*; 27 import javax.mail.MessageAware ; 28 import javax.mail.MessageContext ; 29 import javax.mail.MessagingException ; 30 import javax.mail.Session ; 31 import javax.mail.internet.MimeMessage ; 32 33 37 public abstract class Message 38 implements DataContentHandler 39 { 40 41 44 protected DataFlavor flavor; 45 46 51 protected Message(String mimeType, String description) 52 { 53 flavor = new ActivationDataFlavor(javax.mail.Message .class, mimeType, 54 description); 55 } 56 57 62 public DataFlavor [] getTransferDataFlavors() 63 { 64 DataFlavor [] flavors = new DataFlavor [1]; 65 flavors[0] = flavor; 66 return flavors; 67 } 68 69 77 public Object getTransferData(DataFlavor flavor, DataSource source) 78 throws UnsupportedFlavorException , IOException 79 { 80 if (this.flavor.equals(flavor)) 81 return getContent(source); 82 return null; 83 } 84 85 92 public Object getContent(DataSource source) 93 throws IOException 94 { 95 try 96 { 97 Session session = null; 98 if (source instanceof MessageAware ) 99 { 100 MessageAware ma = (MessageAware )source; 101 MessageContext context = ma.getMessageContext(); 102 session = context.getSession(); 103 } 104 else 105 { 106 Properties props = null; 107 108 session = Session.getDefaultInstance(props, null); 109 } 110 InputStream in = source.getInputStream(); 111 return new MimeMessage (session, in); 112 } 113 catch (MessagingException e) 114 { 115 e.printStackTrace(); 116 } 117 catch (IOException e) 118 { 119 e.printStackTrace(); 120 } 121 return null; 122 } 123 124 131 public void writeTo(Object object, String mimeType, OutputStream out) 132 throws IOException 133 { 134 if (object instanceof javax.mail.Message ) 135 { 136 try 137 { 138 ((javax.mail.Message )object).writeTo(out); 139 } 140 catch (MessagingException e) 141 { 142 143 throw new IOException(e.getMessage()); 144 } 145 } 146 else 147 throw new UnsupportedDataTypeException(); 148 } 149 150 } 151 | Popular Tags |