1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.mail.internet;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.UnknownServiceException;
26 import javax.activation.DataSource;
27 import javax.mail.MessageAware;
28 import javax.mail.MessageContext;
29 import javax.mail.MessagingException;
30
31
32
33
34 public class MimePartDataSource implements DataSource, MessageAware {
35
36 protected MimePart part;
37
38 public MimePartDataSource(MimePart part) {
39 this.part = part;
40 }
41
42 public InputStream getInputStream() throws IOException {
43 try {
44 InputStream stream;
45 if (part instanceof MimeMessage) {
46
47 return ((MimeMessage) part).getContentStream();
48 } else if (part instanceof MimeBodyPart) {
49 stream = ((MimeBodyPart) part).getContentStream();
50 } else {
51 throw new MessagingException("Unknown part");
52 }
53 return checkPartEncoding(part, stream);
54 } catch (MessagingException e) {
55 throw (IOException) new IOException(e.getMessage()).initCause(e);
56 }
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71 private InputStream checkPartEncoding(MimePart part, InputStream stream) throws MessagingException {
72 String encoding = part.getEncoding();
73
74 if (encoding == null) {
75 return stream;
76 }
77
78 encoding = encoding.toLowerCase();
79 if (encoding.equals("7bit") || encoding.equals("8bit") || encoding.equals("binary")) {
80 return stream;
81 }
82
83
84
85 String contentType = part.getContentType();
86 if (contentType != null) {
87 try {
88 ContentType type = new ContentType(contentType);
89
90 if (type.match("multipart/*")) {
91 return stream;
92 }
93 } catch (ParseException e) {
94
95 }
96 }
97
98 return MimeUtility.decode(stream, encoding);
99 }
100
101
102 public OutputStream getOutputStream() throws IOException {
103 throw new UnknownServiceException();
104 }
105
106 public String getContentType() {
107 try {
108 return part.getContentType();
109 } catch (MessagingException e) {
110 return null;
111 }
112 }
113
114 public String getName() {
115 return "";
116 }
117
118 public synchronized MessageContext getMessageContext() {
119 return new MessageContext(part);
120 }
121 }