KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpSoapClientMarshaler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
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 JavaDoc;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.NamedNodeMap JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.traversal.NodeIterator;
34
35 /**
36  * A class which marshalls a client HTTP request to a NMS message
37  *
38  * @version $Revision: 431309 $
39  */

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 JavaDoc {
52         addNmsProperties(normalizedMessage, method);
53         String JavaDoc response = method.getResponseBodyAsString();
54         Node JavaDoc node = sourceTransformer.toDOMNode(new StringSource(response));
55         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
56         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "/*/*[local-name()='Body']/*");
57         Node JavaDoc root = iterator.nextNode();
58         if (root instanceof Element JavaDoc == false) {
59             throw new IllegalStateException JavaDoc("Could not find body content");
60         }
61         Element JavaDoc element = (Element JavaDoc) root;
62         
63         // Copy embedded namespaces from the envelope into the body root
64
for (Node JavaDoc parent = element.getParentNode(); parent != null; parent = parent.getParentNode()) {
65             NamedNodeMap JavaDoc attributes = parent.getAttributes();
66             if (attributes != null) {
67                 for (int i = 0; i < attributes.getLength(); i++) {
68                     Attr JavaDoc att = (Attr JavaDoc) 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 JavaDoc(element));
81     }
82
83     public void fromNMS(PostMethod method, MessageExchange exchange, NormalizedMessage normalizedMessage) throws Exception JavaDoc {
84         addHttpHeaders(method, normalizedMessage);
85         Element JavaDoc elem = sourceTransformer.toDOMElement(normalizedMessage.getContent());
86         Document JavaDoc document = sourceTransformer.createDocument();
87         Element JavaDoc 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 JavaDoc body = document.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "env:Body");
91         env.appendChild(body);
92         body.appendChild(document.importNode(elem, true));
93         String JavaDoc text = sourceTransformer.toString(document);
94         method.setRequestEntity(new StringRequestEntity(text));
95     }
96
97 }
98
Popular Tags