author | mkos |
Mon, 31 Mar 2014 10:43:20 +0200 | |
changeset 23782 | 953bfc3fbe31 |
parent 22679 | d785acd84a14 |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22679
d785acd84a14
8032639: Update copyright year to match last edit in jaxws repository for 2013
mkos
parents:
16791
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
12009 | 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.util.ASCIIUtility; |
|
34 |
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentType; |
|
35 |
||
36 |
/** |
|
37 |
* JAF data content handler for text/plain --> String |
|
38 |
* |
|
39 |
*/ |
|
40 |
public class StringDataContentHandler implements DataContentHandler { |
|
41 |
private static ActivationDataFlavor myDF = new ActivationDataFlavor( |
|
42 |
java.lang.String.class, |
|
43 |
"text/plain", |
|
44 |
"Text String"); |
|
45 |
||
46 |
protected ActivationDataFlavor getDF() { |
|
47 |
return myDF; |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* Return the DataFlavors for this <code>DataContentHandler</code>. |
|
52 |
* |
|
53 |
* @return The DataFlavors |
|
54 |
*/ |
|
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 |
public Object getTransferData(DataFlavor df, DataSource ds) |
|
67 |
throws IOException { |
|
68 |
// use myDF.equals to be sure to get ActivationDataFlavor.equals, |
|
69 |
// which properly ignores Content-Type parameters in comparison |
|
70 |
if (getDF().equals(df)) |
|
71 |
return getContent(ds); |
|
72 |
else |
|
73 |
return null; |
|
74 |
} |
|
75 |
||
76 |
public Object getContent(DataSource ds) throws IOException { |
|
77 |
String enc = null; |
|
78 |
InputStreamReader is = null; |
|
79 |
||
80 |
try { |
|
81 |
enc = getCharset(ds.getContentType()); |
|
82 |
is = new InputStreamReader(ds.getInputStream(), enc); |
|
83 |
} catch (IllegalArgumentException iex) { |
|
84 |
/* |
|
85 |
* An unknown charset of the form ISO-XXX-XXX will cause |
|
86 |
* the JDK to throw an IllegalArgumentException. The |
|
87 |
* JDK will attempt to create a classname using this string, |
|
88 |
* but valid classnames must not contain the character '-', |
|
89 |
* and this results in an IllegalArgumentException, rather than |
|
90 |
* the expected UnsupportedEncodingException. Yikes. |
|
91 |
*/ |
|
92 |
throw new UnsupportedEncodingException(enc); |
|
93 |
} |
|
94 |
||
95 |
try { |
|
96 |
int pos = 0; |
|
97 |
int count; |
|
98 |
char buf[] = new char[1024]; |
|
99 |
||
100 |
while ((count = is.read(buf, pos, buf.length - pos)) != -1) { |
|
101 |
pos += count; |
|
102 |
if (pos >= buf.length) { |
|
103 |
int size = buf.length; |
|
104 |
if (size < 256*1024) |
|
105 |
size += size; |
|
106 |
else |
|
107 |
size += 256*1024; |
|
108 |
char tbuf[] = new char[size]; |
|
109 |
System.arraycopy(buf, 0, tbuf, 0, pos); |
|
110 |
buf = tbuf; |
|
111 |
} |
|
112 |
} |
|
113 |
return new String(buf, 0, pos); |
|
114 |
} finally { |
|
115 |
try { |
|
116 |
is.close(); |
|
117 |
} catch (IOException ex) { } |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Write the object to the output stream, using the specified MIME type. |
|
123 |
*/ |
|
124 |
public void writeTo(Object obj, String type, OutputStream os) |
|
125 |
throws IOException { |
|
126 |
if (!(obj instanceof String)) |
|
127 |
throw new IOException("\"" + getDF().getMimeType() + |
|
128 |
"\" DataContentHandler requires String object, " + |
|
129 |
"was given object of type " + obj.getClass().toString()); |
|
130 |
||
131 |
String enc = null; |
|
132 |
OutputStreamWriter osw = null; |
|
133 |
||
134 |
try { |
|
135 |
enc = getCharset(type); |
|
136 |
osw = new OutputStreamWriter(os, enc); |
|
137 |
} catch (IllegalArgumentException iex) { |
|
138 |
/* |
|
139 |
* An unknown charset of the form ISO-XXX-XXX will cause |
|
140 |
* the JDK to throw an IllegalArgumentException. The |
|
141 |
* JDK will attempt to create a classname using this string, |
|
142 |
* but valid classnames must not contain the character '-', |
|
143 |
* and this results in an IllegalArgumentException, rather than |
|
144 |
* the expected UnsupportedEncodingException. Yikes. |
|
145 |
*/ |
|
146 |
throw new UnsupportedEncodingException(enc); |
|
147 |
} |
|
148 |
||
149 |
String s = (String)obj; |
|
150 |
osw.write(s, 0, s.length()); |
|
151 |
osw.flush(); |
|
152 |
} |
|
153 |
||
154 |
private String getCharset(String type) { |
|
155 |
try { |
|
156 |
ContentType ct = new ContentType(type); |
|
157 |
String charset = ct.getParameter("charset"); |
|
158 |
if (charset == null) |
|
159 |
// If the charset parameter is absent, use US-ASCII. |
|
160 |
charset = "us-ascii"; |
|
161 |
return MimeUtility.javaCharset(charset); |
|
162 |
} catch (Exception ex) { |
|
163 |
return null; |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
} |