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.awt.datatransfer.DataFlavor;
29import java.io.*;
30
31import javax.activation.*;
32import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
33import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentType;
34
35/**
36 * JAF data content handler for text/plain --> String
37 *
38 */
39public class StringDataContentHandler implements DataContentHandler {
40    private static ActivationDataFlavor myDF = new ActivationDataFlavor(
41        java.lang.String.class,
42        "text/plain",
43        "Text String");
44
45    protected ActivationDataFlavor getDF() {
46        return myDF;
47    }
48
49    /**
50     * Return the DataFlavors for this <code>DataContentHandler</code>.
51     *
52     * @return The DataFlavors
53     */
54    @Override
55    public DataFlavor[] getTransferDataFlavors() {
56        return new DataFlavor[] { getDF() };
57    }
58
59    /**
60     * Return the Transfer Data of type DataFlavor from InputStream.
61     *
62     * @param df The DataFlavor
63     * @param ds The DataSource corresponding to the data
64     * @return String object
65     */
66    @Override
67    public Object getTransferData(DataFlavor df, DataSource ds)
68                        throws IOException {
69        // use myDF.equals to be sure to get ActivationDataFlavor.equals,
70        // which properly ignores Content-Type parameters in comparison
71        if (getDF().equals(df))
72            return getContent(ds);
73        else
74            return null;
75    }
76
77    @Override
78    public Object getContent(DataSource ds) throws IOException {
79        String enc = null;
80        InputStreamReader is = null;
81
82        try {
83            enc = getCharset(ds.getContentType());
84            is = new InputStreamReader(ds.getInputStream(), enc);
85        } catch (IllegalArgumentException iex) {
86            /*
87             * An unknown charset of the form ISO-XXX-XXX will cause
88             * the JDK to throw an IllegalArgumentException.  The
89             * JDK will attempt to create a classname using this string,
90             * but valid classnames must not contain the character '-',
91             * and this results in an IllegalArgumentException, rather than
92             * the expected UnsupportedEncodingException.  Yikes.
93             */
94            throw new UnsupportedEncodingException(enc);
95        }
96
97        try {
98            int pos = 0;
99            int count;
100            char buf[] = new char[1024];
101
102            while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
103                pos += count;
104                if (pos >= buf.length) {
105                    int size = buf.length;
106                    if (size < 256*1024)
107                        size += size;
108                    else
109                        size += 256*1024;
110                    char tbuf[] = new char[size];
111                    System.arraycopy(buf, 0, tbuf, 0, pos);
112                    buf = tbuf;
113                }
114            }
115            return new String(buf, 0, pos);
116        } finally {
117            try {
118                is.close();
119            } catch (IOException ex) { }
120        }
121    }
122
123    /**
124     * Write the object to the output stream, using the specified MIME type.
125     */
126    @Override
127    public void writeTo(Object obj, String type, OutputStream os)
128                        throws IOException {
129        if (!(obj instanceof String))
130            throw new IOException("\"" + getDF().getMimeType() +
131                "\" DataContentHandler requires String object, " +
132                "was given object of type " + obj.getClass().toString());
133
134        String enc = null;
135        OutputStreamWriter osw = null;
136
137        try {
138            enc = getCharset(type);
139            osw = new OutputStreamWriter(os, enc);
140        } catch (IllegalArgumentException iex) {
141            /*
142             * An unknown charset of the form ISO-XXX-XXX will cause
143             * the JDK to throw an IllegalArgumentException.  The
144             * JDK will attempt to create a classname using this string,
145             * but valid classnames must not contain the character '-',
146             * and this results in an IllegalArgumentException, rather than
147             * the expected UnsupportedEncodingException.  Yikes.
148             */
149            throw new UnsupportedEncodingException(enc);
150        }
151
152        String s = (String)obj;
153        osw.write(s, 0, s.length());
154        osw.flush();
155    }
156
157    private String getCharset(String type) {
158        try {
159            ContentType ct = new ContentType(type);
160            String charset = ct.getParameter("charset");
161            if (charset == null)
162                // If the charset parameter is absent, use US-ASCII.
163                charset = "us-ascii";
164            return MimeUtility.javaCharset(charset);
165        } catch (Exception ex) {
166            return null;
167        }
168    }
169
170}
171