1 17 package org.apache.servicemix.components.http; 18 19 import javax.jbi.messaging.MessageExchange; 20 import javax.jbi.messaging.NormalizedMessage; 21 import javax.xml.transform.dom.DOMSource ; 22 23 import org.apache.commons.httpclient.HttpMethod; 24 import org.apache.commons.httpclient.methods.PostMethod; 25 import org.apache.commons.httpclient.methods.StringRequestEntity; 26 import org.apache.servicemix.jbi.jaxp.StringSource; 27 import org.apache.xpath.CachedXPathAPI; 28 import org.w3c.dom.Attr ; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.NamedNodeMap ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.traversal.NodeIterator; 34 35 40 public class HttpSoapClientMarshaler extends HttpClientMarshaler { 41 42 public HttpSoapClientMarshaler() { 43 super(); 44 } 45 46 public HttpSoapClientMarshaler(boolean streaming) { 47 super(streaming); 48 } 49 50 public void toNMS(NormalizedMessage normalizedMessage, HttpMethod method) 51 throws Exception { 52 addNmsProperties(normalizedMessage, method); 53 String response = method.getResponseBodyAsString(); 54 Node node = sourceTransformer.toDOMNode(new StringSource(response)); 55 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 56 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "/*/*[local-name()='Body']/*"); 57 Node root = iterator.nextNode(); 58 if (root instanceof Element == false) { 59 throw new IllegalStateException ("Could not find body content"); 60 } 61 Element element = (Element ) root; 62 63 for (Node parent = element.getParentNode(); parent != null; parent = parent.getParentNode()) { 65 NamedNodeMap attributes = parent.getAttributes(); 66 if (attributes != null) { 67 for (int i = 0; i < attributes.getLength(); i++) { 68 Attr att = (Attr ) attributes.item(i); 69 if (att.getName().startsWith("xmlns:") 70 && element.getAttributes().getNamedItemNS(att.getNamespaceURI(), 71 att.getLocalName()) == null) { 72 element.setAttributeNS(att.getNamespaceURI(), 73 att.getName(), 74 att.getValue()); 75 } 76 } 77 } 78 } 79 80 normalizedMessage.setContent(new DOMSource (element)); 81 } 82 83 public void fromNMS(PostMethod method, MessageExchange exchange, NormalizedMessage normalizedMessage) throws Exception { 84 addHttpHeaders(method, normalizedMessage); 85 Element elem = sourceTransformer.toDOMElement(normalizedMessage.getContent()); 86 Document document = sourceTransformer.createDocument(); 87 Element env = document.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "env:Envelope"); 88 document.appendChild(env); 89 env.setAttribute("xmlns:env", "http://schemas.xmlsoap.org/soap/envelope/"); 90 Element body = document.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "env:Body"); 91 env.appendChild(body); 92 body.appendChild(document.importNode(elem, true)); 93 String text = sourceTransformer.toString(document); 94 method.setRequestEntity(new StringRequestEntity(text)); 95 } 96 97 } 98 | Popular Tags |