1 17 package org.apache.james.util.mail.handlers; 18 19 import java.awt.datatransfer.DataFlavor ; 20 import java.awt.datatransfer.UnsupportedFlavorException ; 21 import java.io.IOException ; 22 23 import javax.activation.ActivationDataFlavor ; 24 import javax.activation.DataContentHandler ; 25 import javax.activation.DataSource ; 26 import javax.mail.MessagingException ; 27 28 29 32 public abstract class AbstractDataContentHandler implements DataContentHandler 33 { 34 35 private ActivationDataFlavor fieldDataFlavor; 36 37 40 public AbstractDataContentHandler() 41 { 42 super(); 43 } 44 45 49 protected void updateDataFlavor() 50 { 51 setDataFlavor(computeDataFlavor()); 52 } 53 54 59 abstract protected ActivationDataFlavor computeDataFlavor(); 60 61 protected void setDataFlavor(ActivationDataFlavor aDataFlavor) 62 { 63 fieldDataFlavor = aDataFlavor; 64 } 65 66 69 public Object getContent(DataSource aDataSource) throws IOException 70 { 71 Object content = null; 72 try 73 { 74 content = computeContent(aDataSource); 75 } 76 catch (MessagingException e) 77 { 78 } 80 return content; 81 } 82 83 90 abstract protected Object computeContent(DataSource aDataSource) 91 throws MessagingException ; 92 93 97 public Object getTransferData(DataFlavor aDataFlavor, DataSource aDataSource) 98 throws UnsupportedFlavorException , IOException 99 { 100 Object content = null; 101 if (getDataFlavor().equals(aDataFlavor)) 102 content = getContent(aDataSource); 103 return content; 104 } 105 106 109 public DataFlavor [] getTransferDataFlavors() 110 { 111 return new DataFlavor []{getDataFlavor()}; 112 } 113 114 119 protected ActivationDataFlavor getDataFlavor() 120 { 121 ActivationDataFlavor dataFlavor = null; 122 if (null == (dataFlavor = getDataFlavorBasic())) 123 { 124 updateDataFlavor(); 125 return getDataFlavor(); 126 } 127 return dataFlavor; 128 } 129 130 135 private ActivationDataFlavor getDataFlavorBasic() 136 { 137 return fieldDataFlavor; 138 } 139 140 } 141 | Popular Tags |