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
26package com.sun.xml.internal.messaging.saaj.soap;
27
28import java.io.*;
29import java.awt.datatransfer.DataFlavor;
30import javax.activation.*;
31import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart;
32import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentType;
33import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
34
35public class MultipartDataContentHandler implements DataContentHandler {
36    private ActivationDataFlavor myDF = new ActivationDataFlavor(
37            com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart.class,
38            "multipart/mixed",
39            "Multipart");
40
41    /**
42     * Return the DataFlavors for this <code>DataContentHandler</code>.
43     *
44     * @return The DataFlavors
45     */
46    @Override
47    public DataFlavor[] getTransferDataFlavors() { // throws Exception;
48        return new DataFlavor[] { myDF };
49    }
50
51    /**
52     * Return the Transfer Data of type DataFlavor from InputStream.
53     *
54     * @param df The DataFlavor
55     * @param ds The DataSource
56     * @return String object
57     */
58    @Override
59    public Object getTransferData(DataFlavor df, DataSource ds) {
60        // use myDF.equals to be sure to get ActivationDataFlavor.equals,
61        // which properly ignores Content-Type parameters in comparison
62        if (myDF.equals(df))
63            return getContent(ds);
64        else
65            return null;
66    }
67
68    /**
69     * Return the content.
70     *
71     * @param ds The DataSource
72     * @return content
73     */
74    @Override
75    public Object getContent(DataSource ds) {
76        try {
77            return new MimeMultipart(
78                ds, new ContentType(ds.getContentType()));
79        } catch (Exception e) {
80            return null;
81        }
82    }
83
84    /**
85     * Write the object to the output stream, using the specific MIME type.
86     */
87    @Override
88    public void writeTo(Object obj, String mimeType, OutputStream os)
89                        throws IOException {
90        if (obj instanceof MimeMultipart) {
91            try {
92                //TODO: temporarily allow only ByteOutputStream
93                // Need to add writeTo(OutputStream) on MimeMultipart
94                ByteOutputStream baos = null;
95                if (os instanceof ByteOutputStream) {
96                    baos = (ByteOutputStream)os;
97                } else {
98                    throw new IOException("Input Stream expected to be a com.sun.xml.internal.messaging.saaj.util.ByteOutputStream, but found " +
99                        os.getClass().getName());
100                }
101                ((MimeMultipart)obj).writeTo(baos);
102            } catch (Exception e) {
103                throw new IOException(e.toString());
104            }
105        }
106    }
107}
108