View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.xmpp;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.endpoint.OutboundEndpoint;
13  import org.mule.transformer.types.DataTypeFactory;
14  import org.mule.transport.AbstractMessageDispatcher;
15  import org.mule.transport.NullPayload;
16  
17  import org.jivesoftware.smack.packet.Message;
18  
19  /**
20   * Allows Mule events to be sent over Xmpp
21   */
22  public class XmppMessageDispatcher extends AbstractMessageDispatcher
23  {
24      private final XmppConnector connector;
25      private XmppConversation conversation;
26  
27      public XmppMessageDispatcher(OutboundEndpoint endpoint)
28      {
29          super(endpoint);
30          connector = (XmppConnector) endpoint.getConnector();
31          conversation = connector.getConversationFactory().create(endpoint);
32      }
33  
34      @Override
35      protected void doConnect() throws Exception
36      {
37          conversation.connect();
38      }
39  
40      @Override
41      protected void doDisconnect() throws Exception
42      {
43          conversation.disconnect();
44      }
45  
46      @Override
47      protected void doDispose()
48      {
49          conversation = null;
50      }
51  
52      @Override
53      protected void doDispatch(MuleEvent event) throws Exception
54      {
55          sendMessage(event);
56      }
57  
58      @Override
59      protected MuleMessage doSend(MuleEvent event) throws Exception
60      {
61          sendMessage(event);
62  
63          // TODO xmpp: even sync endpoints do not wait for a reply. Look at how the JMS transport handles replies, use reply handler
64  //        if (returnResponse(event, false))
65  //        {
66  //            Message response = conversation.receive(event.getTimeout());
67  //
68  ////            if (groupChat != null)
69  ////            {
70  ////                response = groupChat.nextMessage(event.getTimeout());
71  ////            }
72  //
73  //            if (response != null)
74  //            {
75  //                return createMuleMessage(response);
76  //            }
77  //        }
78          return new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext());
79      }
80  
81      protected void sendMessage(MuleEvent event) throws Exception
82      {
83          Message jabberMessage = event.getMessage().getPayload(DataTypeFactory.create(Message.class));
84          conversation.dispatch(jabberMessage);
85  
86          if (logger.isDebugEnabled())
87          {
88              String recipient = XmppConnector.getRecipient(endpoint);
89              logger.debug("Message \"" + jabberMessage.getBody()
90                  + "\" successfully sent to " + recipient);
91          }
92      }
93  }