1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.xmpp; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.endpoint.EndpointURI; |
17 | |
import org.mule.api.endpoint.InboundEndpoint; |
18 | |
import org.mule.api.endpoint.MalformedEndpointException; |
19 | |
import org.mule.transport.AbstractMessageRequester; |
20 | |
|
21 | |
import org.jivesoftware.smack.Chat; |
22 | |
import org.jivesoftware.smack.XMPPConnection; |
23 | |
import org.jivesoftware.smack.packet.Message; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class XmppMessageRequester extends AbstractMessageRequester |
30 | |
{ |
31 | |
|
32 | |
private final XmppConnector connector; |
33 | 0 | private volatile XMPPConnection xmppConnection = null; |
34 | |
|
35 | |
public XmppMessageRequester(InboundEndpoint endpoint) |
36 | |
{ |
37 | 0 | super(endpoint); |
38 | 0 | this.connector = (XmppConnector)endpoint.getConnector(); |
39 | 0 | } |
40 | |
|
41 | |
protected void doConnect() throws Exception |
42 | |
{ |
43 | 0 | if (xmppConnection == null) |
44 | |
{ |
45 | 0 | EndpointURI uri = endpoint.getEndpointURI(); |
46 | 0 | xmppConnection = connector.createXmppConnection(uri); |
47 | |
} |
48 | 0 | } |
49 | |
|
50 | |
protected void doDisconnect() throws Exception |
51 | |
{ |
52 | |
try |
53 | |
{ |
54 | 0 | if (xmppConnection != null) |
55 | |
{ |
56 | 0 | xmppConnection.close(); |
57 | |
} |
58 | |
} |
59 | |
finally |
60 | |
{ |
61 | 0 | xmppConnection = null; |
62 | 0 | } |
63 | 0 | } |
64 | |
|
65 | |
protected void doDispose() |
66 | |
{ |
67 | |
|
68 | 0 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
protected MuleMessage doRequest(long timeout) throws Exception |
82 | |
{ |
83 | |
|
84 | 0 | String to = (String)endpoint.getProperty("folder"); |
85 | 0 | if (to == null) |
86 | |
{ |
87 | 0 | throw new MalformedEndpointException(endpoint.getEndpointURI().toString()); |
88 | |
} |
89 | 0 | Chat chat = xmppConnection.createChat(to); |
90 | 0 | Message message = null; |
91 | 0 | if (timeout == MuleEvent.TIMEOUT_WAIT_FOREVER) |
92 | |
{ |
93 | 0 | message = chat.nextMessage(); |
94 | |
} |
95 | 0 | else if (timeout == MuleEvent.TIMEOUT_DO_NOT_WAIT) |
96 | |
{ |
97 | 0 | message = chat.nextMessage(1); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 0 | message = chat.nextMessage(timeout); |
102 | |
} |
103 | 0 | if (message != null) |
104 | |
{ |
105 | 0 | return new DefaultMuleMessage(connector.getMessageAdapter(message)); |
106 | |
} |
107 | |
else |
108 | |
{ |
109 | 0 | return null; |
110 | |
} |
111 | |
} |
112 | |
|
113 | |
} |