View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Server-side component for receiving XMPP (Jabber) messages.  Requires a {@linkplain #setConnection(XMPPConnection)
31   * connection} to be set, in addition to the {@link #setMessageFactory(org.springframework.ws.WebServiceMessageFactory)
32   * messageFactory} and {@link #setMessageReceiver(org.springframework.ws.transport.WebServiceMessageReceiver)
33   * messageReceiver} required by the base class.
34   *
35   * @author Gildas Cuisinier
36   * @author Arjen Poutsma
37   * @see org.springframework.ws.transport.xmpp.support.XmppConnectionFactoryBean
38   * @since 2.0
39   */
40  public class XmppMessageReceiver extends AbstractStandaloneMessageReceiver {
41  
42      /** Default encoding used to read from and write to {@link org.jivesoftware.smack.packet.Message} messages. */
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      /** Sets the {@code XMPPConnection} to use. Setting this property is required. */
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 }