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