1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.xmpp; |
12 | |
|
13 | |
import org.mule.impl.ThreadSafeAccess; |
14 | |
import org.mule.providers.AbstractMessageAdapter; |
15 | |
import org.mule.umo.MessagingException; |
16 | |
import org.mule.umo.provider.MessageTypeNotSupportedException; |
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 | 0 | { |
41 | 0 | if (message instanceof Packet) |
42 | |
{ |
43 | 0 | this.message = (Packet)message; |
44 | |
|
45 | 0 | for (Iterator iter = this.message.getPropertyNames(); iter.hasNext();) |
46 | |
{ |
47 | 0 | String name = (String)iter.next(); |
48 | 0 | this.setProperty(name, this.message.getProperty(name)); |
49 | |
} |
50 | |
|
51 | 0 | if (this.message instanceof Message) |
52 | |
{ |
53 | 0 | this.setProperty("subject", StringUtils.defaultIfEmpty(((Message)this.message).getSubject(), |
54 | |
DEFAULT_SUBJECT)); |
55 | 0 | this.setProperty("thread", StringUtils.defaultIfEmpty(((Message)this.message).getThread(), |
56 | |
DEFAULT_THREAD)); |
57 | |
} |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 0 | throw new MessageTypeNotSupportedException(message, getClass()); |
62 | |
} |
63 | 0 | } |
64 | |
|
65 | |
protected XmppMessageAdapter(XmppMessageAdapter template) |
66 | |
{ |
67 | 0 | super(template); |
68 | 0 | message = template.message; |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public String getPayloadAsString(String encoding) throws Exception |
80 | |
{ |
81 | 0 | if (message instanceof Message) |
82 | |
{ |
83 | 0 | return ((Message)message).getBody(); |
84 | |
} |
85 | |
else |
86 | |
{ |
87 | 0 | return message.toString(); |
88 | |
} |
89 | |
} |
90 | |
|
91 | |
public byte[] getPayloadAsBytes() throws Exception |
92 | |
{ |
93 | 0 | if (message instanceof Message) |
94 | |
{ |
95 | 0 | return ((Message)message).getBody().getBytes(); |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | 0 | return message.toString().getBytes(); |
100 | |
} |
101 | |
} |
102 | |
|
103 | |
public Object getPayload() |
104 | |
{ |
105 | 0 | return message; |
106 | |
} |
107 | |
|
108 | |
|
109 | |
public String getUniqueId() |
110 | |
{ |
111 | 0 | return message.getPacketID(); |
112 | |
} |
113 | |
|
114 | |
public ThreadSafeAccess newThreadCopy() |
115 | |
{ |
116 | 0 | return new XmppMessageAdapter(this); |
117 | |
} |
118 | |
|
119 | |
} |