Coverage Report - org.mule.transport.xmpp.XmppMultiUserChatConversation
 
Classes in this File Line Coverage Branch Coverage Complexity
XmppMultiUserChatConversation
0%
0/43
0%
0/12
0
 
 1  
 /*
 2  
  * $Id: XmppMultiUserChatConversation.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.xmpp;
 12  
 
 13  
 import org.mule.api.endpoint.ImmutableEndpoint;
 14  
 import org.mule.transport.ConnectException;
 15  
 import org.mule.util.UUID;
 16  
 
 17  
 import org.jivesoftware.smack.PacketCollector;
 18  
 import org.jivesoftware.smack.SmackConfiguration;
 19  
 import org.jivesoftware.smack.XMPPException;
 20  
 import org.jivesoftware.smack.packet.Message;
 21  
 import org.jivesoftware.smack.packet.XMPPError;
 22  
 import org.jivesoftware.smackx.muc.DiscussionHistory;
 23  
 import org.jivesoftware.smackx.muc.MultiUserChat;
 24  
 
 25  
 public class XmppMultiUserChatConversation extends AbstractXmppConversation
 26  
 {
 27  
     private MultiUserChat chat;
 28  
     private String nickname;
 29  
     
 30  
     public XmppMultiUserChatConversation(ImmutableEndpoint endpoint)
 31  
     {
 32  0
         super(endpoint);
 33  
         
 34  0
         Object nickValue = endpoint.getProperty(XmppConnector.XMPP_NICKNAME);
 35  0
         if (nickValue != null)
 36  
         {
 37  0
             nickname = nickValue.toString();
 38  
         }
 39  
         else
 40  
         {
 41  0
             nickname = UUID.getUUID().toString();
 42  
         }
 43  0
     }
 44  
 
 45  
     @Override
 46  
     protected void doConnect() throws ConnectException
 47  
     {
 48  0
         chat = new MultiUserChat(connection, recipient);
 49  0
         joinChat();
 50  0
     }
 51  
 
 52  
     protected void joinChat() throws ConnectException
 53  
     {
 54  
         try
 55  
         {
 56  0
             tryToJoinChat();
 57  
         }
 58  0
         catch (XMPPException e)
 59  
         {
 60  0
             if (roomDoesNotExist(e))
 61  
             {
 62  0
                 createRoom();
 63  
             }
 64  
             else
 65  
             {
 66  0
                 throw new ConnectException(e, this);
 67  
             }
 68  0
         }
 69  0
     }
 70  
     
 71  
     protected void tryToJoinChat() throws XMPPException
 72  
     {
 73  0
         DiscussionHistory history = new DiscussionHistory();
 74  0
         history.setMaxStanzas(0);
 75  
         
 76  
         // use the same default value that the smack API uses internally
 77  0
         long joinTimeout = SmackConfiguration.getPacketReplyTimeout();
 78  
         
 79  0
         chat.join(nickname, null, history, joinTimeout);
 80  0
         if (logger.isDebugEnabled())
 81  
         {
 82  0
             logger.debug("joined groupchat '" + recipient + "'");
 83  
         }
 84  0
     }
 85  
     
 86  
     protected boolean roomDoesNotExist(XMPPException exception)
 87  
     {
 88  0
         XMPPError error = exception.getXMPPError();
 89  0
         if ((error.getCode() == 404) &&
 90  
             error.getCondition().equals(XMPPError.Condition.recipient_unavailable.toString()))
 91  
         {
 92  0
             return true;
 93  
         }
 94  0
         return false;
 95  
     }
 96  
     
 97  
     protected void createRoom() throws ConnectException
 98  
     {
 99  
         try
 100  
         {
 101  0
             chat.create(nickname);
 102  0
             if (logger.isDebugEnabled())
 103  
             {
 104  0
                 logger.debug("created and joined groupchat '" + recipient + "'");
 105  
             }
 106  
         }
 107  0
         catch (XMPPException e)
 108  
         {
 109  0
             throw new ConnectException(e, this);
 110  0
         }
 111  0
     }
 112  
 
 113  
     @Override
 114  
     protected void doDisconnect()
 115  
     {
 116  0
         chat.leave();
 117  0
     }
 118  
     
 119  
     /**
 120  
      * This implementation returns <code>null</code> as we override {@link #receive()} and
 121  
      * {@link #receive(long)}.
 122  
      */
 123  
     @Override
 124  
     protected PacketCollector createPacketCollector()
 125  
     {
 126  0
         return null;
 127  
     }
 128  
 
 129  
     public void dispatch(Message message) throws XMPPException
 130  
     {
 131  0
         message.setType(Message.Type.groupchat);
 132  0
         message.setTo(recipient);
 133  
         
 134  0
         chat.sendMessage(message);
 135  0
     }
 136  
 
 137  
     @Override
 138  
     public Message receive()
 139  
     {
 140  0
         return chat.nextMessage();
 141  
     }
 142  
 
 143  
     @Override
 144  
     public Message receive(long timeout)
 145  
     {
 146  0
         return chat.nextMessage(timeout);
 147  
     }
 148  
 }