1
2
3
4
5
6
7
8
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
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 private int port = 5222;
42 private String serviceName = null;
43 private String user;
44 private String password;
45 private String resource;
46 private boolean createAccount = false;
47
48 private XMPPConnection connection;
49 private XmppConversationFactory conversationFactory = new XmppConversationFactory();
50
51 public XmppConnector(MuleContext context)
52 {
53 super(context);
54 }
55
56 protected static String getRecipient(ImmutableEndpoint endpoint)
57 {
58
59 return endpoint.getEndpointURI().getPath().substring(1);
60 }
61
62 @Override
63 protected void doInitialise() throws InitialisationException
64 {
65 try
66 {
67 createXmppConnection();
68 }
69 catch (XMPPException ex)
70 {
71 throw new InitialisationException(ex, this);
72 }
73 }
74
75 @Override
76 protected void doDispose()
77 {
78 connection = null;
79 }
80
81 @Override
82 protected void doConnect() throws Exception
83 {
84 connectToJabberServer();
85 }
86
87 @Override
88 protected void doDisconnect() throws Exception
89 {
90 if (connection.isConnected())
91 {
92 connection.disconnect();
93 }
94 }
95
96 @Override
97 protected void doStart() throws MuleException
98 {
99
100 }
101
102 @Override
103 protected void doStop() throws MuleException
104 {
105
106 }
107
108 public String getProtocol()
109 {
110 return XMPP;
111 }
112
113 protected void createXmppConnection() throws XMPPException
114 {
115 if (logger.isDebugEnabled())
116 {
117 logger.debug("Connecting to " + host + ":" + port);
118 }
119
120 ConnectionConfiguration connectionConfig = null;
121 if (serviceName != null)
122 {
123 connectionConfig = new ConnectionConfiguration(host, port, serviceName);
124 }
125 else
126 {
127 connectionConfig = new ConnectionConfiguration(host, port);
128 }
129
130 connectionConfig.setRosterLoadedAtLogin(false);
131
132 connection = new XMPPConnection(connectionConfig);
133 }
134
135 protected void connectToJabberServer() throws XMPPException
136 {
137 connection.connect();
138
139 if (createAccount)
140 {
141 createAccount();
142 }
143
144 if (resource != null)
145 {
146 connection.login(user, password, resource);
147 }
148 else
149 {
150 connection.login(user, password);
151 }
152 }
153
154 private void createAccount()
155 {
156 try
157 {
158 AccountManager accountManager = new AccountManager(connection);
159 accountManager.createAccount(user, password);
160 }
161 catch (XMPPException ex)
162 {
163
164 logger.warn("Account (" + user + ") already exists");
165 }
166 }
167
168 @Override
169 public boolean isResponseEnabled()
170 {
171 return true;
172 }
173
174 public String getHost()
175 {
176 return host;
177 }
178
179 public void setHost(String host)
180 {
181 this.host = host;
182 }
183
184 public int getPort()
185 {
186 return port;
187 }
188
189 public void setPort(int port)
190 {
191 this.port = port;
192 }
193
194 public String getServiceName()
195 {
196 return serviceName;
197 }
198
199 public void setServiceName(String serviceName)
200 {
201 this.serviceName = serviceName;
202 }
203
204 public String getUser()
205 {
206 return user;
207 }
208
209 public void setUser(String user)
210 {
211 this.user = user;
212 }
213
214 public String getPassword()
215 {
216 return password;
217 }
218
219 public void setPassword(String password)
220 {
221 this.password = password;
222 }
223
224 public String getResource()
225 {
226 return resource;
227 }
228
229 public void setResource(String resource)
230 {
231 this.resource = resource;
232 }
233
234 public boolean isCreateAccount()
235 {
236 return createAccount;
237 }
238
239 public void setCreateAccount(boolean createAccount)
240 {
241 this.createAccount = createAccount;
242 }
243
244 public XmppConversationFactory getConversationFactory()
245 {
246 return conversationFactory;
247 }
248
249 public void setConversationFactory(XmppConversationFactory conversationFactory)
250 {
251 this.conversationFactory = conversationFactory;
252 }
253
254 protected XMPPConnection getXmppConnection()
255 {
256 return connection;
257 }
258 }