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