author | avstepan |
Thu, 24 Sep 2015 18:26:42 +0300 | |
changeset 32795 | 5a5710ee05a0 |
parent 25871 | b80b84e87032 |
permissions | -rw-r--r-- |
16791 | 1 |
/* |
23961 | 2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
16791 | 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.ws.commons.xmlutil; |
|
27 |
||
28 |
import com.sun.istack.internal.NotNull; |
|
29 |
import com.sun.istack.internal.logging.Logger; |
|
30 |
import com.sun.xml.internal.ws.api.message.Message; |
|
31 |
import com.sun.xml.internal.ws.api.message.Messages; |
|
32 |
import com.sun.xml.internal.ws.api.message.Packet; |
|
33 |
import com.sun.xml.internal.ws.util.xml.XmlUtil; |
|
34 |
||
35 |
import javax.xml.stream.*; |
|
36 |
import javax.xml.xpath.XPathFactoryConfigurationException; |
|
37 |
import java.io.*; |
|
38 |
import java.lang.reflect.Constructor; |
|
39 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
40 |
import java.util.logging.Level; |
|
41 |
||
42 |
/** |
|
43 |
* Utility class that provides conversion of different XML representations |
|
44 |
* from/to various other formats |
|
45 |
* |
|
46 |
* @author Marek Potociar |
|
47 |
*/ |
|
48 |
public final class Converter { |
|
49 |
||
50 |
public static final String UTF_8 = "UTF-8"; |
|
51 |
||
52 |
private Converter() { |
|
53 |
// prevents instantiation |
|
54 |
} |
|
55 |
private static final Logger LOGGER = Logger.getLogger(Converter.class); |
|
23961 | 56 |
private static final ContextClassloaderLocal<XMLOutputFactory> xmlOutputFactory = new ContextClassloaderLocal<XMLOutputFactory>() { |
57 |
@Override |
|
58 |
protected XMLOutputFactory initialValue() throws Exception { |
|
59 |
return XMLOutputFactory.newInstance(); |
|
60 |
} |
|
61 |
}; |
|
16791 | 62 |
private static final AtomicBoolean logMissingStaxUtilsWarning = new AtomicBoolean(false); |
63 |
||
64 |
/** |
|
65 |
* Converts a throwable to String |
|
66 |
* |
|
67 |
* @param throwable |
|
68 |
* @return String representation of throwable |
|
69 |
*/ |
|
70 |
public static String toString(Throwable throwable) { |
|
71 |
if (throwable == null) { |
|
72 |
return "[ No exception ]"; |
|
73 |
} |
|
74 |
||
75 |
StringWriter stringOut = new StringWriter(); |
|
76 |
throwable.printStackTrace(new PrintWriter(stringOut)); |
|
77 |
||
78 |
return stringOut.toString(); |
|
79 |
} |
|
80 |
||
81 |
public static String toString(Packet packet) { |
|
82 |
if (packet == null) { |
|
83 |
return "[ Null packet ]"; |
|
84 |
} else if (packet.getMessage() == null) { |
|
85 |
return "[ Empty packet ]"; |
|
86 |
} |
|
87 |
||
88 |
return toString(packet.getMessage()); |
|
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
89 |
} |
16791 | 90 |
|
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
91 |
public static String toStringNoIndent(Packet packet) { |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
92 |
if (packet == null) { |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
93 |
return "[ Null packet ]"; |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
94 |
} else if (packet.getMessage() == null) { |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
95 |
return "[ Empty packet ]"; |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
96 |
} |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
97 |
|
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
98 |
return toStringNoIndent(packet.getMessage()); |
16791 | 99 |
} |
100 |
||
101 |
public static String toString(Message message) { |
|
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
102 |
return toString(message, true); |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
103 |
} |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
104 |
|
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
105 |
public static String toStringNoIndent(Message message) { |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
106 |
return toString(message, false); |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
107 |
} |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
108 |
|
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
109 |
private static String toString(Message message, boolean createIndenter) { |
16791 | 110 |
if (message == null) { |
111 |
return "[ Null message ]"; |
|
112 |
} |
|
113 |
StringWriter stringOut = null; |
|
114 |
try { |
|
115 |
stringOut = new StringWriter(); |
|
116 |
XMLStreamWriter writer = null; |
|
117 |
try { |
|
23961 | 118 |
writer = xmlOutputFactory.get().createXMLStreamWriter(stringOut); |
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
119 |
if (createIndenter) { |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
120 |
writer = createIndenter(writer); |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
16791
diff
changeset
|
121 |
} |
16791 | 122 |
message.copy().writeTo(writer); |
123 |
} catch (Exception e) { // WSIT-1596 - Message Dumping should not affect other processing |
|
124 |
LOGGER.log(Level.WARNING, "Unexpected exception occured while dumping message", e); |
|
125 |
} finally { |
|
126 |
if (writer != null) { |
|
127 |
try { |
|
128 |
writer.close(); |
|
129 |
} catch (XMLStreamException ignored) { |
|
130 |
LOGGER.fine("Unexpected exception occured while closing XMLStreamWriter", ignored); |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
return stringOut.toString(); |
|
135 |
} finally { |
|
136 |
if (stringOut != null) { |
|
137 |
try { |
|
138 |
stringOut.close(); |
|
139 |
} catch (IOException ex) { |
|
140 |
LOGGER.finest("An exception occured when trying to close StringWriter", ex); |
|
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
public static byte[] toBytes(Message message, String encoding) throws XMLStreamException { |
|
147 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
148 |
||
149 |
try { |
|
150 |
if (message != null) { |
|
23961 | 151 |
XMLStreamWriter xsw = xmlOutputFactory.get().createXMLStreamWriter(baos, encoding); |
16791 | 152 |
try { |
153 |
message.writeTo(xsw); |
|
154 |
} finally { |
|
155 |
try { |
|
156 |
xsw.close(); |
|
157 |
} catch (XMLStreamException ex) { |
|
158 |
LOGGER.warning("Unexpected exception occured while closing XMLStreamWriter", ex); |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
return baos.toByteArray(); |
|
164 |
} finally { |
|
165 |
try { |
|
166 |
baos.close(); |
|
167 |
} catch (IOException ex) { |
|
168 |
LOGGER.warning("Unexpected exception occured while closing ByteArrayOutputStream", ex); |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Converts JAX-WS RI message represented as input stream back to Message |
|
175 |
* object. |
|
176 |
* |
|
177 |
* @param dataStream message data stream |
|
178 |
* @param encoding message data stream encoding |
|
179 |
* |
|
180 |
* @return {@link com.sun.xml.internal.ws.api.message.Message} object created from the data stream |
|
181 |
*/ |
|
182 |
public static Message toMessage(@NotNull InputStream dataStream, String encoding) throws XMLStreamException { |
|
183 |
XMLStreamReader xsr = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(dataStream, encoding); |
|
184 |
return Messages.create(xsr); |
|
185 |
} |
|
186 |
||
187 |
public static String messageDataToString(final byte[] data, final String encoding) { |
|
188 |
try { |
|
189 |
return toString(toMessage(new ByteArrayInputStream(data), encoding)); |
|
190 |
// closing ByteArrayInputStream has no effect, so ignoring the redundant call |
|
191 |
} catch (XMLStreamException ex) { |
|
192 |
LOGGER.warning("Unexpected exception occured while converting message data to string", ex); |
|
193 |
return "[ Message Data Conversion Failed ]"; |
|
194 |
} |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Wraps {@link javax.xml.stream.XMLStreamWriter} by an indentation engine if possible. |
|
199 |
* |
|
200 |
* <p> |
|
32795
5a5710ee05a0
8133651: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
25871
diff
changeset
|
201 |
* We can do this only when we have {@code stax-utils.jar} in the class path. |
16791 | 202 |
*/ |
203 |
private static XMLStreamWriter createIndenter(XMLStreamWriter writer) { |
|
204 |
try { |
|
205 |
Class<?> clazz = Converter.class.getClassLoader().loadClass("javanet.staxutils.IndentingXMLStreamWriter"); |
|
206 |
Constructor<?> c = clazz.getConstructor(XMLStreamWriter.class); |
|
207 |
writer = XMLStreamWriter.class.cast(c.newInstance(writer)); |
|
208 |
} catch (Exception ex) { |
|
209 |
// if stax-utils.jar is not in the classpath, this will fail |
|
210 |
// so, we'll just have to do without indentation |
|
211 |
if (logMissingStaxUtilsWarning.compareAndSet(false, true)) { |
|
212 |
LOGGER.log(Level.WARNING, "Put stax-utils.jar to the classpath to indent the dump output", ex); |
|
213 |
} |
|
214 |
} |
|
215 |
return writer; |
|
216 |
} |
|
217 |
} |