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