1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.xmpp; |
12 | |
|
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.construct.FlowConstruct; |
17 | |
import org.mule.api.endpoint.InboundEndpoint; |
18 | |
import org.mule.api.lifecycle.CreateException; |
19 | |
import org.mule.transport.AbstractConnector; |
20 | |
import org.mule.transport.AbstractMessageReceiver; |
21 | |
|
22 | |
import javax.resource.spi.work.Work; |
23 | |
import javax.resource.spi.work.WorkException; |
24 | |
import javax.resource.spi.work.WorkManager; |
25 | |
|
26 | |
import org.jivesoftware.smack.PacketListener; |
27 | |
import org.jivesoftware.smack.packet.Message; |
28 | |
import org.jivesoftware.smack.packet.Packet; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class XmppMessageReceiver extends AbstractMessageReceiver implements PacketListener |
34 | |
{ |
35 | |
|
36 | |
private XmppConversation xmppConversation; |
37 | |
|
38 | |
public XmppMessageReceiver(AbstractConnector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint) |
39 | |
throws CreateException |
40 | |
{ |
41 | 0 | super(connector, flowConstruct, endpoint); |
42 | 0 | XmppConnector xmppConnector = (XmppConnector) connector; |
43 | 0 | xmppConversation = xmppConnector.getConversationFactory().create(endpoint); |
44 | 0 | } |
45 | |
|
46 | |
@Override |
47 | |
protected void doConnect() throws Exception |
48 | |
{ |
49 | 0 | xmppConversation.connect(); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 0 | } |
69 | |
|
70 | |
@Override |
71 | |
protected void doDisconnect() throws Exception |
72 | |
{ |
73 | 0 | xmppConversation.disconnect(); |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 0 | } |
80 | |
|
81 | |
|
82 | |
@Override |
83 | |
protected void doStart() throws MuleException |
84 | |
{ |
85 | |
|
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
@Override |
90 | |
protected void doStop() throws MuleException |
91 | |
{ |
92 | |
|
93 | 0 | } |
94 | |
|
95 | |
@Override |
96 | |
protected void doDispose() |
97 | |
{ |
98 | 0 | xmppConversation = null; |
99 | 0 | } |
100 | |
|
101 | |
protected Work createWork(Packet message) |
102 | |
{ |
103 | 0 | return new XMPPWorker(message); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
public void processPacket(Packet packet) |
108 | |
{ |
109 | 0 | if (logger.isDebugEnabled()) |
110 | |
{ |
111 | 0 | logger.debug("processing packet: " + packet.toXML()); |
112 | |
} |
113 | |
|
114 | 0 | Work work = createWork(packet); |
115 | |
try |
116 | |
{ |
117 | 0 | getWorkManager().scheduleWork(work, WorkManager.INDEFINITE, null, connector); |
118 | |
} |
119 | 0 | catch (WorkException e) |
120 | |
{ |
121 | 0 | logger.error("Xmpp Server receiver work failed: " + e.getMessage(), e); |
122 | 0 | } |
123 | 0 | } |
124 | |
|
125 | |
private class XMPPWorker implements Work |
126 | |
{ |
127 | 0 | Packet packet = null; |
128 | |
|
129 | |
public XMPPWorker(Packet message) |
130 | 0 | { |
131 | 0 | this.packet = message; |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
public void run() |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 0 | if (logger.isDebugEnabled()) |
140 | |
{ |
141 | 0 | logger.debug("Processing XMPP packet from: " + packet.getFrom()); |
142 | |
} |
143 | |
|
144 | 0 | MuleMessage message = createMuleMessage(packet, endpoint.getEncoding()); |
145 | 0 | MuleEvent event = routeMessage(message); |
146 | 0 | MuleMessage returnMessage = event == null ? null : event.getMessage(); |
147 | |
|
148 | 0 | if (returnMessage != null && packet instanceof Message) |
149 | |
{ |
150 | 0 | returnMessage.applyTransformers(event, connector.getDefaultResponseTransformers(endpoint)); |
151 | 0 | Packet result = (Packet) returnMessage.getPayload(); |
152 | |
|
153 | |
} |
154 | |
} |
155 | 0 | catch (Exception e) |
156 | |
{ |
157 | 0 | getConnector().getMuleContext().getExceptionListener().handleException(e); |
158 | 0 | } |
159 | 0 | } |
160 | |
|
161 | |
public void release() |
162 | |
{ |
163 | |
|
164 | 0 | } |
165 | |
} |
166 | |
} |