KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > wsif > WSIFMarshaler


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.wsif;
18
19 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
20 import org.apache.servicemix.jbi.util.DOMUtil;
21 import org.apache.servicemix.jbi.util.LazyDOMSource;
22 import org.apache.wsif.WSIFException;
23 import org.apache.wsif.WSIFMessage;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29 import javax.jbi.messaging.MessageExchange;
30 import javax.jbi.messaging.MessagingException;
31 import javax.jbi.messaging.NormalizedMessage;
32 import javax.wsdl.Part;
33 import javax.xml.namespace.QName JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import javax.xml.transform.TransformerException JavaDoc;
36
37 import java.io.IOException JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * A class which marshalls a WSIF messages into and out of NMS
43  *
44  * @version $Revision: 431309 $
45  */

46 public class WSIFMarshaler {
47     public static final String JavaDoc WSDL_WRAPPER_NAMESPACE = "http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper";
48
49     private SourceTransformer transformer = new SourceTransformer();
50
51     public void toNMS(final MessageExchange exchange, final NormalizedMessage nmsMessage, final WSIFOperationInfo operationInfo, final WSIFMessage wsifMessage) throws WSIFException, MessagingException {
52         addNmsProperties(nmsMessage, wsifMessage);
53         for (Iterator JavaDoc iter = wsifMessage.getPartNames(); iter.hasNext();) {
54             String JavaDoc name = (String JavaDoc) iter.next();
55             Object JavaDoc value = wsifMessage.getObjectPart(name);
56             nmsMessage.setProperty(name, value);
57         }
58         nmsMessage.setContent(new LazyDOMSource() {
59             protected Node JavaDoc loadNode() {
60                 return createResultDocument(exchange, nmsMessage, operationInfo, wsifMessage);
61             }
62         });
63     }
64
65     public void fromNMS(WSIFOperationInfo operationInfo, WSIFMessage wsifMessage, NormalizedMessage nmsMessage, Object JavaDoc body) throws WSIFException, MessagingException {
66         addWSIFProperties(wsifMessage, nmsMessage);
67         try {
68             Element JavaDoc element = transformer.toDOMElement(nmsMessage);
69             Map JavaDoc parts = wsifMessage.getMessageDefinition().getParts();
70             for (Iterator JavaDoc iter = parts.entrySet().iterator(); iter.hasNext();) {
71                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
72                 String JavaDoc name = (String JavaDoc) entry.getKey();
73                 Part part = (Part) entry.getValue();
74
75                 Object JavaDoc value = getPartValue(name, part, nmsMessage, element);
76                 wsifMessage.setObjectPart(name, value);
77             }
78         }
79         catch (TransformerException JavaDoc e) {
80             throw new MessagingException(e);
81         }
82         catch (ParserConfigurationException JavaDoc e) {
83             throw new MessagingException(e);
84         }
85         catch (IOException JavaDoc e) {
86             throw new MessagingException(e);
87         }
88         catch (SAXException JavaDoc e) {
89             throw new MessagingException(e);
90         }
91     }
92
93     // Properties
94
//-------------------------------------------------------------------------
95
public SourceTransformer getTransformer() {
96         return transformer;
97     }
98
99     public void setTransformer(SourceTransformer transformer) {
100         this.transformer = transformer;
101     }
102
103     // Implementation methods
104
//-------------------------------------------------------------------------
105
protected void addNmsProperties(NormalizedMessage nmsMessage, WSIFMessage wsifMessage) throws WSIFException {
106         Iterator JavaDoc iter = wsifMessage.getPartNames();
107         while (iter.hasNext()) {
108             String JavaDoc name = (String JavaDoc) iter.next();
109             Object JavaDoc value = wsifMessage.getObjectPart(name);
110             nmsMessage.setProperty(name, value);
111         }
112     }
113
114     protected void addWSIFProperties(WSIFMessage wsifMessage, NormalizedMessage nmsMessage) throws WSIFException {
115         for (Iterator JavaDoc iter = nmsMessage.getPropertyNames().iterator(); iter.hasNext();) {
116             String JavaDoc name = (String JavaDoc) iter.next();
117             Object JavaDoc value = nmsMessage.getProperty(name);
118             if (shouldIncludeHeader(nmsMessage, name, value)) {
119                 wsifMessage.setObjectPart(name, value);
120             }
121         }
122     }
123
124
125     /**
126      * Decides whether or not the given header should be included in the WSIF message.
127      * By default this includes all values
128      */

129     protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String JavaDoc name, Object JavaDoc value) {
130         return true;
131     }
132
133     protected Object JavaDoc getPartValue(String JavaDoc name, Part part, NormalizedMessage nmsMessage, Element JavaDoc body) throws MessagingException, TransformerException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
134         if (isSimpleType(part)) {
135             // lets extract the text content
136
return DOMUtil.getElementText(body).trim();
137         }
138         // TODO we should be extracting each part of the XML using XPath
139
return transformer.toDOMNode(nmsMessage);
140
141     }
142
143     /**
144      * Returns true if the given part is a string type
145      */

146     protected boolean isSimpleType(Part part) {
147        QName JavaDoc typeName = part.getTypeName();
148        if (typeName != null) {
149           return "http://www.w3.org/2001/XMLSchema".equals(typeName.getNamespaceURI());
150        }
151        return false;
152     }
153
154     protected Node JavaDoc createResultDocument(MessageExchange exchange, NormalizedMessage normalizedMessage, WSIFOperationInfo operationInfo, WSIFMessage wsifMessage) {
155         try {
156             Document JavaDoc document = transformer.createDocument();
157             Element JavaDoc root = document.createElementNS(WSDL_WRAPPER_NAMESPACE, "jbi:message");
158             document.appendChild(root);
159             root.setAttribute("xmlns:jbi", WSDL_WRAPPER_NAMESPACE);
160             QName JavaDoc operation = exchange.getOperation();
161             String JavaDoc operationName = "unknown";
162             if (operation != null) {
163                 operationName = operation.getLocalPart();
164                 String JavaDoc uri = operation.getNamespaceURI();
165                 if (uri != null && uri.length() > 0) {
166                     root.setAttribute("xmlns:op", uri);
167                 }
168             }
169             root.setAttribute("version", "1.0");
170             root.setAttribute("type", "op:" + operationName);
171             root.setAttribute("name", operationName);
172
173
174             for (Iterator JavaDoc iter = wsifMessage.getPartNames(); iter.hasNext();) {
175                 String JavaDoc name = (String JavaDoc) iter.next();
176                 Object JavaDoc value = normalizedMessage.getProperty(name);
177
178                 Element JavaDoc element = document.createElementNS(WSDL_WRAPPER_NAMESPACE, "jbi:part");
179                 root.appendChild(element);
180                 addPartValue(element, name, value);
181             }
182
183             return document;
184         }
185         catch (Exception JavaDoc e) {
186             throw new FailedToCreateDOMException(e);
187         }
188     }
189
190     protected void addPartValue(Element JavaDoc element, String JavaDoc name, Object JavaDoc value) {
191         if (value instanceof Document JavaDoc) {
192             Document JavaDoc doc = (Document JavaDoc) value;
193             Element JavaDoc root = doc.getDocumentElement();
194             doc.removeChild(root);
195             element.appendChild(root);
196         }
197         else if (value != null) {
198             String JavaDoc text = value.toString();
199             element.appendChild(element.getOwnerDocument().createTextNode(text));
200         }
201     }
202
203
204 }
205
Popular Tags