View Javadoc

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  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      }
43  
44      protected void doDispose()
45      {
46          // template method
47      }
48  
49      protected void doConnect() throws Exception
50      {
51          // template method
52      }
53  
54      protected void doDisconnect() throws Exception
55      {
56          // template method
57      }
58  
59      protected void doStart() throws MuleException
60      {
61          // template method
62      }
63  
64      protected void doStop() throws MuleException
65      {
66          // template method
67      }
68  
69      public String getProtocol()
70      {
71          return XMPP;
72      }
73  
74      public XMPPConnection createXmppConnection(EndpointURI endpointURI) throws XMPPException
75      {
76          logger.info("Trying to find XMPP connection for uri: " + endpointURI);
77  
78          String username = endpointURI.getUser();
79          String hostname = endpointURI.getHost();
80          String password = endpointURI.getPassword();
81          String resource = (String)endpointURI.getParams().get("resource");
82  
83          XMPPConnection xmppConnection = this.doCreateXmppConnection(endpointURI);
84  
85          if (!xmppConnection.isAuthenticated())
86          {
87              // Make sure we have an account. If we don't, make one.
88              try
89              {
90                  AccountManager accManager = new AccountManager(xmppConnection);
91                  accManager.createAccount(username, password);
92              }
93              catch (XMPPException ex)
94              {
95                  // User probably already exists, throw away...
96                  logger.info("*** account (" + username + ") already exists ***");
97              }
98  
99              if (logger.isDebugEnabled())
100             {
101                 logger.debug("Logging in as: " + username);
102                 logger.debug("pw is        : " + password);
103                 logger.debug("server       : " + hostname);
104                 logger.debug("resource     : " + resource);
105             }
106 
107             if (resource == null)
108             {
109                 xmppConnection.login(username, password);
110             }
111             else
112             {
113                 xmppConnection.login(username, password, resource);
114             }
115         }
116         else
117         {
118             if (logger.isDebugEnabled())
119             {
120                 logger.debug("Already authenticated on this connection, no need to log in again.");
121             }
122         }
123         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         XMPPConnection xmppConnection = null;
139         
140         if (endpointURI.getPort() != -1)
141         {
142             xmppConnection = new XMPPConnection(endpointURI.getHost(), endpointURI.getPort());
143         }
144         else
145         {
146             xmppConnection = new XMPPConnection(endpointURI.getHost());
147         }
148         
149         return xmppConnection;
150     }
151 
152     public boolean isRemoteSyncEnabled()
153     {
154         return true;
155     }
156 }