Skip to content

Commit e72d131

Browse files
committed
Modifed to work with DOM Element as Message content
1 parent aee54ec commit e72d131

File tree

7 files changed

+118
-39
lines changed

7 files changed

+118
-39
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.project
2+
.classpath
3+
.settings/
4+
target/

src/main/java/org/switchyard/soap/DefaultMessageComposer.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@
2222

2323
package org.switchyard.soap;
2424

25-
import java.io.StringWriter;
2625
import java.util.Iterator;
2726

2827
import javax.xml.soap.Node;
2928
import javax.xml.soap.SOAPBody;
3029
import javax.xml.soap.SOAPElement;
3130
import javax.xml.soap.SOAPException;
3231
import javax.xml.soap.SOAPMessage;
33-
import javax.xml.stream.XMLEventWriter;
34-
import javax.xml.transform.stream.StreamResult;
3532

3633
import org.switchyard.Message;
3734
import org.switchyard.MessageBuilder;
38-
import org.switchyard.soap.util.XMLHelper;
3935

4036
/**
4137
* The default implementation of MessageComposer simply copies the SOAP body into
@@ -66,10 +62,8 @@ public Message compose(final SOAPMessage soapMessage) throws SOAPException {
6662
if (found) {
6763
throw new SOAPException("Found multiple SOAPElements in SOAPBody");
6864
}
69-
final StringWriter sw = new StringWriter();
70-
final XMLEventWriter writer = XMLHelper.getXMLEventWriter(new StreamResult(sw));
71-
XMLHelper.readDomNode(node, writer, true);
72-
message.setContent(sw.toString());
65+
node.detachNode();
66+
message.setContent(node);
7367
found = true;
7468
}
7569
}

src/main/java/org/switchyard/soap/DefaultMessageDecomposer.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
import org.switchyard.Message;
2929
import org.switchyard.soap.util.SOAPUtil;
30-
import org.w3c.dom.Document;
30+
import org.w3c.dom.Element;
31+
import org.w3c.dom.Node;
3132

3233
/**
3334
* The default implementation of MessageDecomposer simply copies the Message body onto SOAP
@@ -48,14 +49,13 @@ public SOAPMessage decompose(final Message message) throws SOAPException {
4849

4950
final SOAPMessage response = SOAPUtil.SOAP_MESSAGE_FACTORY.createMessage();
5051
if (message != null) {
51-
final Object input = message.getContent();
52+
final Element input = message.getContent(Element.class);
5253
if (input == null) {
5354
throw new SOAPException("Null response from service");
5455
}
55-
final String soapRes = input.toString();
5656
try {
57-
final Document root = SOAPUtil.parseAsDom(soapRes);
58-
response.getSOAPBody().addDocument(root);
57+
Node node = response.getSOAPBody().getOwnerDocument().importNode(input, true);
58+
response.getSOAPBody().appendChild(node);
5959
} catch (Exception e) {
6060
throw new SOAPException("Unable to parse SOAP Message", e);
6161
}

src/main/java/org/switchyard/soap/OutboundHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import javax.xml.namespace.QName;
3131
import javax.xml.soap.SOAPException;
3232
import javax.xml.soap.SOAPMessage;
33-
import javax.xml.ws.BindingProvider;
33+
//import javax.xml.ws.BindingProvider;
3434
import javax.xml.ws.Dispatch;
3535
import javax.xml.ws.Service;
3636
import javax.xml.ws.soap.AddressingFeature;
@@ -142,8 +142,8 @@ private SOAPMessage invokeService(final SOAPMessage soapMessage) throws SOAPExce
142142
URL wsdlUrl = new URL(_wsdlLocation);
143143
Service service = Service.create(wsdlUrl, serviceName);
144144
_dispatcher = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE, new AddressingFeature(false, false));
145-
BindingProvider bp = (BindingProvider) _dispatcher;
146-
// this does not return a proper qualified Fault element and has no Detail so deffering for now
145+
// this does not return a proper qualified Fault element and has no Detail so defering for now
146+
// BindingProvider bp = (BindingProvider) _dispatcher;
147147
// bp.getRequestContext().put("jaxws.response.throwExceptionIfSOAPFault", Boolean.FALSE);
148148

149149
} catch (Exception e) {

src/main/java/org/switchyard/soap/util/XMLHelper.java

+72-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.OutputStream;
2828
import java.io.Reader;
2929
import java.io.StringReader;
30+
import java.io.StringWriter;
3031
import java.io.Writer;
3132
import java.util.concurrent.atomic.AtomicReference;
3233

@@ -46,16 +47,21 @@
4647
import javax.xml.stream.events.XMLEvent;
4748
import javax.xml.transform.Result;
4849
import javax.xml.transform.Source;
50+
import javax.xml.transform.Transformer;
51+
import javax.xml.transform.TransformerException;
52+
import javax.xml.transform.TransformerFactory;
4953
import javax.xml.transform.dom.DOMResult;
5054
import javax.xml.transform.dom.DOMSource;
55+
import javax.xml.transform.stream.StreamResult;
5156
import javax.xml.transform.stream.StreamSource;
5257
import javax.xml.validation.Schema;
53-
import javax.xml.validation.SchemaFactory;
5458
import javax.xml.validation.Validator;
5559

5660
import org.apache.log4j.Logger;
61+
import org.w3c.dom.Element;
5762
import org.w3c.dom.Document;
5863
import org.w3c.dom.Node;
64+
import org.w3c.dom.NodeList;
5965
import org.xml.sax.InputSource;
6066
import org.xml.sax.SAXException;
6167

@@ -402,8 +408,29 @@ public static boolean compareXMLContent(final InputSource content1, final InputS
402408
return (handler1.getRootElement().equals(handler2.getRootElement()));
403409
}
404410

405-
private static SchemaFactory newSchemaFactory() {
406-
return SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
411+
/**
412+
* Compare two DOM Nodes.
413+
* @param node1 The first Node.
414+
* @param node2 The second Node.
415+
* @return true if equals, false otherwise.
416+
* @throws ParserConfigurationException Parser confiuration exception
417+
* @throws TransformerException Transformer exception
418+
* @throws SAXException SAX exception
419+
* @throws IOException If unable to read the stream
420+
*/
421+
public static boolean compareXMLContent(final Node node1, final Node node2)
422+
throws ParserConfigurationException, TransformerException, SAXException, IOException {
423+
TransformerFactory transFactory = TransformerFactory.newInstance();
424+
Transformer transformer = transFactory.newTransformer();
425+
StringWriter writer1 = new StringWriter();
426+
StringWriter writer2 = new StringWriter();
427+
DOMSource source = new DOMSource(node1);
428+
StreamResult result = new StreamResult(writer1);
429+
transformer.transform(source, result);
430+
source = new DOMSource(node2);
431+
result = new StreamResult(writer2);
432+
transformer.transform(source, result);
433+
return compareXMLContent(writer1.toString(), writer2.toString());
407434
}
408435

409436
/**
@@ -447,6 +474,48 @@ private static Document getNewDocument()
447474
return builder.newDocument();
448475
}
449476
}
477+
478+
/**
479+
* Transform a DOM Node to String.
480+
* @param node The Node to be transformed.
481+
* @return a String representation.
482+
* @throws ParserConfigurationException Parser confiuration exception
483+
* @throws TransformerException Transformer exception
484+
*/
485+
public static String toString(final Node node)
486+
throws ParserConfigurationException, TransformerException {
487+
TransformerFactory transFactory = TransformerFactory.newInstance();
488+
Transformer transformer = transFactory.newTransformer();
489+
StringWriter writer = new StringWriter();
490+
DOMSource source = new DOMSource(node);
491+
StreamResult result = new StreamResult(writer);
492+
transformer.transform(source, result);
493+
return writer.toString();
494+
}
495+
496+
/**
497+
* Get the first child Element of the supplied node that matches a given tag name.
498+
*
499+
* @param node The DOM Node.
500+
* @param name The name of the child node to search for.
501+
* @return The first child element with the matching tag name.
502+
*/
503+
public static Element getFirstChildElementByName(Node node, String name) {
504+
NodeList children = node.getChildNodes();
505+
int childCount = children.getLength();
506+
507+
for (int i = 0; i < childCount; i++) {
508+
Node child = children.item(i);
509+
if (child != null
510+
&& child.getNodeType() == Node.ELEMENT_NODE
511+
&& child.getNodeName() != null
512+
&& child.getNodeName().equals(name)) {
513+
return (Element) child;
514+
}
515+
}
516+
517+
return null;
518+
}
450519

451520
/**
452521
* Get the document builder for creation

src/test/java/org/switchyard/soap/SOAPGatewayTest.java

+17-14
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
import org.switchyard.MockHandler;
3939
import org.switchyard.ServiceDomain;
4040
import org.switchyard.internal.ServiceDomains;
41+
import org.switchyard.soap.util.SOAPUtil;
4142
import org.switchyard.soap.util.XMLHelper;
4243

44+
import org.w3c.dom.Element;
45+
4346
public class SOAPGatewayTest {
4447
private static final QName PUBLISH_AS_WS_SERVICE = new QName("publish-as-ws");
4548
private static final QName WS_CONSUMER_SERVICE = new QName("webservice-consumer");
@@ -84,9 +87,9 @@ public static void tearDown() throws Exception {
8487

8588
@Test
8689
public void invokeOneWay() throws Exception {
87-
String input = "<test:helloWS xmlns:test=\"http://test.ws/\">"
90+
Element input = SOAPUtil.parseAsDom("<test:helloWS xmlns:test=\"http://test.ws/\">"
8891
+ " <arg0>Hello</arg0>"
89-
+ "</test:helloWS>";
92+
+ "</test:helloWS>").getDocumentElement();
9093

9194
// Invoke the WS via our WS Consumer service
9295
MockHandler consumer = new MockHandler();
@@ -98,13 +101,13 @@ public void invokeOneWay() throws Exception {
98101

99102
@Test
100103
public void invokeRequestResponse() throws Exception {
101-
String input = "<test:sayHello xmlns:test=\"http://test.ws/\">"
104+
Element input = SOAPUtil.parseAsDom("<test:sayHello xmlns:test=\"http://test.ws/\">"
102105
+ " <arg0>Jimbo</arg0>"
103-
+ "</test:sayHello>";
106+
+ "</test:sayHello>").getDocumentElement();
104107

105-
String output = "<test:sayHelloResponse xmlns:test=\"http://test.ws/\">"
108+
Element output = SOAPUtil.parseAsDom("<test:sayHelloResponse xmlns:test=\"http://test.ws/\">"
106109
+ " <return>Hello Jimbo</return>"
107-
+ "</test:sayHelloResponse>";
110+
+ "</test:sayHelloResponse>").getDocumentElement();
108111

109112
// Invoke the WS via our WS Consumer service
110113
MockHandler consumer = new MockHandler();
@@ -113,24 +116,24 @@ public void invokeRequestResponse() throws Exception {
113116
message.setContent(input);
114117
exchange.send(message);
115118
consumer.waitForMessage();
116-
String response = consumer._messages.peek().getMessage().getContent(String.class);
117-
Assert.assertTrue("Expected \r\n" + output + "\r\nbut was \r\n" + response, XMLHelper.compareXMLContent(output, response));
119+
Element response = consumer.getMessages().peek().getMessage().getContent(Element.class);
120+
Assert.assertTrue("Expected \r\n" + XMLHelper.toString(output) + "\r\nbut was \r\n" + XMLHelper.toString(response), XMLHelper.compareXMLContent(output, response));
118121
}
119122

120123
@Test
121124
public void invokeRequestResponseFault() throws Exception {
122-
String input = "<test:sayHello xmlns:test=\"http://test.ws/\">"
125+
Element input = SOAPUtil.parseAsDom("<test:sayHello xmlns:test=\"http://test.ws/\">"
123126
+ " <arg0></arg0>"
124-
+ "</test:sayHello>";
127+
+ "</test:sayHello>").getDocumentElement();
125128

126-
String output = "<soap:Fault xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
129+
Element output = SOAPUtil.parseAsDom("<soap:Fault xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
127130
+ " <faultcode>soap:Server.AppError</faultcode>"
128131
+ " <faultstring>Invalid name</faultstring>"
129132
+ " <detail>"
130133
+ " <message>Looks like you did not specify a name!</message>"
131134
+ " <errorcode>1000</errorcode>"
132135
+ " </detail>"
133-
+ "</soap:Fault>";
136+
+ "</soap:Fault>").getDocumentElement();
134137

135138
// Invoke the WS via our WS Consumer service
136139
MockHandler consumer = new MockHandler();
@@ -139,7 +142,7 @@ public void invokeRequestResponseFault() throws Exception {
139142
message.setContent(input);
140143
exchange.send(message);
141144
consumer.waitForMessage();
142-
String response = consumer._messages.peek().getMessage().getContent(String.class);
143-
Assert.assertTrue("Expected \r\n" + output + "\r\nbut was \r\n" + response, XMLHelper.compareXMLContent(output, response));
145+
Element response = consumer.getMessages().peek().getMessage().getContent(Element.class);
146+
Assert.assertTrue("Expected \r\n" + XMLHelper.toString(output) + "\r\nbut was \r\n" + XMLHelper.toString(response), XMLHelper.compareXMLContent(output, response));
144147
}
145148
}

src/test/java/org/switchyard/soap/SOAPProvider.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,23 @@
2929
import org.switchyard.Message;
3030
import org.switchyard.MessageBuilder;
3131
import org.switchyard.message.FaultMessage;
32+
import org.switchyard.soap.util.SOAPUtil;
33+
import org.switchyard.soap.util.XMLHelper;
34+
35+
import org.w3c.dom.Document;
36+
import org.w3c.dom.Element;
3237

3338
public class SOAPProvider extends BaseHandler {
3439

3540
@Override
3641
public void handleMessage(Exchange exchange) throws HandlerException {
3742
if (exchange.getPattern().equals(ExchangePattern.IN_OUT)) {
3843
Message message;
39-
String request = exchange.getMessage().getContent(String.class);
44+
Element request = exchange.getMessage().getContent(Element.class);
45+
Element name = XMLHelper.getFirstChildElementByName(request, "arg0");
4046
String toWhom = "";
41-
int argIdx = request.indexOf("<arg0>");
42-
if (argIdx > 0) {
43-
toWhom = request.substring(argIdx + 6, request.indexOf("</arg0>"));
47+
if (name != null) {
48+
toWhom = name.getTextContent();
4449
}
4550
String response = null;
4651
if (toWhom.length() == 0) {
@@ -59,8 +64,12 @@ public void handleMessage(Exchange exchange) throws HandlerException {
5964
+ " <return>Hello " + toWhom + "</return>"
6065
+ "</test:sayHelloResponse>";
6166
}
62-
63-
message.setContent(response);
67+
try {
68+
Document responseDom = SOAPUtil.parseAsDom(response);
69+
message.setContent(responseDom.getDocumentElement());
70+
} catch (Exception e) {
71+
// Generate fault
72+
}
6473
exchange.send(message);
6574
}
6675
}

0 commit comments

Comments
 (0)