1 /*
2 * Copyright (c) 1997, 2012, 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 package com.sun.xml.internal.messaging.saaj.soap;
27
28 import java.awt.datatransfer.DataFlavor;
29 import java.io.*;
30
31 import javax.activation.*;
32 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
33 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentType;
34
35 /**
36 * JAF data content handler for text/plain --> String
37 *
38 */
39 public 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 public DataFlavor[] getTransferDataFlavors() {
55 return new DataFlavor[] { getDF() };
56 }
57
58 /**
59 * Return the Transfer Data of type DataFlavor from InputStream.
60 *
61 * @param df The DataFlavor
62 * @param ds The DataSource corresponding to the data
63 * @return String object
64 */
65 public Object getTransferData(DataFlavor df, DataSource ds)
66 throws IOException {
67 // use myDF.equals to be sure to get ActivationDataFlavor.equals,
68 // which properly ignores Content-Type parameters in comparison
69 if (getDF().equals(df))
70 return getContent(ds);
71 else
72 return null;
73 }
74
75 public Object getContent(DataSource ds) throws IOException {
76 String enc = null;
77 InputStreamReader is = null;
78
79 try {
80 enc = getCharset(ds.getContentType());
81 is = new InputStreamReader(ds.getInputStream(), enc);
82 } catch (IllegalArgumentException iex) {
83 /*
84 * An unknown charset of the form ISO-XXX-XXX will cause
85 * the JDK to throw an IllegalArgumentException. The
86 * JDK will attempt to create a classname using this string,
87 * but valid classnames must not contain the character '-',
88 * and this results in an IllegalArgumentException, rather than
89 * the expected UnsupportedEncodingException. Yikes.
90 */
91 throw new UnsupportedEncodingException(enc);
92 }
93
94 try {
103 if (size < 256*1024)
104 size += size;
105 else
106 size += 256*1024;
107 char tbuf[] = new char[size];
108 System.arraycopy(buf, 0, tbuf, 0, pos);
109 buf = tbuf;
110 }
111 }
112 return new String(buf, 0, pos);
113 } finally {
114 try {
115 is.close();
116 } catch (IOException ex) { }
117 }
118 }
119
120 /**
121 * Write the object to the output stream, using the specified MIME type.
122 */
123 public void writeTo(Object obj, String type, OutputStream os)
124 throws IOException {
125 if (!(obj instanceof String))
126 throw new IOException("\"" + getDF().getMimeType() +
127 "\" DataContentHandler requires String object, " +
128 "was given object of type " + obj.getClass().toString());
129
130 String enc = null;
131 OutputStreamWriter osw = null;
132
133 try {
134 enc = getCharset(type);
135 osw = new OutputStreamWriter(os, enc);
136 } catch (IllegalArgumentException iex) {
137 /*
138 * An unknown charset of the form ISO-XXX-XXX will cause
139 * the JDK to throw an IllegalArgumentException. The
140 * JDK will attempt to create a classname using this string,
141 * but valid classnames must not contain the character '-',
142 * and this results in an IllegalArgumentException, rather than
|
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 package com.sun.xml.internal.messaging.saaj.soap;
27
28 import java.awt.datatransfer.DataFlavor;
29 import java.io.*;
30
31 import javax.activation.*;
32 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
33 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentType;
34
35 /**
36 * JAF data content handler for text/plain --> String
37 *
38 */
39 public 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 {
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
|