1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.geronimo.mail.handlers;
17
18 import javax.activation.ActivationDataFlavor;
19 import javax.activation.DataContentHandler;
20 import javax.activation.DataSource;
21 import java.awt.datatransfer.DataFlavor;
22 import java.io.IOException;
23 import java.io.OutputStream;
24
25 import javax.mail.internet.MimeMultipart;
26 import javax.mail.internet.MimeMessage;
27 import javax.mail.MessagingException;
28
29 public class MultipartHandler implements DataContentHandler {
30 /**
31 * Field dataFlavor
32 */
33 ActivationDataFlavor dataFlavor;
34
35 public MultipartHandler(){
36 dataFlavor = new ActivationDataFlavor(javax.mail.internet.MimeMultipart.class, "multipart/mixed", "Multipart");
37 }
38
39 /**
40 * Constructor TextHandler
41 *
42 * @param dataFlavor
43 */
44 public MultipartHandler(ActivationDataFlavor dataFlavor) {
45 this.dataFlavor = dataFlavor;
46 }
47
48 /**
49 * Method getDF
50 *
51 * @return dataflavor
52 */
53 protected ActivationDataFlavor getDF() {
54 return dataFlavor;
55 }
56
57 /**
58 * Method getTransferDataFlavors
59 *
60 * @return dataflavors
61 */
62 public DataFlavor[] getTransferDataFlavors() {
63 return (new DataFlavor[]{dataFlavor});
64 }
65
66 /**
67 * Method getTransferData
68 *
69 * @param dataflavor
70 * @param datasource
71 * @return
72 * @throws IOException
73 */
74 public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
75 throws IOException {
76 if (getDF().equals(dataflavor)) {
77 return getContent(datasource);
78 }
79 return null;
80 }
81
82 /**
83 * Method getContent
84 *
85 * @param datasource
86 * @return
87 * @throws IOException
88 */
89 public Object getContent(DataSource datasource) throws IOException {
90 try {
91 return new MimeMultipart(datasource);
92 } catch (MessagingException e) {
93
94
95 return null;
96 }
97 }
98
99 /**
100 * Method writeTo
101 *
102 * @param object
103 * @param s
104 * @param outputstream
105 * @throws IOException
106 */
107 public void writeTo(Object object, String s, OutputStream outputstream) throws IOException {
108
109 if (object instanceof MimeMultipart) {
110 try {
111 ((MimeMultipart)object).writeTo(outputstream);
112 } catch (MessagingException e) {
113
114 throw new IOException("Exception writing MimeMultipart: " + e.toString());
115 }
116 }
117 }
118 }