1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.transport.xmpp;
18
19 import org.springframework.ws.transport.support.AbstractStandaloneMessageReceiver;
20
21 import org.jivesoftware.smack.PacketListener;
22 import org.jivesoftware.smack.XMPPConnection;
23 import org.jivesoftware.smack.XMPPException;
24 import org.jivesoftware.smack.filter.PacketFilter;
25 import org.jivesoftware.smack.filter.PacketTypeFilter;
26 import org.jivesoftware.smack.packet.Message;
27 import org.jivesoftware.smack.packet.Packet;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class XmppMessageReceiver extends AbstractStandaloneMessageReceiver {
41
42
43 public static final String DEFAULT_MESSAGE_ENCODING = "UTF-8";
44
45 private XMPPConnection connection;
46
47 private WebServicePacketListener packetListener;
48
49 private String messageEncoding = DEFAULT_MESSAGE_ENCODING;
50
51 public XmppMessageReceiver() {
52 }
53
54
55 public void setConnection(XMPPConnection connection) {
56 this.connection = connection;
57 }
58
59 @Override
60 protected void onActivate() throws XMPPException {
61 if (!connection.isConnected()) {
62 connection.connect();
63 }
64 }
65
66 @Override
67 protected void onStart() {
68 if (logger.isInfoEnabled()) {
69 logger.info("Starting XMPP receiver [" + connection.getUser() + "]");
70 }
71 packetListener = new WebServicePacketListener();
72 PacketFilter packetFilter = new PacketTypeFilter(Message.class);
73 connection.addPacketListener(packetListener, packetFilter);
74 }
75
76 @Override
77 protected void onStop() {
78 if (logger.isInfoEnabled()) {
79 logger.info("Stopping XMPP receiver [" + connection.getUser() + "]");
80 }
81 connection.removePacketListener(packetListener);
82 packetListener = null;
83 }
84
85 @Override
86 protected void onShutdown() {
87 if (logger.isInfoEnabled()) {
88 logger.info("Shutting down XMPP receiver [" + connection.getUser() + "]");
89 }
90 if (connection.isConnected()) {
91 connection.disconnect();
92 }
93 }
94
95 private class WebServicePacketListener implements PacketListener {
96
97 public void processPacket(Packet packet) {
98 logger.info("Received " + packet);
99 if (packet instanceof Message) {
100 Message message = (Message) packet;
101 try {
102 XmppReceiverConnection wsConnection = new XmppReceiverConnection(connection, message);
103 wsConnection.setMessageEncoding(messageEncoding);
104 handleConnection(wsConnection);
105 }
106 catch (Exception ex) {
107 logger.error(ex);
108 }
109 }
110 }
111 }
112
113 }