changeset 24212:d9e83e39c11b

8040754: Break the circular dependency between SAAJ and JAXB Reviewed-by: chegar, mchung
author mkos
date Mon, 28 Apr 2014 14:05:34 -0700
parents 0809c9a4d36e
children 899a85110630
files jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/StaxBridge.java jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/stax/SaajStaxReaderEx.java jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/stax/SaajStaxWriter.java jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/stax/SaajStaxWriterEx.java jaxws/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/BinaryText.java jaxws/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/StAxSOAPBody.java jaxws/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SAAJFactory.java jaxws/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SaajStaxWriter.java
diffstat 8 files changed, 788 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/StaxBridge.java	Wed Jul 05 19:38:35 2017 +0200
+++ b/jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/StaxBridge.java	Mon Apr 28 14:05:34 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,11 +25,12 @@
 
 package com.sun.xml.internal.messaging.saaj.soap;
 
+import com.sun.xml.internal.messaging.saaj.util.stax.SaajStaxWriter;
+
 import javax.xml.namespace.QName;
 import javax.xml.soap.SOAPException;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
-import com.sun.xml.internal.org.jvnet.staxex.util.SaajStaxWriter;
 import com.sun.xml.internal.org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter;
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/stax/SaajStaxReaderEx.java	Mon Apr 28 14:05:34 2014 -0700
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.xml.internal.messaging.saaj.util.stax;
+
+import com.sun.xml.internal.org.jvnet.staxex.Base64Data;
+import com.sun.xml.internal.org.jvnet.staxex.BinaryText;
+import com.sun.xml.internal.org.jvnet.staxex.XMLStreamReaderEx;
+import com.sun.xml.internal.org.jvnet.staxex.util.DOMStreamReader;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+
+import javax.xml.soap.SOAPElement;
+import javax.xml.stream.XMLStreamException;
+import java.util.Iterator;
+
+/**
+ * SaajStaxReaderEx
+ *
+ * @author shih-chang.chen@oracle.com
+ */
+public class SaajStaxReaderEx extends DOMStreamReader implements XMLStreamReaderEx {
+    //TODO extends com.sun.xml.internal.ws.streaming.DOMStreamReader
+    private BinaryText binaryText = null;
+    private Base64Data base64AttData = null;
+
+    public SaajStaxReaderEx(SOAPElement se) {
+        super(se);
+    }
+
+    @Override
+    public int next() throws XMLStreamException {
+        binaryText = null;
+        base64AttData = null;
+        while(true) {
+            int r = _next();
+            switch (r) {
+            case CHARACTERS:
+                if (_current instanceof BinaryText) {
+                    binaryText = (BinaryText) _current;
+                    base64AttData = new Base64Data();
+                    base64AttData.set(binaryText.getDataHandler());
+//System.out.println("--------------- debug SaajStaxReaderEx binaryText " + binaryText);
+                } else {
+                    // if we are currently at text node, make sure that this is a meaningful text node.
+                    Node prev = _current.getPreviousSibling();
+                    if(prev!=null && prev.getNodeType()==Node.TEXT_NODE)
+                        continue;   // nope. this is just a continuation of previous text that should be invisible
+
+                    Text t = (Text)_current;
+                    wholeText = t.getWholeText();
+                    if(wholeText.length()==0)
+                        continue;   // nope. this is empty text.
+                }
+                return CHARACTERS;
+            case START_ELEMENT:
+                splitAttributes();
+                return START_ELEMENT;
+            default:
+                return r;
+            }
+        }
+    }
+
+    @Override
+    public String getElementTextTrim() throws XMLStreamException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public CharSequence getPCDATA() throws XMLStreamException {
+        return (binaryText != null) ? base64AttData : getText();
+    }
+
+    @Override
+    public com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx getNamespaceContext() {
+        return new com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx() {
+
+            @Override
+            public String getNamespaceURI(String prefix) {
+                return _current.lookupNamespaceURI(prefix);
+            }
+
+            @Override
+            public String getPrefix(String uri) {
+                return _current.lookupPrefix(uri);
+            }
+
+            @Override
+            public Iterator getPrefixes(String arg0) {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            @Override
+            public Iterator<Binding> iterator() {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+        };
+    }
+
+
+    @Override
+    public int getTextLength() {
+        return (binaryText != null) ? base64AttData.length() : super.getTextLength();
+    }
+
+    @Override
+    public int getTextStart() {
+        return (binaryText != null) ? 0: super.getTextStart();
+    }
+
+    @Override
+    public char[] getTextCharacters() {
+        if (binaryText != null) {
+            char[] chars = new char[base64AttData.length()];
+            base64AttData.writeTo(chars, 0);
+            return chars;
+        }
+        return super.getTextCharacters();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/stax/SaajStaxWriter.java	Mon Apr 28 14:05:34 2014 -0700
@@ -0,0 +1,352 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.xml.internal.messaging.saaj.util.stax;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.soap.SOAPElement;
+import javax.xml.soap.SOAPException;
+import javax.xml.soap.SOAPMessage;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.w3c.dom.Comment;
+import org.w3c.dom.Node;
+
+/**
+ * SaajStaxWriter builds a SAAJ SOAPMessage by using XMLStreamWriter interface.
+ *
+ * @author shih-chang.chen@oracle.com
+ */
+public class SaajStaxWriter implements XMLStreamWriter {
+
+    protected SOAPMessage soap;
+    protected String envURI;
+    protected SOAPElement currentElement;
+
+    static final protected String Envelope = "Envelope";
+    static final protected String Header = "Header";
+    static final protected String Body = "Body";
+    static final protected String xmlns = "xmlns";
+
+    private boolean isHeaderSeen = false;
+
+    public SaajStaxWriter(final SOAPMessage msg, String uri) throws SOAPException {
+        soap = msg;
+        this.envURI = uri;
+    }
+
+    public SOAPMessage getSOAPMessage() {
+        return soap;
+    }
+
+    protected SOAPElement getEnvelope() throws SOAPException {
+        return soap.getSOAPPart().getEnvelope();
+    }
+
+    @Override
+    public void writeStartElement(final String localName) throws XMLStreamException {
+        try {
+            currentElement = currentElement.addChildElement(localName);
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    @Override
+    public void writeStartElement(final String ns, final String ln) throws XMLStreamException {
+        writeStartElement(null, ln, ns);
+    }
+
+    @Override
+    public void writeStartElement(final String prefix, final String ln, final String ns) throws XMLStreamException {
+        try {
+            if (envURI.equals(ns)) {
+                if (Envelope.equals(ln)) {
+                    currentElement = getEnvelope();
+                    fixPrefix(prefix);
+                    return;
+                } else if (Header.equals(ln)) {
+                    isHeaderSeen = true;
+                    currentElement = soap.getSOAPHeader();
+                    fixPrefix(prefix);
+                    return;
+                } else if (Body.equals(ln)) {
+                    currentElement = soap.getSOAPBody();
+                    fixPrefix(prefix);
+                    return;
+                }
+            }
+            currentElement = (prefix == null) ?
+                    currentElement.addChildElement(new QName(ns, ln)) :
+                    currentElement.addChildElement(ln, prefix, ns);
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    private void fixPrefix(final String prfx) throws XMLStreamException {
+        fixPrefix(prfx, currentElement);
+    }
+
+    private void fixPrefix(final String prfx, SOAPElement element) throws XMLStreamException {
+        String oldPrfx = element.getPrefix();
+        if (prfx != null && !prfx.equals(oldPrfx)) {
+            element.setPrefix(prfx);
+        }
+    }
+
+    @Override
+    public void writeEmptyElement(final String uri, final String ln) throws XMLStreamException {
+        writeStartElement(null, ln, uri);
+    }
+
+    @Override
+    public void writeEmptyElement(final String prefix, final String ln, final String uri) throws XMLStreamException {
+        writeStartElement(prefix, ln, uri);
+    }
+
+    @Override
+    public void writeEmptyElement(final String ln) throws XMLStreamException {
+        writeStartElement(null, ln, null);
+    }
+
+    @Override
+    public void writeEndElement() throws XMLStreamException {
+        if (currentElement != null) currentElement = currentElement.getParentElement();
+    }
+
+    @Override
+    public void writeEndDocument() throws XMLStreamException {
+        try {
+            if (!isHeaderSeen) {
+                SOAPElement header = soap.getSOAPHeader();
+                if (header != null) {
+                    String prefixAtHeader = header.getPrefix();
+                    SOAPElement env = getEnvelope();
+                    header.detachNode();
+                    if (prefixAtHeader != null && !prefixAtHeader.equals(env.getPrefix())) {
+                        env.removeNamespaceDeclaration(prefixAtHeader);
+                    }
+                }
+            }
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    @Override
+    public void close() throws XMLStreamException {
+    }
+
+    @Override
+    public void flush() throws XMLStreamException {
+    }
+
+    @Override
+    public void writeAttribute(final String ln, final String val) throws XMLStreamException {
+        writeAttribute(null, null, ln, val);
+    }
+
+    @Override
+    public void writeAttribute(final String prefix, final String ns, final String ln, final String value) throws XMLStreamException {
+        try {
+            if (ns == null) {
+                if (prefix == null && xmlns.equals(ln)) {
+                    currentElement.addNamespaceDeclaration("", value);
+                } else {
+                    currentElement.setAttributeNS("", ln, value);
+                }
+            } else {
+                QName name = (prefix == null) ? new QName(ns, ln) : new QName(ns, ln, prefix);
+                currentElement.addAttribute(name, value);
+            }
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    @Override
+    public void writeAttribute(final String ns, final String ln, final String val) throws XMLStreamException {
+        writeAttribute(null, ns, ln, val);
+    }
+
+    @Override
+    public void writeNamespace(String prefix, final String uri) throws XMLStreamException {
+
+        // make prefix default if null or "xmlns" (according to javadoc)
+        if (prefix == null || "xmlns".equals(prefix)) {
+            prefix = "";
+        }
+
+        try {
+            currentElement.addNamespaceDeclaration(prefix, uri);
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    @Override
+    public void writeDefaultNamespace(final String uri) throws XMLStreamException {
+        writeNamespace("", uri);
+    }
+
+    @Override
+    public void writeComment(final String data) throws XMLStreamException {
+        Comment c = soap.getSOAPPart().createComment(data);
+        currentElement.appendChild(c);
+    }
+
+    @Override
+    public void writeProcessingInstruction(final String target) throws XMLStreamException {
+        Node n = soap.getSOAPPart().createProcessingInstruction(target, "");
+        currentElement.appendChild(n);
+    }
+
+    @Override
+    public void writeProcessingInstruction(final String target, final String data) throws XMLStreamException {
+        Node n = soap.getSOAPPart().createProcessingInstruction(target, data);
+        currentElement.appendChild(n);
+    }
+
+    @Override
+    public void writeCData(final String data) throws XMLStreamException {
+        Node n = soap.getSOAPPart().createCDATASection(data);
+        currentElement.appendChild(n);
+    }
+
+    @Override
+    public void writeDTD(final String dtd) throws XMLStreamException {
+        //TODO ... Don't do anything here
+    }
+
+    @Override
+    public void writeEntityRef(final String name) throws XMLStreamException {
+        Node n = soap.getSOAPPart().createEntityReference(name);
+        currentElement.appendChild(n);
+    }
+
+    @Override
+    public void writeStartDocument() throws XMLStreamException {
+    }
+
+    @Override
+    public void writeStartDocument(final String version) throws XMLStreamException {
+        if (version != null) soap.getSOAPPart().setXmlVersion(version);
+    }
+
+    @Override
+    public void writeStartDocument(final String encoding, final String version) throws XMLStreamException {
+        if (version != null) soap.getSOAPPart().setXmlVersion(version);
+        if (encoding != null) {
+            try {
+                soap.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, encoding);
+            } catch (SOAPException e) {
+                throw new XMLStreamException(e);
+            }
+        }
+    }
+
+    @Override
+    public void writeCharacters(final String text) throws XMLStreamException {
+        try {
+            currentElement.addTextNode(text);
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    @Override
+    public void writeCharacters(final char[] text, final int start, final int len) throws XMLStreamException {
+        char[] chr = (start == 0 && len == text.length) ? text : Arrays.copyOfRange(text, start, start + len);
+        try {
+            currentElement.addTextNode(new String(chr));
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    @Override
+    public String getPrefix(final String uri) throws XMLStreamException {
+        return currentElement.lookupPrefix(uri);
+    }
+
+    @Override
+    public void setPrefix(final String prefix, final String uri) throws XMLStreamException {
+        try {
+            this.currentElement.addNamespaceDeclaration(prefix, uri);
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    @Override
+    public void setDefaultNamespace(final String uri) throws XMLStreamException {
+        setPrefix("", uri);
+    }
+
+    @Override
+    public void setNamespaceContext(final NamespaceContext context)throws XMLStreamException {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Object getProperty(final String name) throws IllegalArgumentException {
+        //TODO the following line is to make eclipselink happy ... they are aware of this problem -
+        if (javax.xml.stream.XMLOutputFactory.IS_REPAIRING_NAMESPACES.equals(name)) return Boolean.FALSE;
+        return null;
+    }
+
+    @Override
+    public NamespaceContext getNamespaceContext() {
+        return new NamespaceContext() {
+            public String getNamespaceURI(final String prefix) {
+                return currentElement.getNamespaceURI(prefix);
+            }
+            public String getPrefix(final String namespaceURI) {
+                return currentElement.lookupPrefix(namespaceURI);
+            }
+            public Iterator getPrefixes(final String namespaceURI) {
+                return new Iterator() {
+                    String prefix = getPrefix(namespaceURI);
+                    public boolean hasNext() {
+                        return (prefix != null);
+                    }
+                    public Object next() {
+                        if (!hasNext()) throw new java.util.NoSuchElementException();
+                        String next = prefix;
+                        prefix = null;
+                        return next;
+                    }
+                    public void remove() {}
+                };
+            }
+        };
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/stax/SaajStaxWriterEx.java	Mon Apr 28 14:05:34 2014 -0700
@@ -0,0 +1,245 @@
+/*
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.xml.internal.messaging.saaj.util.stax;
+
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.UUID;
+
+import javax.activation.DataHandler;
+import javax.xml.bind.attachment.AttachmentMarshaller;
+import javax.xml.soap.SOAPException;
+import javax.xml.soap.SOAPMessage;
+import javax.xml.stream.XMLStreamException;
+
+import com.sun.xml.internal.org.jvnet.staxex.Base64Data;
+import com.sun.xml.internal.org.jvnet.staxex.BinaryText;
+import com.sun.xml.internal.org.jvnet.staxex.MtomEnabled;
+import com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx;
+import com.sun.xml.internal.org.jvnet.staxex.StreamingDataHandler;
+import com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx;
+import com.sun.xml.internal.org.jvnet.staxex.util.MtomStreamWriter;
+//
+//import com.sun.xml.internal.ws.api.message.saaj.SaajStaxWriter;
+//import com.sun.xml.internal.ws.developer.StreamingDataHandler;
+//import com.sun.xml.internal.ws.streaming.MtomStreamWriter;
+
+/**
+ * SaajStaxWriterEx converts XMLStreamWriterEx calls to build an orasaaj SOAPMessage with BinaryTextImpl.
+ *
+ * @author shih-chang.chen@oracle.com
+ */
+public class SaajStaxWriterEx extends SaajStaxWriter implements XMLStreamWriterEx, MtomStreamWriter {
+
+    static final protected String xopNS = "http://www.w3.org/2004/08/xop/include";
+    static final protected String Include = "Include";
+    static final protected String href = "href";
+
+    private enum State {xopInclude, others};
+    private State state = State.others;
+    private BinaryText binaryText;
+
+    public SaajStaxWriterEx(SOAPMessage msg, String uri) throws SOAPException {
+        super(msg, uri);
+    }
+
+    public void writeStartElement(String prefix, String ln, String ns) throws XMLStreamException {
+        if (xopNS.equals(ns) && Include.equals(ln)) {
+            state = State.xopInclude;
+            return;
+        } else {
+            super.writeStartElement(prefix, ln, ns);
+        }
+    }
+
+    @Override
+    public void writeEndElement() throws XMLStreamException {
+        if (state.equals(State.xopInclude)) {
+            state = State.others;
+        } else {
+            super.writeEndElement();
+        }
+    }
+
+    @Override
+    public void writeAttribute(String prefix, String ns, String ln, String value) throws XMLStreamException {
+        if (binaryText != null && href.equals(ln)) {
+            return;
+        } else {
+            super.writeAttribute(prefix, ns, ln, value);
+        }
+    }
+
+//    @Override
+//    public void writeComment(String data) throws XMLStreamException {
+//        ((ElementImpl)currentElement).addCommentNode(data);
+//    }
+//
+//    @Override
+//    public void writeCData(String data) throws XMLStreamException {
+//      CDataTextImpl cdt = new CDataTextImpl(soap.getSOAPPart(), data);
+//        currentElement.appendChild(cdt);
+//    }
+
+    @Override
+    public NamespaceContextEx getNamespaceContext() {
+        return new NamespaceContextEx() {
+            public String getNamespaceURI(String prefix) {
+                return currentElement.getNamespaceURI(prefix);
+            }
+            public String getPrefix(String namespaceURI) {
+                return currentElement.lookupPrefix(namespaceURI);
+            }
+            public Iterator getPrefixes(final String namespaceURI) {
+                return new Iterator() {
+                    String prefix = getPrefix(namespaceURI);
+                    public boolean hasNext() {
+                        return (prefix != null);
+                    }
+                    public Object next() {
+                        if (prefix == null) throw new java.util.NoSuchElementException();
+                        String next = prefix;
+                        prefix = null;
+                        return next;
+                    }
+                    public void remove() {}
+                };
+            }
+            public Iterator<Binding> iterator() {
+                return new Iterator<Binding>() {
+                    public boolean hasNext() { return false; }
+                    public Binding next() { return null; }
+                    public void remove() {}
+                };
+            }
+        };
+    }
+
+    @Override
+    public void writeBinary(DataHandler data) throws XMLStreamException {
+//      binaryText = BinaryTextImpl.createBinaryTextFromDataHandler((MessageImpl)soap, null, currentElement.getOwnerDocument(), data);
+//      currentElement.appendChild(binaryText);
+        addBinaryText(data);
+    }
+
+    @Override
+    public OutputStream writeBinary(String arg0) throws XMLStreamException {
+        return null;
+    }
+
+    @Override
+    public void writeBinary(byte[] data, int offset, int length, String contentType) throws XMLStreamException {
+//        if (mtomThreshold == -1 || mtomThreshold > length) return null;
+        byte[] bytes = (offset == 0 && length == data.length) ? data : Arrays.copyOfRange(data, offset, offset + length);
+        if (currentElement instanceof MtomEnabled) {
+            binaryText = ((MtomEnabled) currentElement).addBinaryText(bytes);
+        } else {
+            throw new IllegalStateException("The currentElement is not MtomEnabled " + currentElement);
+        }
+    }
+
+    @Override
+    public void writePCDATA(CharSequence arg0) throws XMLStreamException {
+        if (arg0 instanceof Base64Data) {
+            // The fix of StreamReaderBufferCreator preserves this dataHandler
+            addBinaryText(((Base64Data) arg0).getDataHandler());
+        } else {
+            // We should not normally get here as we expect a DataHandler,
+            // but this is the most general solution.  If we do get
+            // something other than a Data Handler, create a Text node with
+            // the data.  Another alternative would be to throw an exception,
+            // but in the most general case, we don't know whether this input
+            // is expected.
+            try {
+                currentElement.addTextNode(arg0.toString());
+            } catch (SOAPException e) {
+                throw new XMLStreamException("Cannot add Text node", e);
+            }
+        }
+    }
+
+    static private String encodeCid() {
+        String cid = "example.jaxws.sun.com";
+        String name = UUID.randomUUID() + "@";
+        return name + cid;
+    }
+
+    private String addBinaryText(DataHandler data) {
+        String hrefOrCid = null;
+        if (data instanceof StreamingDataHandler) {
+            hrefOrCid = ((StreamingDataHandler) data).getHrefCid();
+        }
+        if (hrefOrCid == null) hrefOrCid = encodeCid();
+
+        String prefixedCid = (hrefOrCid.startsWith("cid:")) ? hrefOrCid : "cid:" + hrefOrCid;
+        // Should we do the threshold processing on DataHandler ? But that would be
+        // expensive as DataHolder need to read the data again from its source
+      //binaryText = BinaryTextImpl.createBinaryTextFromDataHandler((MessageImpl) soap, prefixedCid, currentElement.getOwnerDocument(), data);
+      //currentElement.appendChild(binaryText);
+        if (currentElement instanceof MtomEnabled) {
+            binaryText = ((MtomEnabled) currentElement).addBinaryText(prefixedCid, data);
+        } else {
+            throw new IllegalStateException("The currentElement is not MtomEnabled " + currentElement);
+        }
+        return hrefOrCid;
+    }
+
+    public AttachmentMarshaller getAttachmentMarshaller() {
+        return new AttachmentMarshaller() {
+            @Override
+            public String addMtomAttachment(DataHandler data, String ns, String ln) {
+//                if (mtomThreshold == -1) return null;
+                String hrefOrCid = addBinaryText(data);
+//                return binaryText.getHref();
+                return hrefOrCid;
+            }
+
+            @Override
+            public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String ns, String ln) {
+//                if (mtomThreshold == -1 || mtomThreshold > length) return null;
+                byte[] bytes = (offset == 0 && length == data.length) ? data : Arrays.copyOfRange(data, offset, offset + length);
+//                binaryText = (BinaryTextImpl) ((ElementImpl) currentElement).addAsBase64TextNode(bytes);
+                if (currentElement instanceof MtomEnabled) {
+                    binaryText = ((MtomEnabled) currentElement).addBinaryText(bytes);
+                } else {
+                    throw new IllegalStateException("The currentElement is not MtomEnabled " + currentElement);
+                }
+                return binaryText.getHref();
+            }
+
+            @Override
+            public String addSwaRefAttachment(DataHandler data) {
+                return "cid:"+encodeCid();
+            }
+
+            @Override
+            public boolean isXOPPackage() {
+                return true;
+            }
+        };
+    }
+}
--- a/jaxws/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/BinaryText.java	Wed Jul 05 19:38:35 2017 +0200
+++ b/jaxws/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/BinaryText.java	Mon Apr 28 14:05:34 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,15 +26,13 @@
 package com.sun.xml.internal.org.jvnet.staxex;
 
 import javax.activation.DataHandler;
-import javax.xml.soap.SOAPException;
-import javax.xml.soap.Text;
 
 /**
  * BinaryText represents a MTOM attachment.
  *
  * @author shih-chang.chen@oracle.com
  */
-public interface BinaryText extends Text {
+public interface BinaryText {
     public String getHref();
-    public DataHandler getDataHandler() throws SOAPException;
+    public DataHandler getDataHandler();
 }
--- a/jaxws/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/StAxSOAPBody.java	Wed Jul 05 19:38:35 2017 +0200
+++ b/jaxws/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/StAxSOAPBody.java	Mon Apr 28 14:05:34 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,6 @@
 package com.sun.xml.internal.org.jvnet.staxex;
 
 import javax.xml.namespace.QName;
-import javax.xml.soap.SOAPException;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
@@ -46,7 +45,6 @@
         /**
          * Retrieve payload qname without materializing its contents
          * @return
-         * @throws SOAPException
          */
        public QName getPayloadQName();
 
@@ -58,7 +56,7 @@
         * Retrieve payload attribute value without materializing its contents
         * @param localName
         * @return
-        * @throws SOAPException
+        * @throws XMLStreamException
         */
        public String getPayloadAttributeValue(String localName) throws XMLStreamException;
 
@@ -66,16 +64,16 @@
         * Retrieve payload attribute value without materializing its contents
         * @param qName
         * @return
-        * @throws SOAPException
+        * @throws XMLStreamException
         */
        public String getPayloadAttributeValue(QName qName) throws XMLStreamException;
 
-       public void materialize() throws SOAPException;
+       public void materialize() throws XMLStreamException;
     }
 
-    public void setPayload(Payload src) throws SOAPException;
+    public void setPayload(Payload src) throws XMLStreamException;
 
-    public Payload getPayload()throws SOAPException;
+    public Payload getPayload()throws XMLStreamException;
 
     public boolean hasStaxPayload();
  }
--- a/jaxws/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SAAJFactory.java	Wed Jul 05 19:38:35 2017 +0200
+++ b/jaxws/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SAAJFactory.java	Mon Apr 28 14:05:34 2014 -0700
@@ -36,7 +36,6 @@
 import javax.xml.stream.XMLStreamException;
 
 import org.xml.sax.SAXException;
-import com.sun.xml.internal.org.jvnet.staxex.util.SaajStaxWriter;
 
 import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
 import com.sun.xml.internal.ws.api.SOAPVersion;
--- a/jaxws/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SaajStaxWriter.java	Wed Jul 05 19:38:35 2017 +0200
+++ b/jaxws/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SaajStaxWriter.java	Mon Apr 28 14:05:34 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -42,8 +42,6 @@
 /**
  * SaajStaxWriter builds a SAAJ SOAPMessage by using XMLStreamWriter interface.
  *
- * @deprecated use com.sun.xml.internal.org.jvnet.staxex.util.SaajStaxWriter
- *
  * @author shih-chang.chen@oracle.com
  */
 public class SaajStaxWriter implements XMLStreamWriter {
@@ -57,16 +55,21 @@
     static final protected String Body = "Body";
     static final protected String xmlns = "xmlns";
 
-    public SaajStaxWriter(final SOAPMessage msg) throws SOAPException {
+    private boolean isHeaderSeen = false;
+
+    public SaajStaxWriter(final SOAPMessage msg, String uri) throws SOAPException {
         soap = msg;
-        currentElement = soap.getSOAPPart().getEnvelope();
-        envURI = currentElement.getNamespaceURI();
+        this.envURI = uri;
     }
 
     public SOAPMessage getSOAPMessage() {
         return soap;
     }
 
+    protected SOAPElement getEnvelope() throws SOAPException {
+        return soap.getSOAPPart().getEnvelope();
+    }
+
     @Override
     public void writeStartElement(final String localName) throws XMLStreamException {
         try {
@@ -86,10 +89,11 @@
         try {
             if (envURI.equals(ns)) {
                 if (Envelope.equals(ln)) {
-                    currentElement = soap.getSOAPPart().getEnvelope();
+                    currentElement = getEnvelope();
                     fixPrefix(prefix);
                     return;
                 } else if (Header.equals(ln)) {
+                    isHeaderSeen = true;
                     currentElement = soap.getSOAPHeader();
                     fixPrefix(prefix);
                     return;
@@ -108,9 +112,13 @@
     }
 
     private void fixPrefix(final String prfx) throws XMLStreamException {
-        String oldPrfx = currentElement.getPrefix();
+        fixPrefix(prfx, currentElement);
+    }
+
+    private void fixPrefix(final String prfx, SOAPElement element) throws XMLStreamException {
+        String oldPrfx = element.getPrefix();
         if (prfx != null && !prfx.equals(oldPrfx)) {
-            currentElement.setPrefix(prfx);
+            element.setPrefix(prfx);
         }
     }
 
@@ -136,6 +144,21 @@
 
     @Override
     public void writeEndDocument() throws XMLStreamException {
+        try {
+            if (!isHeaderSeen) {
+                SOAPElement header = soap.getSOAPHeader();
+                if (header != null) {
+                    String prefixAtHeader = header.getPrefix();
+                    SOAPElement env = getEnvelope();
+                    header.detachNode();
+                    if (prefixAtHeader != null && !prefixAtHeader.equals(env.getPrefix())) {
+                        env.removeNamespaceDeclaration(prefixAtHeader);
+                    }
+                }
+            }
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
     }
 
     @Override