1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.xmpp; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.impl.MuleMessage; |
15 | |
import org.mule.impl.RequestContext; |
16 | |
import org.mule.providers.AbstractConnector; |
17 | |
import org.mule.providers.AbstractMessageReceiver; |
18 | |
import org.mule.providers.ConnectException; |
19 | |
import org.mule.umo.UMOComponent; |
20 | |
import org.mule.umo.UMOException; |
21 | |
import org.mule.umo.UMOMessage; |
22 | |
import org.mule.umo.endpoint.UMOEndpoint; |
23 | |
import org.mule.umo.lifecycle.InitialisationException; |
24 | |
import org.mule.umo.provider.UMOMessageAdapter; |
25 | |
|
26 | |
import javax.resource.spi.work.Work; |
27 | |
import javax.resource.spi.work.WorkException; |
28 | |
import javax.resource.spi.work.WorkManager; |
29 | |
|
30 | |
import org.jivesoftware.smack.PacketListener; |
31 | |
import org.jivesoftware.smack.XMPPConnection; |
32 | |
import org.jivesoftware.smack.XMPPException; |
33 | |
import org.jivesoftware.smack.filter.PacketFilter; |
34 | |
import org.jivesoftware.smack.filter.PacketTypeFilter; |
35 | |
import org.jivesoftware.smack.packet.Message; |
36 | |
import org.jivesoftware.smack.packet.Packet; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class XmppMessageReceiver extends AbstractMessageReceiver implements PacketListener |
42 | |
{ |
43 | 0 | private XMPPConnection xmppConnection = null; |
44 | |
|
45 | |
public XmppMessageReceiver(AbstractConnector connector, UMOComponent component, UMOEndpoint endpoint) |
46 | |
throws InitialisationException |
47 | |
{ |
48 | 0 | super(connector, component, endpoint); |
49 | 0 | } |
50 | |
|
51 | |
protected void doConnect() throws Exception |
52 | |
{ |
53 | |
try |
54 | |
{ |
55 | 0 | XmppConnector cnn = (XmppConnector)connector; |
56 | 0 | xmppConnection = cnn.createXmppConnection(endpoint.getEndpointURI()); |
57 | 0 | if (endpoint.getFilter() instanceof PacketFilter) |
58 | |
{ |
59 | 0 | xmppConnection.addPacketListener(this, (PacketFilter)endpoint.getFilter()); |
60 | |
} |
61 | |
else |
62 | |
{ |
63 | 0 | PacketFilter filter = new PacketTypeFilter(Message.class); |
64 | 0 | xmppConnection.addPacketListener(this, filter); |
65 | |
} |
66 | |
} |
67 | 0 | catch (XMPPException e) |
68 | |
{ |
69 | 0 | throw new ConnectException(CoreMessages.failedToCreate("XMPP Connection"), e, this); |
70 | 0 | } |
71 | 0 | } |
72 | |
|
73 | |
protected void doDisconnect() throws Exception |
74 | |
{ |
75 | 0 | if (xmppConnection != null) |
76 | |
{ |
77 | 0 | xmppConnection.removePacketListener(this); |
78 | 0 | xmppConnection.close(); |
79 | |
} |
80 | 0 | } |
81 | |
|
82 | |
protected void doStart() throws UMOException |
83 | |
{ |
84 | |
|
85 | 0 | } |
86 | |
|
87 | |
protected void doStop() throws UMOException |
88 | |
{ |
89 | |
|
90 | 0 | } |
91 | |
|
92 | |
protected void doDispose() |
93 | |
{ |
94 | |
|
95 | 0 | } |
96 | |
|
97 | |
protected Work createWork(Packet message) |
98 | |
{ |
99 | 0 | return new XMPPWorker(message); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public void processPacket(Packet packet) |
106 | |
{ |
107 | 0 | if (logger.isDebugEnabled()) |
108 | |
{ |
109 | 0 | logger.debug("processing packet: " + packet.toXML()); |
110 | |
} |
111 | |
|
112 | 0 | Work work = createWork(packet); |
113 | |
try |
114 | |
{ |
115 | 0 | getWorkManager().scheduleWork(work, WorkManager.INDEFINITE, null, connector); |
116 | |
} |
117 | 0 | catch (WorkException e) |
118 | |
{ |
119 | 0 | logger.error("Xmpp Server receiver work failed: " + e.getMessage(), e); |
120 | 0 | } |
121 | 0 | } |
122 | |
|
123 | |
private class XMPPWorker implements Work |
124 | |
{ |
125 | 0 | Packet packet = null; |
126 | |
|
127 | |
public XMPPWorker(Packet message) |
128 | 0 | { |
129 | 0 | this.packet = message; |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public void run() |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 0 | UMOMessageAdapter adapter = connector.getMessageAdapter(packet); |
140 | |
|
141 | 0 | if (logger.isDebugEnabled()) |
142 | |
{ |
143 | 0 | logger.debug("Processing XMPP packet from: " + packet.getFrom()); |
144 | 0 | logger.debug("UMOMessageAdapter is a: " + adapter.getClass().getName()); |
145 | |
} |
146 | |
|
147 | 0 | UMOMessage returnMessage = routeMessage(new MuleMessage(adapter), endpoint.isSynchronous()); |
148 | |
|
149 | 0 | if (returnMessage != null && packet instanceof Message) |
150 | |
{ |
151 | 0 | RequestContext.rewriteEvent(returnMessage); |
152 | 0 | Packet result = (Packet)connector.getDefaultResponseTransformer().transform( |
153 | |
returnMessage.getPayload()); |
154 | 0 | xmppConnection.sendPacket(result); |
155 | |
} |
156 | |
} |
157 | 0 | catch (Exception e) |
158 | |
{ |
159 | 0 | handleException(e); |
160 | 0 | } |
161 | 0 | } |
162 | |
|
163 | |
public void release() |
164 | |
{ |
165 | |
|
166 | 0 | } |
167 | |
} |
168 | |
} |