1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
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 | |
} |
45 | 0 | if (message.getSecuritySubject() != null) |
46 | |
{ |
47 | 0 | properties.put(MuleProperties.MULE_USER_PROPERTY, message.getSecuritySubject()); |
48 | |
} |
49 | |
try |
50 | |
{ |
51 | |
|
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 | |
} |
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 | |
} |
89 | 0 | } |
90 | |
} |