KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jbi > JbiUtils


1 /*
2  * $Id: JbiUtils.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 javax.jbi.messaging.MessagingException;
18 import javax.jbi.messaging.NormalizedMessage;
19 import javax.xml.transform.Source JavaDoc;
20 import javax.xml.transform.TransformerFactory JavaDoc;
21 import javax.xml.transform.stream.StreamResult JavaDoc;
22 import javax.xml.transform.stream.StreamSource JavaDoc;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import org.apache.commons.io.output.ByteArrayOutputStream;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Useful for converting message types
32  *
33  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
34  * @version $Revision: 3798 $
35  */

36 public class JbiUtils
37 {
38
39     public static UMOMessage createMessage(NormalizedMessage message) throws MessagingException
40     {
41
42         Map JavaDoc properties = new HashMap JavaDoc();
43         for (Iterator JavaDoc iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
44         {
45             String JavaDoc s = (String JavaDoc)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             // TODO source transformer
55
Source JavaDoc source = message.getContent();
56             ByteArrayOutputStream baos = new ByteArrayOutputStream();
57             StreamResult JavaDoc result = new StreamResult JavaDoc(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 JavaDoc 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 JavaDoc(new ByteArrayInputStream JavaDoc(muleMessage.getPayloadAsBytes())));
75         }
76         catch (Exception JavaDoc e)
77         {
78             throw new MessagingException(e.getMessage(), e);
79         }
80
81         for (Iterator JavaDoc iterator = muleMessage.getPropertyNames().iterator(); iterator.hasNext();)
82         {
83             String JavaDoc s = (String JavaDoc)iterator.next();
84             message.setProperty(s, muleMessage.getProperty(s));
85         }
86
87         for (Iterator JavaDoc iterator = muleMessage.getAttachmentNames().iterator(); iterator.hasNext();)
88         {
89             String JavaDoc s = (String JavaDoc)iterator.next();
90             message.addAttachment(s, muleMessage.getAttachment(s));
91         }
92     }
93 }
94
Popular Tags