Mon, 23 Apr 2018 13:24:44 +0100
8196491: Newlines in JAXB string values of SOAP-requests are escaped to "
"
Reviewed-by: lancea, rgrigoriadi
1 /*
2 * Copyright (c) 1997, 2018, 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 */
26 package com.sun.xml.internal.ws.streaming;
28 import com.sun.istack.internal.Nullable;
29 import com.sun.xml.internal.ws.api.streaming.XMLStreamWriterFactory;
30 import com.sun.xml.internal.ws.encoding.HasEncoding;
31 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
33 import javax.xml.namespace.QName;
34 import javax.xml.stream.XMLStreamWriter;
35 import javax.xml.stream.XMLStreamException;
36 import java.util.Map;
37 import java.io.OutputStream;
39 /**
40 * <p>XMLStreamWriterUtil provides some utility methods intended to be used
41 * in conjunction with a StAX XMLStreamWriter. </p>
42 *
43 * @author Santiago.PericasGeertsen@sun.com
44 */
45 public class XMLStreamWriterUtil {
47 private XMLStreamWriterUtil() {
48 }
50 /**
51 * Gives the underlying stream for XMLStreamWriter. It closes any start elements, and returns the stream so
52 * that JAXB can write infoset directly to the stream.
53 *
54 * @param writer XMLStreamWriter for which stream is required
55 * @return underlying OutputStream, null if writer doesn't provide a way to get it
56 * @throws XMLStreamException if any of writer operations throw the exception
57 */
58 public static @Nullable OutputStream getOutputStream(XMLStreamWriter writer) throws XMLStreamException {
59 Object obj = null;
61 XMLStreamWriter xmlStreamWriter =
62 writer instanceof XMLStreamWriterFactory.HasEncodingWriter ?
63 ((XMLStreamWriterFactory.HasEncodingWriter) writer).getWriter()
64 : writer;
67 // Hack for JDK6's SJSXP
68 if (xmlStreamWriter instanceof Map) {
69 obj = ((Map) xmlStreamWriter).get("sjsxp-outputstream");
70 }
72 // woodstox
73 if (obj == null) {
74 try {
75 obj = writer.getProperty("com.ctc.wstx.outputUnderlyingStream");
76 } catch(Exception ie) {
77 // Catch all exceptions. SJSXP in JDK throws NPE
78 // nothing to do here
79 }
80 }
82 // SJSXP
83 if (obj == null) {
84 try {
85 obj = writer.getProperty("http://java.sun.com/xml/stream/properties/outputstream");
86 } catch(Exception ie) {
87 // Catch all exceptions. SJSXP in JDK throws NPE
88 // nothing to do here
89 }
90 }
93 if (obj != null) {
94 writer.writeCharacters(""); // Force completion of open elems
95 writer.flush();
96 return (OutputStream)obj;
97 }
98 return null;
99 }
101 /**
102 * Gives the encoding with which XMLStreamWriter is created.
103 *
104 * @param writer XMLStreamWriter for which encoding is required
105 * @return null if cannot be found, else the encoding
106 */
107 public static @Nullable String getEncoding(XMLStreamWriter writer) {
108 /*
109 * TODO Add reflection logic to handle woodstox writer
110 * as it implements XMLStreamWriter2#getEncoding()
111 * It's not that important since woodstox writer is typically wrapped
112 * in a writer with HasEncoding
113 */
114 return (writer instanceof HasEncoding)
115 ? ((HasEncoding)writer).getEncoding()
116 : null;
117 }
119 public static String encodeQName(XMLStreamWriter writer, QName qname,
120 PrefixFactory prefixFactory)
121 {
122 // NOTE: Here it is assumed that we do not serialize using default
123 // namespace declarations and therefore that writer.getPrefix will
124 // never return ""
126 try {
127 String namespaceURI = qname.getNamespaceURI();
128 String localPart = qname.getLocalPart();
130 if (namespaceURI == null || namespaceURI.equals("")) {
131 return localPart;
132 }
133 else {
134 String prefix = writer.getPrefix(namespaceURI);
135 if (prefix == null) {
136 prefix = prefixFactory.getPrefix(namespaceURI);
137 writer.writeNamespace(prefix, namespaceURI);
138 }
139 return prefix + ":" + localPart;
140 }
141 }
142 catch (XMLStreamException e) {
143 throw new RuntimeException(e);
144 }
145 }
146 }