1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
33
34 import java.io.*;
35 import java.net.UnknownServiceException;
36
37 import javax.activation.DataSource;
38
39 import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
40
41 /**
42 * A utility class that implements a DataSource out of
43 * a MimeBodyPart. This class is primarily meant for service providers.
44 *
45 * @author John Mani
46 */
47
48 public final class MimePartDataSource implements DataSource {
49 private final MimeBodyPart part;
50
51 /**
52 * Constructor, that constructs a DataSource from a MimeBodyPart.
53 */
54 public MimePartDataSource(MimeBodyPart part) {
55 this.part = part;
56 }
57
58 /**
59 * Returns an input stream from this MimeBodyPart. <p>
60 *
61 * This method applies the appropriate transfer-decoding, based
62 * on the Content-Transfer-Encoding attribute of this MimeBodyPart.
63 * Thus the returned input stream is a decoded stream of bytes.<p>
64 *
65 * This implementation obtains the raw content from the MimeBodyPart
66 * using the <code>getContentStream()</code> method and decodes
67 * it using the <code>MimeUtility.decode()</code> method.
68 *
69 * @return decoded input stream
70 */
71 public InputStream getInputStream() throws IOException {
72
73 try {
74 InputStream is = part.getContentStream();
75
76 String encoding = part.getEncoding();
77 if (encoding != null)
78 return MimeUtility.decode(is, encoding);
79 else
80 return is;
81 } catch (MessagingException mex) {
82 throw new IOException(mex.getMessage());
83 }
84 }
85
86 /**
87 * DataSource method to return an output stream. <p>
88 *
89 * This implementation throws the UnknownServiceException.
90 */
91 public OutputStream getOutputStream() throws IOException {
92 throw new UnknownServiceException();
93 }
94
95 /**
96 * Returns the content-type of this DataSource. <p>
97 *
98 * This implementation just invokes the <code>getContentType</code>
99 * method on the MimeBodyPart.
100 */
101 public String getContentType() {
102 return part.getContentType();
103 }
104
105 /**
106 * DataSource method to return a name. <p>
107 *
108 * This implementation just returns an empty string.
109 */
110 public String getName() {
111 try {
112 return part.getFileName();
113 } catch (MessagingException mex) {
114 return "";
115 }
116 }
117 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
33
34 import java.io.*;
35 import java.net.UnknownServiceException;
36
37 import javax.activation.DataSource;
38
39 import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
40
41 /**
42 * A utility class that implements a DataSource out of
43 * a MimeBodyPart. This class is primarily meant for service providers.
44 *
45 * @author John Mani
46 */
47
48 public final class MimePartDataSource implements DataSource {
49 private final MimeBodyPart part;
50
51 /**
52 * Constructor, that constructs a DataSource from a MimeBodyPart.
53 *
54 * @param part body part
55 */
56 public MimePartDataSource(MimeBodyPart part) {
57 this.part = part;
58 }
59
60 /**
61 * Returns an input stream from this MimeBodyPart. <p>
62 *
63 * This method applies the appropriate transfer-decoding, based
64 * on the Content-Transfer-Encoding attribute of this MimeBodyPart.
65 * Thus the returned input stream is a decoded stream of bytes.<p>
66 *
67 * This implementation obtains the raw content from the MimeBodyPart
68 * using the <code>getContentStream()</code> method and decodes
69 * it using the <code>MimeUtility.decode()</code> method.
70 *
71 * @return decoded input stream
72 */
73 @Override
74 public InputStream getInputStream() throws IOException {
75
76 try {
77 InputStream is = part.getContentStream();
78
79 String encoding = part.getEncoding();
80 if (encoding != null)
81 return MimeUtility.decode(is, encoding);
82 else
83 return is;
84 } catch (MessagingException mex) {
85 throw new IOException(mex.getMessage());
86 }
87 }
88
89 /**
90 * DataSource method to return an output stream. <p>
91 *
92 * This implementation throws the UnknownServiceException.
93 */
94 @Override
95 public OutputStream getOutputStream() throws IOException {
96 throw new UnknownServiceException();
97 }
98
99 /**
100 * Returns the content-type of this DataSource. <p>
101 *
102 * This implementation just invokes the <code>getContentType</code>
103 * method on the MimeBodyPart.
104 */
105 @Override
106 public String getContentType() {
107 return part.getContentType();
108 }
109
110 /**
111 * DataSource method to return a name. <p>
112 *
113 * This implementation just returns an empty string.
114 */
115 @Override
116 public String getName() {
117 try {
118 return part.getFileName();
119 } catch (MessagingException mex) {
120 return "";
121 }
122 }
123 }
|