Skip to content

Package: MimePartDataSource

MimePartDataSource

nameinstructionbranchcomplexitylinemethod
MimePartDataSource(MimePart)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getContentType()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getInputStream()
M: 60 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
getMessageContext()
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getName()
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getOutputStream()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail.internet;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.OutputStream;
22: import java.net.UnknownServiceException;
23:
24: import jakarta.activation.DataSource;
25: import jakarta.mail.FolderClosedException;
26: import jakarta.mail.MessageAware;
27: import jakarta.mail.MessageContext;
28: import jakarta.mail.MessagingException;
29:
30:
31:
32: /**
33: * A utility class that implements a DataSource out of
34: * a MimePart. This class is primarily meant for service providers.
35: *
36: * @see                jakarta.mail.internet.MimePart
37: * @see                jakarta.activation.DataSource
38: * @author         John Mani
39: */
40:
41: public class MimePartDataSource implements DataSource, MessageAware {
42: /**
43: * The MimePart that provides the data for this DataSource.
44: *
45: * @since        JavaMail 1.4
46: */
47: protected MimePart part;
48:
49: private MessageContext context;
50:
51: /**
52: * Constructor, that constructs a DataSource from a MimePart.
53: *
54: * @param        part        the MimePart
55: */
56: public MimePartDataSource(MimePart part) {
57:         this.part = part;
58: }
59:
60: /**
61: * Returns an input stream from this MimePart. <p>
62: *
63: * This method applies the appropriate transfer-decoding, based
64: * on the Content-Transfer-Encoding attribute of this MimePart.
65: * Thus the returned input stream is a decoded stream of bytes.<p>
66: *
67: * This implementation obtains the raw content from the Part
68: * using the <code>getContentStream()</code> method and decodes
69: * it using the <code>MimeUtility.decode()</code> method.
70: *
71: * @see        jakarta.mail.internet.MimeMessage#getContentStream
72: * @see        jakarta.mail.internet.MimeBodyPart#getContentStream
73: * @see        jakarta.mail.internet.MimeUtility#decode
74: * @return         decoded input stream
75: */
76: @Override
77: public InputStream getInputStream() throws IOException {
78:         InputStream is;
79:
80:         try {
81:•         if (part instanceof MimeBodyPart)
82:                 is = ((MimeBodyPart)part).getContentStream();
83:•         else if (part instanceof MimeMessage)
84:                 is = ((MimeMessage)part).getContentStream();
85:          else
86:                 throw new MessagingException("Unknown part");
87:         
88:          String encoding =
89:                 MimeBodyPart.restrictEncoding(part, part.getEncoding());
90:•         if (encoding != null)
91:                 return MimeUtility.decode(is, encoding);
92:          else
93:                 return is;
94:         } catch (FolderClosedException fex) {
95:                 throw new IOException(new FolderClosedException(fex.getFolder(), fex.getMessage(), fex));
96:         } catch (MessagingException mex) {
97:                 // Cause is removed because in upper stack the cause is checked. See test POP3FolderClosedExceptionTest
98:          throw new IOException(mex.getMessage());
99:         }
100: }
101:
102: /**
103: * DataSource method to return an output stream. <p>
104: *
105: * This implementation throws the UnknownServiceException.
106: */
107: @Override
108: public OutputStream getOutputStream() throws IOException {
109:         throw new UnknownServiceException("Writing not supported");
110: }
111:
112: /**
113: * Returns the content-type of this DataSource. <p>
114: *
115: * This implementation just invokes the <code>getContentType</code>
116: * method on the MimePart.
117: */
118: @Override
119: public String getContentType() {
120:         try {
121:          return part.getContentType();
122:         } catch (MessagingException mex) {
123:          // would like to be able to reflect the exception to the
124:          // application, but since we can't do that we return a
125:          // generic "unknown" value here and hope for another
126:          // exception later.
127:          return "application/octet-stream";
128:         }
129: }
130:
131: /**
132: * DataSource method to return a name. <p>
133: *
134: * This implementation just returns an empty string.
135: */
136: @Override
137: public String getName() {
138:         try {
139:•         if (part instanceof MimeBodyPart)
140:                 return ((MimeBodyPart)part).getFileName();
141:         } catch (MessagingException mex) {
142:          // ignore it
143:         }
144:         return "";
145: }
146:
147: /**
148: * Return the <code>MessageContext</code> for the current part.
149: * @since JavaMail 1.1
150: */
151: @Override
152: public synchronized MessageContext getMessageContext() {
153:•        if (context == null)
154:          context = new MessageContext(part);
155:         return context;
156: }
157: }