View Javadoc

1   /*
2    * $Id: XmppMessageDispatcher.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.xmpp;
12  
13  import org.mule.impl.MuleMessage;
14  import org.mule.providers.AbstractMessageDispatcher;
15  import org.mule.umo.UMOEvent;
16  import org.mule.umo.UMOMessage;
17  import org.mule.umo.endpoint.MalformedEndpointException;
18  import org.mule.umo.endpoint.UMOEndpointURI;
19  import org.mule.umo.endpoint.UMOImmutableEndpoint;
20  
21  import org.jivesoftware.smack.Chat;
22  import org.jivesoftware.smack.GroupChat;
23  import org.jivesoftware.smack.XMPPConnection;
24  import org.jivesoftware.smack.packet.Message;
25  
26  /**
27   * <code>XmppMessageDispatcher</code> allows Mule events to be sent and received
28   * over Xmpp
29   */
30  
31  public class XmppMessageDispatcher extends AbstractMessageDispatcher
32  {
33      private final XmppConnector connector;
34      private volatile XMPPConnection xmppConnection = null;
35      private volatile Chat chat;
36      private volatile GroupChat groupChat;
37  
38      public XmppMessageDispatcher(UMOImmutableEndpoint endpoint)
39      {
40          super(endpoint);
41          this.connector = (XmppConnector)endpoint.getConnector();
42      }
43  
44      protected void doConnect() throws Exception
45      {
46          if (xmppConnection == null)
47          {
48              UMOEndpointURI uri = endpoint.getEndpointURI();
49              xmppConnection = connector.createXmppConnection(uri);
50          }
51      }
52  
53      protected void doDisconnect() throws Exception
54      {
55          try
56          {
57              if (groupChat != null)
58              {
59                  groupChat.leave();
60              }
61              if (xmppConnection != null)
62              {
63                  xmppConnection.close();
64              }
65          }
66          finally
67          {
68              xmppConnection = null;
69          }
70      }
71  
72      protected void doDispose()
73      {
74          // template method
75      }
76  
77      protected void doDispatch(UMOEvent event) throws Exception
78      {
79          sendMessage(event);
80      }
81  
82      protected UMOMessage doSend(UMOEvent event) throws Exception
83      {
84          sendMessage(event);
85  
86          if (useRemoteSync(event))
87          {
88              Message response;
89  
90              if (groupChat != null)
91              {
92                  response = groupChat.nextMessage(event.getTimeout());
93              }
94              else
95              {
96                  response = chat.nextMessage(event.getTimeout());
97              }
98  
99              if (response != null)
100             {
101                 if (logger.isDebugEnabled())
102                 {
103                     logger.debug("Got a response from chat: " + chat);
104                 }
105                 return new MuleMessage(connector.getMessageAdapter(response));
106             }
107         }
108         return null;
109     }
110 
111     protected void sendMessage(UMOEvent event) throws Exception
112     {
113         if (chat == null && groupChat == null)
114         {
115             UMOMessage msg = event.getMessage();
116             boolean group = msg.getBooleanProperty(XmppConnector.XMPP_GROUP_CHAT, false);
117             String nickname = msg.getStringProperty(XmppConnector.XMPP_NICKNAME, "mule");
118             String recipient = event.getEndpoint().getEndpointURI().getPath().substring(1);
119 
120             if (group)
121             {
122                 groupChat = new GroupChat(xmppConnection, recipient);
123                 if (!groupChat.isJoined())
124                 {
125                     groupChat.join(nickname);
126                 }
127             }
128             else
129             {
130                 chat = new Chat(xmppConnection, recipient);
131             }
132         }
133 
134         Object msgObj = event.getMessage().getPayload();
135         Message message;
136         // avoid duplicate transformation
137         if (!(msgObj instanceof Message))
138         {
139             message = (Message)event.getTransformedMessage();
140         }
141         else
142         {
143             message = (Message)msgObj;
144         }
145 
146         if (logger.isTraceEnabled())
147         {
148             logger.trace("Transformed packet: " + message.toXML());
149         }
150 
151         if (chat != null)
152         {
153             chat.sendMessage(message);
154         }
155         else
156         {
157             groupChat.sendMessage(message);
158         }
159 
160         if (logger.isDebugEnabled())
161         {
162             logger.debug("Packet successfully sent");
163         }
164     }
165 
166     /**
167      * Make a specific request to the underlying transport
168      * 
169      * @param endpoint the endpoint to use when connecting to the resource
170      * @param timeout the maximum time the operation should block before returning.
171      *            The call should return immediately if there is data available. If
172      *            no data becomes available before the timeout elapses, null will be
173      *            returned
174      * @return the result of the request wrapped in a UMOMessage object. Null will be
175      *         returned if no data was avaialable
176      * @throws Exception if the call to the underlying protocal cuases an exception
177      */
178     protected UMOMessage doReceive(long timeout) throws Exception
179     {
180         // Should be in the form of xmpp://user:pass@host:[port]/folder
181         String to = (String)endpoint.getProperty("folder");
182         if (to == null)
183         {
184             throw new MalformedEndpointException(endpoint.getEndpointURI().toString());
185         }
186         Chat chat = xmppConnection.createChat(to);
187         Message message = null;
188         if (timeout == UMOEvent.TIMEOUT_WAIT_FOREVER)
189         {
190             message = chat.nextMessage();
191         }
192         else if (timeout == UMOEvent.TIMEOUT_DO_NOT_WAIT)
193         {
194             message = chat.nextMessage(1);
195         }
196         else
197         {
198             message = chat.nextMessage(timeout);
199         }
200         if (message != null)
201         {
202             return new MuleMessage(connector.getMessageAdapter(message));
203         }
204         else
205         {
206             return null;
207         }
208     }
209 
210 }