Coverage Report - org.mule.transport.xmpp.XmppConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
XmppConnector
22%
8/37
0%
0/10
1.6
 
 1  
 /*
 2  
  * $Id: XmppConnector.java 10489 2008-01-23 17:53:38Z dfeist $
 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.transport.xmpp;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.endpoint.EndpointURI;
 15  
 import org.mule.api.lifecycle.InitialisationException;
 16  
 import org.mule.transport.AbstractConnector;
 17  
 
 18  
 import org.jivesoftware.smack.AccountManager;
 19  
 import org.jivesoftware.smack.XMPPConnection;
 20  
 import org.jivesoftware.smack.XMPPException;
 21  
 
 22  
 /**
 23  
  * <code>XmppConnector</code> TODO
 24  
  */
 25  48
 public class XmppConnector extends AbstractConnector
 26  
 {
 27  
 
 28  
     public static final String XMPP = "xmpp";
 29  
     public static final String XMPP_PROPERTY_PREFIX = "";
 30  
     public static final String XMPP_RESOURCE = XMPP_PROPERTY_PREFIX + "resource";
 31  
     public static final String XMPP_SUBJECT = XMPP_PROPERTY_PREFIX + "subject";
 32  
     public static final String XMPP_THREAD = XMPP_PROPERTY_PREFIX + "thread";
 33  
     public static final String XMPP_TO = XMPP_PROPERTY_PREFIX + "to";
 34  
     public static final String XMPP_FROM = XMPP_PROPERTY_PREFIX + "from";
 35  
     public static final String XMPP_GROUP_CHAT = XMPP_PROPERTY_PREFIX + "groupChat";
 36  
     public static final String XMPP_NICKNAME = XMPP_PROPERTY_PREFIX + "nickname";
 37  
 
 38  
 
 39  
     protected void doInitialise() throws InitialisationException
 40  
     {
 41  
         // template method, nothing to do
 42  48
     }
 43  
 
 44  
     protected void doDispose()
 45  
     {
 46  
         // template method
 47  80
     }
 48  
 
 49  
     protected void doConnect() throws Exception
 50  
     {
 51  
         // template method
 52  48
     }
 53  
 
 54  
     protected void doDisconnect() throws Exception
 55  
     {
 56  
         // template method
 57  48
     }
 58  
 
 59  
     protected void doStart() throws MuleException
 60  
     {
 61  
         // template method
 62  48
     }
 63  
 
 64  
     protected void doStop() throws MuleException
 65  
     {
 66  
         // template method
 67  48
     }
 68  
 
 69  
     public String getProtocol()
 70  
     {
 71  88
         return XMPP;
 72  
     }
 73  
 
 74  
     public XMPPConnection createXmppConnection(EndpointURI endpointURI) throws XMPPException
 75  
     {
 76  0
         logger.info("Trying to find XMPP connection for uri: " + endpointURI);
 77  
 
 78  0
         String username = endpointURI.getUser();
 79  0
         String hostname = endpointURI.getHost();
 80  0
         String password = endpointURI.getPassword();
 81  0
         String resource = (String)endpointURI.getParams().get("resource");
 82  
 
 83  0
         XMPPConnection xmppConnection = this.doCreateXmppConnection(endpointURI);
 84  
 
 85  0
         if (!xmppConnection.isAuthenticated())
 86  
         {
 87  
             // Make sure we have an account. If we don't, make one.
 88  
             try
 89  
             {
 90  0
                 AccountManager accManager = new AccountManager(xmppConnection);
 91  0
                 accManager.createAccount(username, password);
 92  
             }
 93  0
             catch (XMPPException ex)
 94  
             {
 95  
                 // User probably already exists, throw away...
 96  0
                 logger.info("*** account (" + username + ") already exists ***");
 97  0
             }
 98  
 
 99  0
             if (logger.isDebugEnabled())
 100  
             {
 101  0
                 logger.debug("Logging in as: " + username);
 102  0
                 logger.debug("pw is        : " + password);
 103  0
                 logger.debug("server       : " + hostname);
 104  0
                 logger.debug("resource     : " + resource);
 105  
             }
 106  
 
 107  0
             if (resource == null)
 108  
             {
 109  0
                 xmppConnection.login(username, password);
 110  
             }
 111  
             else
 112  
             {
 113  0
                 xmppConnection.login(username, password, resource);
 114  
             }
 115  
         }
 116  
         else
 117  
         {
 118  0
             if (logger.isDebugEnabled())
 119  
             {
 120  0
                 logger.debug("Already authenticated on this connection, no need to log in again.");
 121  
             }
 122  
         }
 123  0
         return xmppConnection;
 124  
     }
 125  
 
 126  
     /**
 127  
      * This method creates and returns the {@link XMPPConnection} object that's uses to talk to
 128  
      * the Jabber server.
 129  
      * <p/>
 130  
      * Subclasses can override this method to create a more specialized {@link XMPPConnection}.
 131  
      * 
 132  
      * @param endpointURI
 133  
      * @return
 134  
      * @throws XMPPException
 135  
      */
 136  
     protected XMPPConnection doCreateXmppConnection(EndpointURI endpointURI) throws XMPPException
 137  
     {
 138  0
         XMPPConnection xmppConnection = null;
 139  
         
 140  0
         if (endpointURI.getPort() != -1)
 141  
         {
 142  0
             xmppConnection = new XMPPConnection(endpointURI.getHost(), endpointURI.getPort());
 143  
         }
 144  
         else
 145  
         {
 146  0
             xmppConnection = new XMPPConnection(endpointURI.getHost());
 147  
         }
 148  
         
 149  0
         return xmppConnection;
 150  
     }
 151  
 
 152  
     public boolean isRemoteSyncEnabled()
 153  
     {
 154  0
         return true;
 155  
     }
 156  
 }