1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
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 | |
|
42 | 48 | } |
43 | |
|
44 | |
protected void doDispose() |
45 | |
{ |
46 | |
|
47 | 80 | } |
48 | |
|
49 | |
protected void doConnect() throws Exception |
50 | |
{ |
51 | |
|
52 | 48 | } |
53 | |
|
54 | |
protected void doDisconnect() throws Exception |
55 | |
{ |
56 | |
|
57 | 48 | } |
58 | |
|
59 | |
protected void doStart() throws MuleException |
60 | |
{ |
61 | |
|
62 | 48 | } |
63 | |
|
64 | |
protected void doStop() throws MuleException |
65 | |
{ |
66 | |
|
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 | |
|
88 | |
try |
89 | |
{ |
90 | 0 | AccountManager accManager = new AccountManager(xmppConnection); |
91 | 0 | accManager.createAccount(username, password); |
92 | |
} |
93 | 0 | catch (XMPPException ex) |
94 | |
{ |
95 | |
|
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 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
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 | |
} |