1 17 package org.apache.servicemix.components.saaj; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 23 import javax.activation.DataHandler ; 24 import javax.jbi.messaging.MessagingException; 25 import javax.jbi.messaging.NormalizedMessage; 26 import javax.xml.XMLConstants ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.soap.AttachmentPart ; 29 import javax.xml.soap.MessageFactory ; 30 import javax.xml.soap.MimeHeader ; 31 import javax.xml.soap.SOAPBody ; 32 import javax.xml.soap.SOAPElement ; 33 import javax.xml.soap.SOAPEnvelope ; 34 import javax.xml.soap.SOAPException ; 35 import javax.xml.soap.SOAPMessage ; 36 import javax.xml.soap.SOAPPart ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.dom.DOMSource ; 39 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 43 import org.w3c.dom.Attr ; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.NamedNodeMap ; 46 import org.xml.sax.SAXException ; 47 48 51 public class SaajMarshaler { 52 53 private static final transient Log log = LogFactory.getLog(SaajMarshaler.class); 54 55 protected SourceTransformer transformer = new SourceTransformer(); 56 private MessageFactory messageFactory; 57 58 public void toNMS(NormalizedMessage normalizedMessage, SOAPMessage soapMessage) throws MessagingException, SOAPException { 59 60 if (log.isDebugEnabled()) { 61 try { 62 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 63 soapMessage.writeTo(buffer); 64 log.debug(new String (buffer.toByteArray())); 65 } catch (Exception e) { } 66 } 67 68 addNmsProperties(normalizedMessage, soapMessage); 69 70 SOAPPart soapPart = soapMessage.getSOAPPart(); 71 SOAPBody soapBody = soapPart.getEnvelope().getBody(); 72 SOAPElement elem = null; 73 for (Iterator it = soapBody.getChildElements(); it.hasNext();) { 74 Object child = it.next(); 75 if (child instanceof SOAPElement ) { 76 elem = (SOAPElement ) child; 77 break; 78 } 79 } 80 if (elem == null) { 81 throw new IllegalStateException ("Could not find any element in soap body"); 82 } 83 84 for (SOAPElement parent = elem.getParentElement(); parent != null; parent = parent.getParentElement()) { 85 NamedNodeMap attributes = parent.getAttributes(); 87 if (attributes != null) { 88 for (int i = 0; i < attributes.getLength(); i++) { 89 Attr att = (Attr ) parent.getAttributes().item(i); 90 if (att.getName().startsWith(XMLConstants.XMLNS_ATTRIBUTE + ":") 91 && elem.getAttributeNodeNS(att.getNamespaceURI(), att.getLocalName()) == null) { 92 elem.addNamespaceDeclaration(att.getName().substring(XMLConstants.XMLNS_ATTRIBUTE.length() + 1), att.getValue()); 93 elem.setAttributeNS(att.getNamespaceURI(), att.getName(), att.getValue()); 94 } 95 } 96 } 97 for (Iterator itNs = parent.getNamespacePrefixes(); itNs.hasNext();) { 99 String prefix = (String ) itNs.next(); 100 String nsuri = parent.getNamespaceURI(prefix); 101 if (elem.getAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, prefix) == null) { 102 elem.addNamespaceDeclaration(prefix, nsuri); 103 elem.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix, nsuri); 104 } 105 } 106 } 107 108 if (log.isDebugEnabled()) { 109 try { 110 log.debug(transformer.toString(elem)); 111 } catch (Exception e) { } 112 } 113 114 normalizedMessage.setContent(new DOMSource (elem)); 115 116 addNmsAttachments(normalizedMessage, soapMessage); 117 } 118 119 public SOAPMessage createSOAPMessage(NormalizedMessage normalizedMessage) throws SOAPException , IOException , TransformerException , MessagingException, ParserConfigurationException , SAXException { 120 SOAPMessage soapMessage = getMessageFactory().createMessage(); 121 122 addSoapProperties(soapMessage, normalizedMessage); 123 124 SOAPPart soapPart = soapMessage.getSOAPPart(); 125 SOAPEnvelope envelope = soapPart.getEnvelope(); 126 SOAPBody body = envelope.getBody(); 127 128 Document document = transformer.toDOMDocument(normalizedMessage); 134 body.addDocument(document); 135 136 addSoapAttachments(soapMessage, normalizedMessage); 137 138 return soapMessage; 139 } 140 141 public MessageFactory getMessageFactory() throws SOAPException { 144 if (messageFactory == null) { 145 messageFactory = createMessageFactory(); 146 } 147 return messageFactory; 148 } 149 150 public void setMessageFactory(MessageFactory messageFactory) { 151 this.messageFactory = messageFactory; 152 } 153 154 157 protected void addNmsProperties(NormalizedMessage normalizedMessage, SOAPMessage soapMessage) { 158 Iterator iter = soapMessage.getMimeHeaders().getAllHeaders(); 159 while (iter.hasNext()) { 160 MimeHeader header = (MimeHeader ) iter.next(); 161 normalizedMessage.setProperty(header.getName(), header.getValue()); 162 } 163 } 164 165 protected void addNmsAttachments(NormalizedMessage normalizedMessage, SOAPMessage soapMessage) throws MessagingException, SOAPException { 166 Iterator iter = soapMessage.getAttachments(); 167 while (iter.hasNext()) { 168 AttachmentPart attachment = (AttachmentPart ) iter.next(); 169 normalizedMessage.addAttachment(attachment.getContentId(), asDataHandler(attachment)); 170 } 171 } 172 173 protected void addSoapProperties(SOAPMessage soapMessage, NormalizedMessage normalizedMessage) throws SOAPException { 174 for (Iterator iter = normalizedMessage.getPropertyNames().iterator(); iter.hasNext();) { 175 String name = (String ) iter.next(); 176 Object value = normalizedMessage.getProperty(name); 177 if (shouldIncludeHeader(normalizedMessage, name, value)) { 178 soapMessage.getMimeHeaders().addHeader(name, value.toString()); 179 } 180 if (shouldIncludeProperty(normalizedMessage, name, value)) { 181 soapMessage.setProperty(name, value); 182 } 183 } 184 } 185 186 protected void addSoapAttachments(SOAPMessage soapMessage, NormalizedMessage normalizedMessage) throws IOException { 187 Iterator iterator = normalizedMessage.getAttachmentNames().iterator(); 188 while (iterator.hasNext()) { 189 String name = (String ) iterator.next(); 190 DataHandler attachment = normalizedMessage.getAttachment(name); 191 AttachmentPart attachmentPart = soapMessage.createAttachmentPart(attachment.getContent(), attachment.getContentType()); 192 attachmentPart.setContentId(name); 193 soapMessage.addAttachmentPart(attachmentPart); 194 } 195 } 196 197 200 protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String name, Object value) { 201 return true; 203 } 204 205 208 protected boolean shouldIncludeProperty(NormalizedMessage normalizedMessage, String name, Object value) { 209 return true; 210 } 211 212 protected DataHandler asDataHandler(AttachmentPart attachment) throws SOAPException { 213 return new DataHandler (attachment.getContent(), attachment.getContentType()); 214 } 215 216 217 protected MessageFactory createMessageFactory() throws SOAPException { 218 return MessageFactory.newInstance(); 219 } 220 } 221 | Popular Tags |