1 10 11 package org.mule.providers.xmpp; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jivesoftware.smack.Chat; 16 import org.jivesoftware.smack.GroupChat; 17 import org.jivesoftware.smack.XMPPConnection; 18 import org.jivesoftware.smack.packet.Message; 19 import org.mule.impl.MuleMessage; 20 import org.mule.providers.AbstractMessageDispatcher; 21 import org.mule.umo.UMOEvent; 22 import org.mule.umo.UMOMessage; 23 import org.mule.umo.endpoint.MalformedEndpointException; 24 import org.mule.umo.endpoint.UMOEndpointURI; 25 import org.mule.umo.endpoint.UMOImmutableEndpoint; 26 27 35 36 public class XmppMessageDispatcher extends AbstractMessageDispatcher 37 { 38 41 protected static Log logger = LogFactory.getLog(XmppMessageDispatcher.class); 42 43 private XmppConnector connector; 44 45 private XMPPConnection xmppConnection = null; 46 47 private Chat chat; 48 49 private GroupChat groupChat; 50 51 public XmppMessageDispatcher(UMOImmutableEndpoint endpoint) 52 { 53 super(endpoint); 54 this.connector = (XmppConnector)endpoint.getConnector(); 55 } 56 57 protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception 58 { 59 if (xmppConnection == null) 60 { 61 UMOEndpointURI uri = endpoint.getEndpointURI(); 62 xmppConnection = connector.createXmppConnection(uri); 63 } 64 } 65 66 protected void doDisconnect() throws Exception 67 { 68 try 69 { 70 if (groupChat != null) 71 { 72 groupChat.leave(); 73 } 74 if (xmppConnection != null) 75 { 76 xmppConnection.close(); 77 } 78 } 79 finally 80 { 81 xmppConnection = null; 82 } 83 } 84 85 protected void doDispose() 86 { 87 } 89 90 protected void doDispatch(UMOEvent event) throws Exception 91 { 92 sendMessage(event); 93 } 94 95 protected UMOMessage doSend(UMOEvent event) throws Exception 96 { 97 sendMessage(event); 98 if (useRemoteSync(event)) 99 { 100 Message response = null; 101 if (groupChat != null) 102 { 103 response = groupChat.nextMessage(event.getTimeout()); 104 } 105 else 106 { 107 response = chat.nextMessage(event.getTimeout()); 108 } 109 110 if (response != null) 111 { 112 logger.debug("Got a response from chat: " + chat); 113 return new MuleMessage(connector.getMessageAdapter(response)); 114 } 115 } 116 return null; 117 } 118 119 protected void sendMessage(UMOEvent event) throws Exception 120 { 121 if (chat == null && groupChat == null) 122 { 123 UMOMessage msg = event.getMessage(); 124 boolean group = msg.getBooleanProperty(XmppConnector.XMPP_GROUP_CHAT, false); 125 String nickname = msg.getStringProperty(XmppConnector.XMPP_NICKNAME, "mule"); 126 String recipient = event.getEndpoint().getEndpointURI().getPath().substring(1); 127 128 if (group) 129 { 130 groupChat = new GroupChat(xmppConnection, recipient); 131 if (!groupChat.isJoined()) 132 { 133 groupChat.join(nickname); 134 } 135 } 136 else 137 { 138 chat = new Chat(xmppConnection, recipient); 139 } 140 } 141 Object msgObj = event.getMessage().getPayload(); 142 Message message; 143 if (!(msgObj instanceof Message)) 145 { 146 message = (Message)event.getTransformedMessage(); 147 } 148 else 149 { 150 message = (Message)msgObj; 151 } 152 153 if (logger.isTraceEnabled()) 154 { 155 logger.trace("Transformed packet: " + message.toXML()); 156 } 157 158 if (chat != null) 159 { 160 chat.sendMessage(message); 161 } 162 else 163 { 164 groupChat.sendMessage(message); 165 } 166 if (logger.isDebugEnabled()) 167 { 168 logger.debug("packet successfully sent"); 169 } 170 } 171 172 184 protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception 185 { 186 187 String to = (String )endpoint.getProperty("folder"); 189 if (to == null) 190 { 191 throw new MalformedEndpointException(endpoint.getEndpointURI().toString()); 192 } 193 Chat chat = xmppConnection.createChat(to); 194 Message message = null; 195 if (timeout == UMOEvent.TIMEOUT_WAIT_FOREVER) 196 { 197 message = chat.nextMessage(); 198 } 199 else if (timeout == UMOEvent.TIMEOUT_DO_NOT_WAIT) 200 { 201 message = chat.nextMessage(1); 202 } 203 else 204 { 205 message = chat.nextMessage(timeout); 206 } 207 if (message != null) 208 { 209 return new MuleMessage(connector.getMessageAdapter(message)); 210 } 211 else 212 { 213 return null; 214 } 215 } 216 217 public Object getDelegateSession() 218 { 219 return null; 220 } 221 } 222 | Popular Tags |