1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.xmpp;
12
13 import org.mule.api.MessagingException;
14 import org.mule.api.ThreadSafeAccess;
15 import org.mule.api.transport.MessageTypeNotSupportedException;
16 import org.mule.transport.AbstractMessageAdapter;
17 import org.mule.util.StringUtils;
18
19 import java.util.Iterator;
20
21 import org.jivesoftware.smack.packet.Message;
22 import org.jivesoftware.smack.packet.Packet;
23
24
25
26
27 public class XmppMessageAdapter extends AbstractMessageAdapter
28 {
29 public static final String DEFAULT_SUBJECT = "(no subject)";
30 public static final String DEFAULT_THREAD = "(no thread)";
31
32
33
34
35 private static final long serialVersionUID = -4003299444661664762L;
36
37 private final Packet message;
38
39 public XmppMessageAdapter(Object message) throws MessagingException
40 {
41 if (message instanceof Packet)
42 {
43 this.message = (Packet)message;
44
45 for (Iterator iter = this.message.getPropertyNames(); iter.hasNext();)
46 {
47 String name = (String)iter.next();
48 this.setProperty(name, this.message.getProperty(name));
49 }
50
51 if (this.message instanceof Message)
52 {
53 this.setProperty("subject", StringUtils.defaultIfEmpty(((Message)this.message).getSubject(),
54 DEFAULT_SUBJECT));
55 this.setProperty("thread", StringUtils.defaultIfEmpty(((Message)this.message).getThread(),
56 DEFAULT_THREAD));
57 }
58 }
59 else
60 {
61 throw new MessageTypeNotSupportedException(message, getClass());
62 }
63 }
64
65 protected XmppMessageAdapter(XmppMessageAdapter template)
66 {
67 super(template);
68 message = template.message;
69 }
70
71
72
73
74
75
76
77
78
79 public String getPayloadAsString(String encoding) throws Exception
80 {
81 if (message instanceof Message)
82 {
83 return ((Message)message).getBody();
84 }
85 else
86 {
87 return message.toString();
88 }
89 }
90
91 public byte[] getPayloadAsBytes() throws Exception
92 {
93 if (message instanceof Message)
94 {
95 return ((Message)message).getBody().getBytes();
96 }
97 else
98 {
99 return message.toString().getBytes();
100 }
101 }
102
103 public Object getPayload()
104 {
105 return message;
106 }
107
108
109 public String getUniqueId()
110 {
111 return message.getPacketID();
112 }
113
114 public ThreadSafeAccess newThreadCopy()
115 {
116 return new XmppMessageAdapter(this);
117 }
118
119 }