1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.xmpp; |
18 |
|
|
19 |
|
import org.apache.camel.Exchange; |
20 |
|
import org.apache.camel.impl.DefaultProducer; |
21 |
|
import org.apache.commons.logging.Log; |
22 |
|
import org.apache.commons.logging.LogFactory; |
23 |
|
import org.jivesoftware.smack.Chat; |
24 |
|
import org.jivesoftware.smack.XMPPException; |
25 |
|
import org.jivesoftware.smack.packet.Message; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
public class XmppPrivateChatProducer extends DefaultProducer { |
31 |
0 |
private static final transient Log LOG = LogFactory.getLog(XmppPrivateChatProducer.class); |
32 |
|
private final XmppEndpoint endpoint; |
33 |
|
private final String participant; |
34 |
|
private Chat chat; |
35 |
|
|
36 |
|
public XmppPrivateChatProducer(XmppEndpoint endpoint, String participant) { |
37 |
0 |
super(endpoint); |
38 |
0 |
this.endpoint = endpoint; |
39 |
0 |
this.participant = participant; |
40 |
0 |
if (participant == null) { |
41 |
0 |
throw new IllegalArgumentException("No participant property specified"); |
42 |
|
} |
43 |
0 |
} |
44 |
|
|
45 |
|
public void process(Exchange exchange) { |
46 |
|
|
47 |
0 |
Message message = chat.createMessage(); |
48 |
0 |
message.setTo(participant); |
49 |
0 |
message.setFrom(endpoint.getUser()); |
50 |
0 |
message.setThread(exchange.getExchangeId()); |
51 |
0 |
message.setType(Message.Type.NORMAL); |
52 |
|
|
53 |
0 |
endpoint.getBinding().populateXmppMessage(message, exchange); |
54 |
0 |
if (LOG.isDebugEnabled()) { |
55 |
0 |
LOG.debug(">>>> message: " + message.getBody()); |
56 |
|
} |
57 |
|
try { |
58 |
0 |
chat.sendMessage(message); |
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().createChat(getParticipant()); |
69 |
|
} |
70 |
0 |
} |
71 |
|
|
72 |
|
@Override |
73 |
|
protected void doStop() throws Exception { |
74 |
0 |
chat = null; |
75 |
0 |
super.doStop(); |
76 |
0 |
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
public Chat getChat() { |
81 |
0 |
return chat; |
82 |
|
} |
83 |
|
|
84 |
|
public void setChat(Chat chat) { |
85 |
0 |
this.chat = chat; |
86 |
0 |
} |
87 |
|
|
88 |
|
public String getParticipant() { |
89 |
0 |
return participant; |
90 |
|
} |
91 |
|
} |