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 stream = ((MimeMessage) part).getContentStream();
47 } else if (part instanceof MimeBodyPart) {
48 stream = ((MimeBodyPart) part).getContentStream();
49 } else {
50 throw new MessagingException("Unknown part");
51 }
52 return checkPartEncoding(part, stream);
53 } catch (MessagingException e) {
54 throw (IOException) new IOException(e.getMessage()).initCause(e);
55 }
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70 private InputStream checkPartEncoding(MimePart part, InputStream stream) throws MessagingException {
71 String encoding = part.getEncoding();
72
73 if (encoding == null) {
74 return stream;
75 }
76
77 encoding = encoding.toLowerCase();
78 if (encoding.equals("7bit") || encoding.equals("8bit") || encoding.equals("binary")) {
79 return stream;
80 }
81
82
83
84 String contentType = part.getContentType();
85 if (contentType != null) {
86 try {
87 ContentType type = new ContentType(contentType);
88
89 if (type.match("multipart/*")) {
90 return stream;
91 }
92 } catch (ParseException e) {
93
94 }
95 }
96
97 return MimeUtility.decode(stream, encoding);
98 }
99
100
101 public OutputStream getOutputStream() throws IOException {
102 throw new UnknownServiceException();
103 }
104
105 public String getContentType() {
106 try {
107 return part.getContentType();
108 } catch (MessagingException e) {
109 return null;
110 }
111 }
112
113 public String getName() {
114 return "";
115 }
116
117 public synchronized MessageContext getMessageContext() {
118 return new MessageContext(part);
119 }
120 }