001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.http;
018:
019: import javax.jbi.messaging.MessageExchange;
020: import javax.jbi.messaging.NormalizedMessage;
021: import javax.xml.transform.dom.DOMSource;
022:
023: import org.apache.commons.httpclient.HttpMethod;
024: import org.apache.commons.httpclient.methods.PostMethod;
025: import org.apache.commons.httpclient.methods.StringRequestEntity;
026: import org.apache.servicemix.jbi.jaxp.StringSource;
027: import org.apache.xpath.CachedXPathAPI;
028: import org.w3c.dom.Attr;
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Element;
031: import org.w3c.dom.NamedNodeMap;
032: import org.w3c.dom.Node;
033: import org.w3c.dom.traversal.NodeIterator;
034:
035: /**
036: * A class which marshalls a client HTTP request to a NMS message
037: *
038: * @version $Revision: 431309 $
039: */
040: public class HttpSoapClientMarshaler extends HttpClientMarshaler {
041:
042: public HttpSoapClientMarshaler() {
043: super ();
044: }
045:
046: public HttpSoapClientMarshaler(boolean streaming) {
047: super (streaming);
048: }
049:
050: public void toNMS(NormalizedMessage normalizedMessage,
051: HttpMethod method) throws Exception {
052: addNmsProperties(normalizedMessage, method);
053: String response = method.getResponseBodyAsString();
054: Node node = sourceTransformer.toDOMNode(new StringSource(
055: response));
056: CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
057: NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node,
058: "/*/*[local-name()='Body']/*");
059: Node root = iterator.nextNode();
060: if (root instanceof Element == false) {
061: throw new IllegalStateException(
062: "Could not find body content");
063: }
064: Element element = (Element) root;
065:
066: // Copy embedded namespaces from the envelope into the body root
067: for (Node parent = element.getParentNode(); parent != null; parent = parent
068: .getParentNode()) {
069: NamedNodeMap attributes = parent.getAttributes();
070: if (attributes != null) {
071: for (int i = 0; i < attributes.getLength(); i++) {
072: Attr att = (Attr) attributes.item(i);
073: if (att.getName().startsWith("xmlns:")
074: && element.getAttributes().getNamedItemNS(
075: att.getNamespaceURI(),
076: att.getLocalName()) == null) {
077: element.setAttributeNS(att.getNamespaceURI(),
078: att.getName(), att.getValue());
079: }
080: }
081: }
082: }
083:
084: normalizedMessage.setContent(new DOMSource(element));
085: }
086:
087: public void fromNMS(PostMethod method, MessageExchange exchange,
088: NormalizedMessage normalizedMessage) throws Exception {
089: addHttpHeaders(method, normalizedMessage);
090: Element elem = sourceTransformer.toDOMElement(normalizedMessage
091: .getContent());
092: Document document = sourceTransformer.createDocument();
093: Element env = document.createElementNS(
094: "http://schemas.xmlsoap.org/soap/envelope/",
095: "env:Envelope");
096: document.appendChild(env);
097: env.setAttribute("xmlns:env",
098: "http://schemas.xmlsoap.org/soap/envelope/");
099: Element body = document
100: .createElementNS(
101: "http://schemas.xmlsoap.org/soap/envelope/",
102: "env:Body");
103: env.appendChild(body);
104: body.appendChild(document.importNode(elem, true));
105: String text = sourceTransformer.toString(document);
106: method.setRequestEntity(new StringRequestEntity(text));
107: }
108:
109: }
|