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