1 10 11 package org.mule.providers.jbi; 12 13 import org.mule.config.MuleProperties; 14 import org.mule.impl.MuleMessage; 15 import org.mule.umo.UMOMessage; 16 17 import javax.jbi.messaging.MessagingException; 18 import javax.jbi.messaging.NormalizedMessage; 19 import javax.xml.transform.Source ; 20 import javax.xml.transform.TransformerFactory ; 21 import javax.xml.transform.stream.StreamResult ; 22 import javax.xml.transform.stream.StreamSource ; 23 24 import java.io.ByteArrayInputStream ; 25 import org.apache.commons.io.output.ByteArrayOutputStream; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 36 public class JbiUtils 37 { 38 39 public static UMOMessage createMessage(NormalizedMessage message) throws MessagingException 40 { 41 42 Map properties = new HashMap (); 43 for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();) 44 { 45 String s = (String )iterator.next(); 46 properties.put(s, message.getProperty(s)); 47 } 48 if (message.getSecuritySubject() != null) 49 { 50 properties.put(MuleProperties.MULE_USER_PROPERTY, message.getSecuritySubject()); 51 } 52 try 53 { 54 Source source = message.getContent(); 56 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 57 StreamResult result = new StreamResult (baos); 58 TransformerFactory.newInstance().newTransformer().transform(source, result); 59 UMOMessage msg = new MuleMessage(baos.toByteArray(), properties); 60 baos.close(); 61 return msg; 62 } 63 catch (Exception e) 64 { 65 throw new MessagingException(e.getMessage(), e); 66 } 67 } 68 69 public static void populateNormalizedMessage(UMOMessage muleMessage, NormalizedMessage message) 70 throws MessagingException 71 { 72 try 73 { 74 message.setContent(new StreamSource (new ByteArrayInputStream (muleMessage.getPayloadAsBytes()))); 75 } 76 catch (Exception e) 77 { 78 throw new MessagingException(e.getMessage(), e); 79 } 80 81 for (Iterator iterator = muleMessage.getPropertyNames().iterator(); iterator.hasNext();) 82 { 83 String s = (String )iterator.next(); 84 message.setProperty(s, muleMessage.getProperty(s)); 85 } 86 87 for (Iterator iterator = muleMessage.getAttachmentNames().iterator(); iterator.hasNext();) 88 { 89 String s = (String )iterator.next(); 90 message.addAttachment(s, muleMessage.getAttachment(s)); 91 } 92 } 93 } 94 | Popular Tags |