Mon, 23 Apr 2018 13:24:44 +0100
8196491: Newlines in JAXB string values of SOAP-requests are escaped to "
"
Reviewed-by: lancea, rgrigoriadi
ohair@286 | 1 | /* |
aefimov@1609 | 2 | * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.bind.v2.runtime.output; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.io.IOException; |
aefimov@1443 | 29 | import java.io.Writer; |
ohair@286 | 30 | import java.lang.reflect.Constructor; |
ohair@286 | 31 | |
ohair@286 | 32 | import javax.xml.stream.XMLStreamException; |
ohair@286 | 33 | import javax.xml.stream.XMLStreamWriter; |
ohair@286 | 34 | |
aefimov@1443 | 35 | import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; |
aefimov@1609 | 36 | import com.sun.xml.internal.bind.marshaller.NoEscapeHandler; |
ohair@286 | 37 | import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; |
ohair@286 | 38 | import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; |
ohair@286 | 39 | |
ohair@286 | 40 | import org.xml.sax.SAXException; |
ohair@286 | 41 | |
ohair@286 | 42 | /** |
ohair@286 | 43 | * {@link XmlOutput} that writes to StAX {@link XMLStreamWriter}. |
ohair@286 | 44 | * <p> |
ohair@286 | 45 | * TODO: |
ohair@286 | 46 | * Finding the optimized FI implementations is a bit hacky and not very |
ohair@286 | 47 | * extensible. Can we use the service provider mechanism in general for |
ohair@286 | 48 | * concrete implementations of XmlOutputAbstractImpl. |
ohair@286 | 49 | * |
ohair@286 | 50 | * @author Kohsuke Kawaguchi |
ohair@286 | 51 | */ |
ohair@286 | 52 | public class XMLStreamWriterOutput extends XmlOutputAbstractImpl { |
ohair@286 | 53 | |
ohair@286 | 54 | /** |
ohair@286 | 55 | * Creates a new {@link XmlOutput} from a {@link XMLStreamWriter}. |
ohair@286 | 56 | * This method recognizes an FI StAX writer. |
ohair@286 | 57 | */ |
aefimov@1443 | 58 | public static XmlOutput create(XMLStreamWriter out, JAXBContextImpl context, CharacterEscapeHandler escapeHandler) { |
ohair@286 | 59 | // try optimized path |
ohair@286 | 60 | final Class writerClass = out.getClass(); |
ohair@286 | 61 | if (writerClass==FI_STAX_WRITER_CLASS) { |
ohair@286 | 62 | try { |
ohair@286 | 63 | return FI_OUTPUT_CTOR.newInstance(out, context); |
ohair@286 | 64 | } catch (Exception e) { |
ohair@286 | 65 | } |
ohair@286 | 66 | } |
ohair@286 | 67 | if (STAXEX_WRITER_CLASS!=null && STAXEX_WRITER_CLASS.isAssignableFrom(writerClass)) { |
ohair@286 | 68 | try { |
ohair@286 | 69 | return STAXEX_OUTPUT_CTOR.newInstance(out); |
ohair@286 | 70 | } catch (Exception e) { |
ohair@286 | 71 | } |
ohair@286 | 72 | } |
ohair@286 | 73 | |
aefimov@1443 | 74 | CharacterEscapeHandler xmlStreamEscapeHandler = escapeHandler != null ? |
aefimov@1609 | 75 | escapeHandler : NoEscapeHandler.theInstance; |
aefimov@1443 | 76 | |
ohair@286 | 77 | // otherwise the normal writer. |
aefimov@1443 | 78 | return new XMLStreamWriterOutput(out, xmlStreamEscapeHandler); |
ohair@286 | 79 | } |
ohair@286 | 80 | |
ohair@286 | 81 | |
ohair@286 | 82 | private final XMLStreamWriter out; |
ohair@286 | 83 | |
aefimov@1443 | 84 | private final CharacterEscapeHandler escapeHandler; |
aefimov@1443 | 85 | |
aefimov@1443 | 86 | private final XmlStreamOutWriterAdapter writerWrapper; |
aefimov@1443 | 87 | |
ohair@286 | 88 | protected final char[] buf = new char[256]; |
ohair@286 | 89 | |
aefimov@1443 | 90 | protected XMLStreamWriterOutput(XMLStreamWriter out, CharacterEscapeHandler escapeHandler) { |
ohair@286 | 91 | this.out = out; |
aefimov@1443 | 92 | this.escapeHandler = escapeHandler; |
aefimov@1443 | 93 | this.writerWrapper = new XmlStreamOutWriterAdapter(out); |
ohair@286 | 94 | } |
ohair@286 | 95 | |
ohair@286 | 96 | // not called if we are generating fragments |
ohair@286 | 97 | @Override |
ohair@286 | 98 | public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 99 | super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext); |
ohair@286 | 100 | if(!fragment) |
ohair@286 | 101 | out.writeStartDocument(); |
ohair@286 | 102 | } |
ohair@286 | 103 | |
ohair@286 | 104 | @Override |
ohair@286 | 105 | public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 106 | if(!fragment) { |
ohair@286 | 107 | out.writeEndDocument(); |
ohair@286 | 108 | out.flush(); |
ohair@286 | 109 | } |
ohair@286 | 110 | super.endDocument(fragment); |
ohair@286 | 111 | } |
ohair@286 | 112 | |
ohair@286 | 113 | public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException { |
ohair@286 | 114 | out.writeStartElement( |
ohair@286 | 115 | nsContext.getPrefix(prefix), |
ohair@286 | 116 | localName, |
ohair@286 | 117 | nsContext.getNamespaceURI(prefix)); |
ohair@286 | 118 | |
ohair@286 | 119 | NamespaceContextImpl.Element nse = nsContext.getCurrent(); |
ohair@286 | 120 | if(nse.count()>0) { |
ohair@286 | 121 | for( int i=nse.count()-1; i>=0; i-- ) { |
ohair@286 | 122 | String uri = nse.getNsUri(i); |
ohair@286 | 123 | if(uri.length()==0 && nse.getBase()==1) |
ohair@286 | 124 | continue; // no point in definint xmlns='' on the root |
ohair@286 | 125 | out.writeNamespace(nse.getPrefix(i),uri); |
ohair@286 | 126 | } |
ohair@286 | 127 | } |
ohair@286 | 128 | } |
ohair@286 | 129 | |
ohair@286 | 130 | public void attribute(int prefix, String localName, String value) throws IOException, XMLStreamException { |
ohair@286 | 131 | if(prefix==-1) |
ohair@286 | 132 | out.writeAttribute(localName,value); |
ohair@286 | 133 | else |
ohair@286 | 134 | out.writeAttribute( |
ohair@286 | 135 | nsContext.getPrefix(prefix), |
ohair@286 | 136 | nsContext.getNamespaceURI(prefix), |
ohair@286 | 137 | localName, value); |
ohair@286 | 138 | } |
ohair@286 | 139 | |
ohair@286 | 140 | public void endStartTag() throws IOException, SAXException { |
ohair@286 | 141 | // noop |
ohair@286 | 142 | } |
ohair@286 | 143 | |
ohair@286 | 144 | public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 145 | out.writeEndElement(); |
ohair@286 | 146 | } |
ohair@286 | 147 | |
ohair@286 | 148 | public void text(String value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 149 | if(needsSeparatingWhitespace) |
ohair@286 | 150 | out.writeCharacters(" "); |
aefimov@1443 | 151 | escapeHandler.escape(value.toCharArray(), 0, value.length(), false, writerWrapper); |
ohair@286 | 152 | } |
ohair@286 | 153 | |
ohair@286 | 154 | public void text(Pcdata value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 155 | if(needsSeparatingWhitespace) |
ohair@286 | 156 | out.writeCharacters(" "); |
ohair@286 | 157 | |
ohair@286 | 158 | int len = value.length(); |
ohair@286 | 159 | if(len <buf.length) { |
ohair@286 | 160 | value.writeTo(buf,0); |
ohair@286 | 161 | out.writeCharacters(buf,0,len); |
ohair@286 | 162 | } else { |
ohair@286 | 163 | out.writeCharacters(value.toString()); |
ohair@286 | 164 | } |
ohair@286 | 165 | } |
ohair@286 | 166 | |
ohair@286 | 167 | /** |
ohair@286 | 168 | * Reference to FI's XMLStreamWriter class, if FI can be loaded. |
ohair@286 | 169 | */ |
ohair@286 | 170 | private static final Class FI_STAX_WRITER_CLASS = initFIStAXWriterClass(); |
ohair@286 | 171 | private static final Constructor<? extends XmlOutput> FI_OUTPUT_CTOR = initFastInfosetOutputClass(); |
ohair@286 | 172 | |
ohair@286 | 173 | private static Class initFIStAXWriterClass() { |
ohair@286 | 174 | try { |
mkos@514 | 175 | Class<?> llfisw = Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.stax.LowLevelFastInfosetStreamWriter"); |
mkos@514 | 176 | Class<?> sds = Class.forName("com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer"); |
ohair@286 | 177 | // Check if StAXDocumentSerializer implements LowLevelFastInfosetStreamWriter |
ohair@286 | 178 | if (llfisw.isAssignableFrom(sds)) |
ohair@286 | 179 | return sds; |
ohair@286 | 180 | else |
ohair@286 | 181 | return null; |
ohair@286 | 182 | } catch (Throwable e) { |
ohair@286 | 183 | return null; |
ohair@286 | 184 | } |
ohair@286 | 185 | } |
ohair@286 | 186 | |
ohair@286 | 187 | private static Constructor<? extends XmlOutput> initFastInfosetOutputClass() { |
ohair@286 | 188 | try { |
ohair@286 | 189 | if (FI_STAX_WRITER_CLASS == null) |
ohair@286 | 190 | return null; |
mkos@514 | 191 | Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.FastInfosetStreamWriterOutput"); |
ohair@286 | 192 | return c.getConstructor(FI_STAX_WRITER_CLASS, JAXBContextImpl.class); |
ohair@286 | 193 | } catch (Throwable e) { |
ohair@286 | 194 | return null; |
ohair@286 | 195 | } |
ohair@286 | 196 | } |
ohair@286 | 197 | |
ohair@286 | 198 | // |
ohair@286 | 199 | // StAX-ex |
ohair@286 | 200 | // |
ohair@286 | 201 | private static final Class STAXEX_WRITER_CLASS = initStAXExWriterClass(); |
ohair@286 | 202 | private static final Constructor<? extends XmlOutput> STAXEX_OUTPUT_CTOR = initStAXExOutputClass(); |
ohair@286 | 203 | |
ohair@286 | 204 | private static Class initStAXExWriterClass() { |
ohair@286 | 205 | try { |
mkos@514 | 206 | return Class.forName("com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx"); |
ohair@286 | 207 | } catch (Throwable e) { |
ohair@286 | 208 | return null; |
ohair@286 | 209 | } |
ohair@286 | 210 | } |
ohair@286 | 211 | |
ohair@286 | 212 | private static Constructor<? extends XmlOutput> initStAXExOutputClass() { |
ohair@286 | 213 | try { |
mkos@514 | 214 | Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.StAXExStreamWriterOutput"); |
ohair@286 | 215 | return c.getConstructor(STAXEX_WRITER_CLASS); |
ohair@286 | 216 | } catch (Throwable e) { |
ohair@286 | 217 | return null; |
ohair@286 | 218 | } |
ohair@286 | 219 | } |
ohair@286 | 220 | |
aefimov@1443 | 221 | private static final class XmlStreamOutWriterAdapter extends Writer { |
aefimov@1443 | 222 | |
aefimov@1443 | 223 | private final XMLStreamWriter writer; |
aefimov@1443 | 224 | |
aefimov@1443 | 225 | private XmlStreamOutWriterAdapter(XMLStreamWriter writer) { |
aefimov@1443 | 226 | this.writer = writer; |
aefimov@1443 | 227 | } |
aefimov@1443 | 228 | |
aefimov@1443 | 229 | @Override |
aefimov@1443 | 230 | public void write(char[] cbuf, int off, int len) throws IOException { |
aefimov@1443 | 231 | try { |
aefimov@1443 | 232 | writer.writeCharacters(cbuf, off, len); |
aefimov@1443 | 233 | } catch (XMLStreamException e) { |
aefimov@1443 | 234 | throw new IOException("Error writing XML stream", e); |
aefimov@1443 | 235 | } |
aefimov@1443 | 236 | } |
aefimov@1443 | 237 | |
aefimov@1443 | 238 | public void writeEntityRef(String entityReference) throws XMLStreamException { |
aefimov@1443 | 239 | writer.writeEntityRef(entityReference); |
aefimov@1443 | 240 | } |
aefimov@1443 | 241 | |
aefimov@1443 | 242 | @Override |
aefimov@1443 | 243 | public void flush() throws IOException { |
aefimov@1443 | 244 | try { |
aefimov@1443 | 245 | writer.flush(); |
aefimov@1443 | 246 | } catch (XMLStreamException e) { |
aefimov@1443 | 247 | throw new IOException("Error flushing XML stream", e); |
aefimov@1443 | 248 | } |
aefimov@1443 | 249 | } |
aefimov@1443 | 250 | |
aefimov@1443 | 251 | @Override |
aefimov@1443 | 252 | public void close() throws IOException { |
aefimov@1443 | 253 | try { |
aefimov@1443 | 254 | writer.close(); |
aefimov@1443 | 255 | } catch (XMLStreamException e) { |
aefimov@1443 | 256 | throw new IOException("Error closing XML stream", e); |
aefimov@1443 | 257 | } |
aefimov@1443 | 258 | } |
aefimov@1443 | 259 | } |
ohair@286 | 260 | } |