1 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 ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Node ; 27 import org.xml.sax.SAXException ; 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 ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 import javax.xml.transform.TransformerException ; 36 37 import java.io.IOException ; 38 import java.util.Iterator ; 39 import java.util.Map ; 40 41 46 public class WSIFMarshaler { 47 public static final String 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 iter = wsifMessage.getPartNames(); iter.hasNext();) { 54 String name = (String ) iter.next(); 55 Object value = wsifMessage.getObjectPart(name); 56 nmsMessage.setProperty(name, value); 57 } 58 nmsMessage.setContent(new LazyDOMSource() { 59 protected Node loadNode() { 60 return createResultDocument(exchange, nmsMessage, operationInfo, wsifMessage); 61 } 62 }); 63 } 64 65 public void fromNMS(WSIFOperationInfo operationInfo, WSIFMessage wsifMessage, NormalizedMessage nmsMessage, Object body) throws WSIFException, MessagingException { 66 addWSIFProperties(wsifMessage, nmsMessage); 67 try { 68 Element element = transformer.toDOMElement(nmsMessage); 69 Map parts = wsifMessage.getMessageDefinition().getParts(); 70 for (Iterator iter = parts.entrySet().iterator(); iter.hasNext();) { 71 Map.Entry entry = (Map.Entry ) iter.next(); 72 String name = (String ) entry.getKey(); 73 Part part = (Part) entry.getValue(); 74 75 Object value = getPartValue(name, part, nmsMessage, element); 76 wsifMessage.setObjectPart(name, value); 77 } 78 } 79 catch (TransformerException e) { 80 throw new MessagingException(e); 81 } 82 catch (ParserConfigurationException e) { 83 throw new MessagingException(e); 84 } 85 catch (IOException e) { 86 throw new MessagingException(e); 87 } 88 catch (SAXException e) { 89 throw new MessagingException(e); 90 } 91 } 92 93 public SourceTransformer getTransformer() { 96 return transformer; 97 } 98 99 public void setTransformer(SourceTransformer transformer) { 100 this.transformer = transformer; 101 } 102 103 protected void addNmsProperties(NormalizedMessage nmsMessage, WSIFMessage wsifMessage) throws WSIFException { 106 Iterator iter = wsifMessage.getPartNames(); 107 while (iter.hasNext()) { 108 String name = (String ) iter.next(); 109 Object 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 iter = nmsMessage.getPropertyNames().iterator(); iter.hasNext();) { 116 String name = (String ) iter.next(); 117 Object value = nmsMessage.getProperty(name); 118 if (shouldIncludeHeader(nmsMessage, name, value)) { 119 wsifMessage.setObjectPart(name, value); 120 } 121 } 122 } 123 124 125 129 protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String name, Object value) { 130 return true; 131 } 132 133 protected Object getPartValue(String name, Part part, NormalizedMessage nmsMessage, Element body) throws MessagingException, TransformerException , ParserConfigurationException , IOException , SAXException { 134 if (isSimpleType(part)) { 135 return DOMUtil.getElementText(body).trim(); 137 } 138 return transformer.toDOMNode(nmsMessage); 140 141 } 142 143 146 protected boolean isSimpleType(Part part) { 147 QName 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 createResultDocument(MessageExchange exchange, NormalizedMessage normalizedMessage, WSIFOperationInfo operationInfo, WSIFMessage wsifMessage) { 155 try { 156 Document document = transformer.createDocument(); 157 Element root = document.createElementNS(WSDL_WRAPPER_NAMESPACE, "jbi:message"); 158 document.appendChild(root); 159 root.setAttribute("xmlns:jbi", WSDL_WRAPPER_NAMESPACE); 160 QName operation = exchange.getOperation(); 161 String operationName = "unknown"; 162 if (operation != null) { 163 operationName = operation.getLocalPart(); 164 String 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 iter = wsifMessage.getPartNames(); iter.hasNext();) { 175 String name = (String ) iter.next(); 176 Object value = normalizedMessage.getProperty(name); 177 178 Element 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 e) { 186 throw new FailedToCreateDOMException(e); 187 } 188 } 189 190 protected void addPartValue(Element element, String name, Object value) { 191 if (value instanceof Document ) { 192 Document doc = (Document ) value; 193 Element root = doc.getDocumentElement(); 194 doc.removeChild(root); 195 element.appendChild(root); 196 } 197 else if (value != null) { 198 String text = value.toString(); 199 element.appendChild(element.getOwnerDocument().createTextNode(text)); 200 } 201 } 202 203 204 } 205 | Popular Tags |