Skip to content
Permalink
Browse files
[CXF-5527] Update the SAAJStreamWriter to use the SAAJ methods to cre…
…ate 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/trunk@1561774 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
dkulp committed Jan 27, 2014
1 parent 882d04c commit de76767fa5f2e72b1cf3cbbb9bfd72e43cb4aab3
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
@@ -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();
@@ -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) {
@@ -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));
}
}

@@ -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();
@@ -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;
@@ -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);
}

}
@@ -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
@@ -581,7 +581,8 @@ public void testClientChecker() throws Exception {

// This test only applies to the DOM implementation
if (PORT.equals(test.getPort()) && !test.isStreaming()) {
port.doubleIt(25);
int result = port.doubleIt(25);
assertEquals(50, result);

// Now try with a message that will create a Fault in the SEI
try {

0 comments on commit de76767

Please sign in to comment.