1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.component.xmpp; |
19 |
|
|
20 |
|
import org.apache.camel.Exchange; |
21 |
|
import org.apache.camel.impl.DefaultProducer; |
22 |
|
import org.apache.commons.logging.Log; |
23 |
|
import org.apache.commons.logging.LogFactory; |
24 |
|
import org.jivesoftware.smack.GroupChat; |
25 |
|
import org.jivesoftware.smack.XMPPException; |
26 |
|
import org.jivesoftware.smack.packet.Message; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
public class XmppGroupChatProducer extends DefaultProducer { |
32 |
0 |
private static final transient Log log = LogFactory.getLog(XmppGroupChatProducer.class); |
33 |
|
private final XmppEndpoint endpoint; |
34 |
|
private final String room; |
35 |
|
private GroupChat chat; |
36 |
|
|
37 |
|
public XmppGroupChatProducer(XmppEndpoint endpoint, String room) { |
38 |
0 |
super(endpoint); |
39 |
0 |
this.endpoint = endpoint; |
40 |
0 |
this.room = room; |
41 |
0 |
if (room == null) { |
42 |
0 |
throw new IllegalArgumentException("No room property specified"); |
43 |
|
} |
44 |
0 |
} |
45 |
|
|
46 |
|
public void process(Exchange exchange) { |
47 |
|
|
48 |
0 |
Message message = chat.createMessage(); |
49 |
0 |
message.setTo(room); |
50 |
0 |
message.setFrom(endpoint.getUser()); |
51 |
|
|
52 |
0 |
endpoint.getBinding().populateXmppMessage(message, exchange); |
53 |
0 |
if (log.isDebugEnabled()) { |
54 |
0 |
log.debug(">>>> message: " + message.getBody()); |
55 |
|
} |
56 |
|
try { |
57 |
0 |
chat.sendMessage(message); |
58 |
|
} |
59 |
0 |
catch (XMPPException e) { |
60 |
0 |
throw new RuntimeXmppException(e); |
61 |
0 |
} |
62 |
0 |
} |
63 |
|
|
64 |
|
@Override |
65 |
|
protected void doStart() throws Exception { |
66 |
0 |
super.doStart(); |
67 |
0 |
if (chat == null) { |
68 |
0 |
chat = endpoint.getConnection().createGroupChat(room); |
69 |
|
} |
70 |
0 |
} |
71 |
|
|
72 |
|
@Override |
73 |
|
protected void doStop() throws Exception { |
74 |
0 |
if (chat != null) { |
75 |
0 |
chat.leave(); |
76 |
0 |
chat = null; |
77 |
|
} |
78 |
0 |
super.doStop(); |
79 |
0 |
} |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
public GroupChat getChat() { |
84 |
0 |
return chat; |
85 |
|
} |
86 |
|
|
87 |
|
public void setChat(GroupChat chat) { |
88 |
0 |
this.chat = chat; |
89 |
0 |
} |
90 |
|
|
91 |
|
public String getRoom() { |
92 |
0 |
return room; |
93 |
|
} |
94 |
|
} |