Coverage Report - org.mule.providers.jbi.JbiUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
JbiUtils
0%
0/30
0%
0/8
5.5
 
 1  
 /*
 2  
  * $Id: JbiUtils.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 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 java.io.ByteArrayInputStream;
 18  
 import java.util.HashMap;
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.jbi.messaging.MessagingException;
 23  
 import javax.jbi.messaging.NormalizedMessage;
 24  
 import javax.xml.transform.Source;
 25  
 import javax.xml.transform.TransformerFactory;
 26  
 import javax.xml.transform.stream.StreamResult;
 27  
 import javax.xml.transform.stream.StreamSource;
 28  
 
 29  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 30  
 
 31  
 /**
 32  
  * Useful helpers for converting message types
 33  
  */
 34  0
 public class JbiUtils
 35  
 {
 36  
 
 37  
     public static UMOMessage createMessage(NormalizedMessage message) throws MessagingException
 38  
     {
 39  0
         Map properties = new HashMap();
 40  0
         for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
 41  
         {
 42  0
             String s = (String)iterator.next();
 43  0
             properties.put(s, message.getProperty(s));
 44  0
         }
 45  0
         if (message.getSecuritySubject() != null)
 46  
         {
 47  0
             properties.put(MuleProperties.MULE_USER_PROPERTY, message.getSecuritySubject());
 48  
         }
 49  
         try
 50  
         {
 51  
             // TODO source transformer
 52  0
             Source source = message.getContent();
 53  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 54  0
             StreamResult result = new StreamResult(baos);
 55  0
             TransformerFactory.newInstance().newTransformer().transform(source, result);
 56  0
             UMOMessage msg = new MuleMessage(baos.toByteArray(), properties);
 57  0
             baos.close();
 58  0
             return msg;
 59  
         }
 60  0
         catch (Exception e)
 61  
         {
 62  0
             throw new MessagingException(e.getMessage(), e);
 63  
         }
 64  
     }
 65  
 
 66  
     public static void populateNormalizedMessage(UMOMessage muleMessage, NormalizedMessage message)
 67  
         throws MessagingException
 68  
     {
 69  
         try
 70  
         {
 71  0
             message.setContent(new StreamSource(new ByteArrayInputStream(muleMessage.getPayloadAsBytes())));
 72  
         }
 73  0
         catch (Exception e)
 74  
         {
 75  0
             throw new MessagingException(e.getMessage(), e);
 76  0
         }
 77  
 
 78  0
         for (Iterator iterator = muleMessage.getPropertyNames().iterator(); iterator.hasNext();)
 79  
         {
 80  0
             String s = (String)iterator.next();
 81  0
             message.setProperty(s, muleMessage.getProperty(s));
 82  0
         }
 83  
 
 84  0
         for (Iterator iterator = muleMessage.getAttachmentNames().iterator(); iterator.hasNext();)
 85  
         {
 86  0
             String s = (String)iterator.next();
 87  0
             message.addAttachment(s, muleMessage.getAttachment(s));
 88  0
         }
 89  0
     }
 90  
 }