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
23 * questions.
24 */
25
26/*
27 * @(#)MimePartDataSource.java        1.9 02/03/27
28 */
29
30
31package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
32
33import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
34
35import javax.activation.DataSource;
36import java.io.IOException;
37import java.io.InputStream;
38import java.io.OutputStream;
39import java.net.UnknownServiceException;
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
48public 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}
124