Coverage Report - org.mule.transport.xmpp.XmppConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
XmppConnector
0%
0/70
0%
0/10
0
 
 1  
 /*
 2  
  * $Id: XmppConnector.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.MuleContext;
 14  
 import org.mule.api.MuleException;
 15  
 import org.mule.api.endpoint.ImmutableEndpoint;
 16  
 import org.mule.api.lifecycle.InitialisationException;
 17  
 import org.mule.transport.AbstractConnector;
 18  
 
 19  
 import org.jivesoftware.smack.AccountManager;
 20  
 import org.jivesoftware.smack.ConnectionConfiguration;
 21  
 import org.jivesoftware.smack.XMPPConnection;
 22  
 import org.jivesoftware.smack.XMPPException;
 23  
 
 24  
 /**
 25  
  * <code>XmppConnector</code> represents a connection to a Jabber server.
 26  
  */
 27  
 public class XmppConnector extends AbstractConnector
 28  
 {
 29  
     public static final String XMPP = "xmpp";
 30  
     public static final String XMPP_RESOURCE = "resource";
 31  
     public static final String XMPP_SUBJECT = "subject";
 32  
     public static final String XMPP_THREAD = "thread";
 33  
     public static final String XMPP_TO = "to";
 34  
     public static final String XMPP_FROM = "from";
 35  
     public static final String XMPP_GROUP_CHAT = "groupChat";
 36  
     public static final String XMPP_NICKNAME = "nickname";
 37  
     public static final String XMPP_RECIPIENT = "recipient";
 38  
     public static final String XMPP_TYPE = "type";
 39  
     
 40  
     private String host;
 41  0
     private int port = 5222; // default jabber port
 42  0
     private String serviceName = null;
 43  
     private String user;
 44  
     private String password;
 45  
     private String resource;
 46  0
     private boolean createAccount = false;
 47  
     
 48  
     private XMPPConnection connection;
 49  0
     private XmppConversationFactory conversationFactory = new XmppConversationFactory();
 50  
     
 51  
     public XmppConnector(MuleContext context)
 52  
     {
 53  0
         super(context);
 54  0
     }
 55  
     
 56  
     protected static String getRecipient(ImmutableEndpoint endpoint)
 57  
     {
 58  
         // the path begins with a '/'
 59  0
         return endpoint.getEndpointURI().getPath().substring(1);
 60  
     }
 61  
     
 62  
     @Override
 63  
     protected void doInitialise() throws InitialisationException
 64  
     {
 65  
         try
 66  
         {
 67  0
             createXmppConnection();
 68  
         }
 69  0
         catch (XMPPException ex)
 70  
         {
 71  0
             throw new InitialisationException(ex, this);
 72  0
         }
 73  0
     }
 74  
 
 75  
     @Override
 76  
     protected void doDispose()
 77  
     {
 78  0
         connection = null;
 79  0
     }
 80  
 
 81  
     @Override
 82  
     protected void doConnect() throws Exception
 83  
     {
 84  0
         connectToJabberServer();
 85  0
     }
 86  
 
 87  
     @Override
 88  
     protected void doDisconnect() throws Exception
 89  
     {
 90  0
         if (connection.isConnected())
 91  
         {
 92  0
             connection.disconnect();
 93  
         }
 94  0
     }
 95  
 
 96  
     @Override
 97  
     protected void doStart() throws MuleException
 98  
     {
 99  
         // template method
 100  0
     }
 101  
 
 102  
     @Override
 103  
     protected void doStop() throws MuleException
 104  
     {
 105  
         // template method
 106  0
     }
 107  
 
 108  
     public String getProtocol()
 109  
     {
 110  0
         return XMPP;
 111  
     }
 112  
 
 113  
     protected void createXmppConnection() throws XMPPException
 114  
     {
 115  0
         if (logger.isDebugEnabled())
 116  
         {
 117  0
             logger.debug("Connecting to " + host + ":" + port);
 118  
         }
 119  
 
 120  0
         ConnectionConfiguration connectionConfig = null;
 121  0
         if (serviceName != null)
 122  
         {
 123  0
             connectionConfig = new ConnectionConfiguration(host, port, serviceName);
 124  
         }
 125  
         else
 126  
         {
 127  0
             connectionConfig = new ConnectionConfiguration(host, port);
 128  
         }
 129  
         // no need to load the roster (this is not an interactive app)
 130  0
         connectionConfig.setRosterLoadedAtLogin(false);
 131  
         
 132  0
         connection = new XMPPConnection(connectionConfig);
 133  0
     }
 134  
 
 135  
     protected void connectToJabberServer() throws XMPPException
 136  
     {
 137  0
         connection.connect();
 138  
         
 139  0
         if (createAccount)
 140  
         {
 141  0
             createAccount();
 142  
         }
 143  
      
 144  0
         if (resource != null)
 145  
         {
 146  0
             connection.login(user, password, resource);
 147  
         }
 148  
         else
 149  
         {
 150  0
             connection.login(user, password);
 151  
         }
 152  0
     }
 153  
 
 154  
     private void createAccount()
 155  
     {
 156  
         try
 157  
         {
 158  0
             AccountManager accountManager = new AccountManager(connection);
 159  0
             accountManager.createAccount(user, password);
 160  
         }
 161  0
         catch (XMPPException ex)
 162  
         {
 163  
             // User probably already exists, throw away...
 164  0
             logger.warn("Account (" + user + ") already exists");
 165  0
         }
 166  0
     }
 167  
 
 168  
     @Override
 169  
     public boolean isResponseEnabled()
 170  
     {
 171  0
         return true;
 172  
     }
 173  
 
 174  
     public String getHost()
 175  
     {
 176  0
         return host;
 177  
     }
 178  
 
 179  
     public void setHost(String host)
 180  
     {
 181  0
         this.host = host;
 182  0
     }
 183  
 
 184  
     public int getPort()
 185  
     {
 186  0
         return port;
 187  
     }
 188  
 
 189  
     public void setPort(int port)
 190  
     {
 191  0
         this.port = port;
 192  0
     }
 193  
     
 194  
     public String getServiceName()
 195  
     {
 196  0
         return serviceName;
 197  
     }
 198  
 
 199  
     public void setServiceName(String serviceName)
 200  
     {
 201  0
         this.serviceName = serviceName;
 202  0
     }
 203  
 
 204  
     public String getUser()
 205  
     {
 206  0
         return user;
 207  
     }
 208  
 
 209  
     public void setUser(String user)
 210  
     {
 211  0
         this.user = user;
 212  0
     }
 213  
 
 214  
     public String getPassword()
 215  
     {
 216  0
         return password;
 217  
     }
 218  
 
 219  
     public void setPassword(String password)
 220  
     {
 221  0
         this.password = password;
 222  0
     }
 223  
 
 224  
     public String getResource()
 225  
     {
 226  0
         return resource;
 227  
     }
 228  
 
 229  
     public void setResource(String resource)
 230  
     {
 231  0
         this.resource = resource;
 232  0
     }
 233  
 
 234  
     public boolean isCreateAccount()
 235  
     {
 236  0
         return createAccount;
 237  
     }
 238  
 
 239  
     public void setCreateAccount(boolean createAccount)
 240  
     {
 241  0
         this.createAccount = createAccount;
 242  0
     }
 243  
 
 244  
     public XmppConversationFactory getConversationFactory()
 245  
     {
 246  0
         return conversationFactory;
 247  
     }
 248  
 
 249  
     public void setConversationFactory(XmppConversationFactory conversationFactory)
 250  
     {
 251  0
         this.conversationFactory = conversationFactory;
 252  0
     }
 253  
     
 254  
     protected XMPPConnection getXmppConnection()
 255  
     {
 256  0
         return connection;
 257  
     }
 258  
 }