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