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