1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.xmpp; |
8 | |
|
9 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
10 | |
import org.mule.api.transport.Connector; |
11 | |
import org.mule.transport.ConnectException; |
12 | |
import org.mule.util.UUID; |
13 | |
|
14 | |
import org.jivesoftware.smack.PacketCollector; |
15 | |
import org.jivesoftware.smack.SmackConfiguration; |
16 | |
import org.jivesoftware.smack.XMPPException; |
17 | |
import org.jivesoftware.smack.packet.Message; |
18 | |
import org.jivesoftware.smack.packet.XMPPError; |
19 | |
import org.jivesoftware.smackx.muc.DiscussionHistory; |
20 | |
import org.jivesoftware.smackx.muc.MultiUserChat; |
21 | |
|
22 | |
public class XmppMultiUserChatConversation extends AbstractXmppConversation |
23 | |
{ |
24 | |
private MultiUserChat chat; |
25 | |
private String nickname; |
26 | |
private final Connector connector; |
27 | |
|
28 | |
public XmppMultiUserChatConversation(ImmutableEndpoint endpoint) |
29 | |
{ |
30 | 0 | super(endpoint); |
31 | 0 | connector = endpoint.getConnector(); |
32 | |
|
33 | 0 | Object nickValue = endpoint.getProperty(XmppConnector.XMPP_NICKNAME); |
34 | 0 | if (nickValue != null) |
35 | |
{ |
36 | 0 | nickname = nickValue.toString(); |
37 | |
} |
38 | |
else |
39 | |
{ |
40 | 0 | nickname = UUID.getUUID().toString(); |
41 | |
} |
42 | 0 | } |
43 | |
|
44 | |
@Override |
45 | |
protected void doConnect() throws ConnectException |
46 | |
{ |
47 | 0 | chat = new MultiUserChat(connection, recipient); |
48 | 0 | joinChat(); |
49 | 0 | } |
50 | |
|
51 | |
protected void joinChat() throws ConnectException |
52 | |
{ |
53 | |
try |
54 | |
{ |
55 | 0 | tryToJoinChat(); |
56 | |
} |
57 | 0 | catch (XMPPException e) |
58 | |
{ |
59 | 0 | if (roomDoesNotExist(e)) |
60 | |
{ |
61 | 0 | createRoom(); |
62 | |
} |
63 | |
else |
64 | |
{ |
65 | 0 | throw new ConnectException(e, connector); |
66 | |
} |
67 | 0 | } |
68 | 0 | } |
69 | |
|
70 | |
protected void tryToJoinChat() throws XMPPException |
71 | |
{ |
72 | 0 | DiscussionHistory history = new DiscussionHistory(); |
73 | 0 | history.setMaxStanzas(0); |
74 | |
|
75 | |
|
76 | 0 | long joinTimeout = SmackConfiguration.getPacketReplyTimeout(); |
77 | |
|
78 | 0 | chat.join(nickname, null, history, joinTimeout); |
79 | 0 | if (logger.isDebugEnabled()) |
80 | |
{ |
81 | 0 | logger.debug("joined groupchat '" + recipient + "'"); |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
protected boolean roomDoesNotExist(XMPPException exception) |
86 | |
{ |
87 | 0 | XMPPError error = exception.getXMPPError(); |
88 | 0 | if ((error.getCode() == 404) && |
89 | |
error.getCondition().equals(XMPPError.Condition.recipient_unavailable.toString())) |
90 | |
{ |
91 | 0 | return true; |
92 | |
} |
93 | 0 | return false; |
94 | |
} |
95 | |
|
96 | |
protected void createRoom() throws ConnectException |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 0 | chat.create(nickname); |
101 | 0 | if (logger.isDebugEnabled()) |
102 | |
{ |
103 | 0 | logger.debug("created and joined groupchat '" + recipient + "'"); |
104 | |
} |
105 | |
} |
106 | 0 | catch (XMPPException e) |
107 | |
{ |
108 | 0 | throw new ConnectException(e, connector); |
109 | 0 | } |
110 | 0 | } |
111 | |
|
112 | |
@Override |
113 | |
protected void doDisconnect() |
114 | |
{ |
115 | 0 | chat.leave(); |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
@Override |
123 | |
protected PacketCollector createPacketCollector() |
124 | |
{ |
125 | 0 | return null; |
126 | |
} |
127 | |
|
128 | |
public void dispatch(Message message) throws XMPPException |
129 | |
{ |
130 | 0 | message.setType(Message.Type.groupchat); |
131 | 0 | message.setTo(recipient); |
132 | |
|
133 | 0 | chat.sendMessage(message); |
134 | 0 | } |
135 | |
|
136 | |
@Override |
137 | |
public Message receive() |
138 | |
{ |
139 | 0 | return chat.nextMessage(); |
140 | |
} |
141 | |
|
142 | |
@Override |
143 | |
public Message receive(long timeout) |
144 | |
{ |
145 | 0 | return chat.nextMessage(timeout); |
146 | |
} |
147 | |
} |