Skip to content

Commit

Permalink
Merged revisions 1561774 via git cherry-pick from
Browse files Browse the repository at this point in the history
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1561774 | dkulp | 2014-01-27 13:29:53 -0500 (Mon, 27 Jan 2014) | 2 lines

  [CXF-5527] Update the SAAJStreamWriter to use the SAAJ methods to create children of Body and Header so that they end up as the proper SOAPHeaderElement or SOAPBodyElement types immediately.  Otherwise, they may be converted to those types later.

........

git-svn-id: https://svn.apache.org/repos/asf/cxf/branches/2.7.x-fixes@1561822 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
dkulp committed Feb 19, 2014
1 parent be5043c commit eb70d10
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 18 deletions.
51 changes: 43 additions & 8 deletions api/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
Expand Up @@ -691,26 +691,55 @@ public static void writeTo(Node node, Writer os, int indent) throws XMLStreamExc
* @throws XMLStreamException
*/
public static void copy(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException {
copy(reader, writer, false);
copy(reader, writer, false, false);
}
public static void copy(XMLStreamReader reader, XMLStreamWriter writer,
boolean fragment) throws XMLStreamException {
public static void copy(XMLStreamReader reader, XMLStreamWriter writer, boolean fragment)
throws XMLStreamException {
copy(reader, writer, fragment, false);
}
public static void copy(XMLStreamReader reader,
XMLStreamWriter writer,
boolean fragment,
boolean isThreshold) throws XMLStreamException {
// number of elements read in
int read = 0;
int elementCount = 0;
Stack<Integer> countStack = new Stack<Integer>();
int event = reader.getEventType();

while (reader.hasNext()) {
switch (event) {
case XMLStreamConstants.START_ELEMENT:
read++;
if (isThreshold) {
elementCount++;

if (innerElementLevelThreshold != -1
&& read >= innerElementLevelThreshold) {
throw new DepthExceededStaxException("reach the innerElementLevelThreshold:"
+ innerElementLevelThreshold);
}
if (innerElementCountThreshold != -1
&& elementCount >= innerElementCountThreshold) {
throw new DepthExceededStaxException("reach the innerElementCountThreshold:"
+ innerElementCountThreshold);
}
countStack.push(elementCount);
elementCount = 0;
}
writeStartElement(reader, writer);
break;
case XMLStreamConstants.END_ELEMENT:
writer.writeEndElement();
if (read > 0) {
writer.writeEndElement();
}
read--;
if (read <= 0 && !fragment) {
return;
}
if (isThreshold && !countStack.isEmpty()) {
elementCount = countStack.pop();
}
break;
case XMLStreamConstants.CHARACTERS:
String s = reader.getText();
Expand Down Expand Up @@ -751,6 +780,10 @@ private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter wr
if (uri != null) {
writeElementNS = true;
Iterator<String> it = CastUtils.cast(writer.getNamespaceContext().getPrefixes(uri));
if (!it.hasNext() && StringUtils.isEmpty(prefix) && StringUtils.isEmpty(uri)
&& StringUtils.isEmpty(writer.getNamespaceContext().getNamespaceURI(""))) {
writeElementNS = false;
}
while (it != null && it.hasNext()) {
String s = it.next();
if (s == null) {
Expand Down Expand Up @@ -1739,22 +1772,24 @@ public static void print(Node node) {
}
}

public static String toString(Source src) throws XMLStreamException {
public static String toString(Source src) {
StringWriter sw = new StringWriter(1024);
XMLStreamWriter writer = null;
try {
writer = createXMLStreamWriter(sw);
copy(src, writer);
writer.flush();
} catch (XMLStreamException e) {
throw new RuntimeException(e);
} finally {
StaxUtils.close(writer);
}
return sw.toString();
}
public static String toString(Node src) throws XMLStreamException {
public static String toString(Node src) {
return toString(new DOMSource(src));
}
public static String toString(Document doc) throws XMLStreamException {
public static String toString(Document doc) {
StringWriter sw = new StringWriter(1024);
XMLStreamWriter writer = null;
try {
Expand All @@ -1768,7 +1803,7 @@ public static String toString(Document doc) throws XMLStreamException {
}
return sw.toString();
}
public static String toString(Element el) throws XMLStreamException {
public static String toString(Element el) {
return toString(el, 0);
}
public static String toString(Element el, int indent) {
Expand Down
20 changes: 13 additions & 7 deletions api/src/main/java/org/apache/cxf/staxutils/W3CDOMStreamWriter.java
Expand Up @@ -34,8 +34,8 @@
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.helpers.MapNamespaceContext;
import org.apache.cxf.helpers.XMLUtils;

public class W3CDOMStreamWriter implements XMLStreamWriter {
static final String XML_NS = "http://www.w3.org/2000/xmlns/";
Expand All @@ -47,7 +47,7 @@ public class W3CDOMStreamWriter implements XMLStreamWriter {
private Map<String, Object> properties = Collections.emptyMap();

public W3CDOMStreamWriter() throws ParserConfigurationException {
document = XMLUtils.newDocument();
document = DOMUtils.newDocument();
}

public W3CDOMStreamWriter(DocumentBuilder builder) {
Expand Down Expand Up @@ -144,15 +144,23 @@ public void writeStartElement(String prefix, String local, String namespace) thr
}
}
}

protected Element createElementNS(String ns, String pfx, String local) {
if (pfx != null) {
local = pfx + ":" + local;
}
return document.createElementNS(ns, local);
}

protected void createAndAddElement(String prefix, String local, String namespace) {
if (prefix == null) {
if (namespace == null) {
newChild(document.createElementNS(null, local));
newChild(createElementNS(null, null, local));
} else {
newChild(document.createElementNS(namespace, local));
newChild(createElementNS(namespace, null, local));
}
} else {
newChild(document.createElementNS(namespace, prefix + ":" + local));
newChild(createElementNS(namespace, prefix, local));
}
}

Expand Down Expand Up @@ -335,8 +343,6 @@ public String toString() {
}
try {
return StaxUtils.toString(document);
} catch (XMLStreamException e) {
return super.toString();
} catch (Throwable t) {
t.printStackTrace();
return super.toString();
Expand Down
Expand Up @@ -220,8 +220,10 @@ public void handleMessage(SoapMessage message) throws Fault {
soapMessage.getSOAPPart().getEnvelope().addHeader();
}

StaxUtils.readDocElements(soapMessage.getSOAPPart().getEnvelope().getBody(),
xmlReader, true, true);
StaxUtils.copy(xmlReader,
new SAAJStreamWriter(soapMessage.getSOAPPart(),
soapMessage.getSOAPPart().getEnvelope().getBody()),
true, true);
DOMSource bodySource = new DOMSource(soapMessage.getSOAPPart().getEnvelope().getBody());
xmlReader = StaxUtils.createXMLStreamReader(bodySource);
xmlReader.nextTag();
Expand Down
Expand Up @@ -18,9 +18,12 @@
*/
package org.apache.cxf.binding.soap.saaj;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPPart;

import org.w3c.dom.Element;
Expand Down Expand Up @@ -124,4 +127,46 @@ protected void createAndAddElement(String prefix, String local, String namespace
}
super.createAndAddElement(prefix, local, namespace);
}

@Override
protected Element createElementNS(String ns, String pfx, String local) {
Element cur = getCurrentNode();
if (cur instanceof SOAPBody) {
try {
if (StringUtils.isEmpty(pfx) && StringUtils.isEmpty(ns)) {
Element el = ((SOAPBody)cur).addBodyElement(new QName(local));
cur.removeChild(el);
return el;
}
Element el = ((SOAPBody)cur).addBodyElement(new QName(ns, local, pfx == null ? "" : pfx));
cur.removeChild(el);
return el;
} catch (SOAPException e) {
//ignore
}
} else if (cur instanceof SOAPHeader) {
try {
Element el = ((SOAPHeader)cur).addHeaderElement(new QName(ns, local, pfx == null ? "" : pfx));
cur.removeChild(el);
return el;
} catch (SOAPException e) {
//ignore
}
} else if (cur instanceof SOAPElement) {
try {
Element el = null;
if (StringUtils.isEmpty(pfx) && StringUtils.isEmpty(ns)) {
el = ((SOAPElement)cur).addChildElement(local);
} else {
el = ((SOAPElement)cur).addChildElement(local, pfx, ns);
}
cur.removeChild(el);
return el;
} catch (SOAPException e) {
//ignore
}
}
return super.createElementNS(ns, pfx, local);
}

}
Expand Up @@ -88,7 +88,7 @@ public static void main(String[] args) {
@BeforeClass
public static void startServers() throws Exception {
//must be out of process so the system properties aren't in effect
assertTrue("server did not launch correctly", launchServer(Server.class, false));
assertTrue("server did not launch correctly", launchServer(Server.class, true));
}

@org.junit.Before
Expand Down

0 comments on commit eb70d10

Please sign in to comment.