public class XmppSession extends Object implements AutoCloseable
XmppSession xmppSession = new XmppSession("domain");
xmppSession.connect();
xmppSession.login("username", "password");
xmppSession.send(new Presence());
By default, the session will try to establish a TCP connection over port 5222 and will try BOSH as fallback.
You can configure a session and its connection methods by passing appropriate configurations in its constructor.
xmppSession.send(new Message(Jid.valueOf("juliet@example.net"), Message.Type.CHAT));
xmppSession.close();
// Listen for messages
xmppSession.addInboundMessageListener(new MessageListener() {
@Override
public void handleMessage(MessageEvent e) {
// Handle inbound message
}
});
// Listen for presence changes
xmppSession.addInboundPresenceListener(new PresenceListener() {
@Override
public void handlePresence(PresenceEvent e) {
// Handle inbound presence.
}
});
This class is thread-safe, which means you can safely add listeners or call send(), close() (and other methods) from different threads.| Modifier and Type | Class and Description |
|---|---|
static class |
XmppSession.Status
Represents the session status.
|
| Constructor and Description |
|---|
XmppSession(String xmppServiceDomain,
ConnectionConfiguration... connectionConfigurations)
Creates a session with the specified service domain, by using the default configuration.
|
XmppSession(String xmppServiceDomain,
XmppSessionConfiguration configuration,
ConnectionConfiguration... connectionConfigurations)
Creates a session with the specified service domain by using a configuration.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addInboundIQListener(IQListener iqListener)
Adds an inbound IQ listener to the session, which will get notified, whenever an IQ stanza is received.
|
void |
addInboundMessageListener(MessageListener messageListener)
Adds an inbound message listener to the session, which will get notified, whenever a message is received.
|
void |
addInboundPresenceListener(PresenceListener presenceListener)
Adds an inbound presence listener to the session, which will get notified, whenever a presence is received.
|
void |
addIQHandler(Class<?> type,
IQHandler iqHandler)
Adds an IQ handler for a given payload type.
|
void |
addIQHandler(Class<?> type,
IQHandler iqHandler,
boolean invokeAsync)
Adds an IQ handler for a given payload type.
|
void |
addIQListener(IQListener iqListener)
|
void |
addMessageListener(MessageListener messageListener)
|
void |
addOutboundIQListener(IQListener iqListener)
Adds an outbound IQ listener to the session, which will get notified, whenever an IQ stanza is sent.
|
void |
addOutboundMessageListener(MessageListener messageListener)
Adds an outbound message listener to the session, which will get notified, whenever a message is sent.
|
void |
addOutboundPresenceListener(PresenceListener presenceListener)
Adds an outbound presence listener to the session, which will get notified, whenever a presence is sent.
|
void |
addPresenceListener(PresenceListener presenceListener)
|
void |
addSessionStatusListener(SessionStatusListener sessionStatusListener)
Adds a session listener, which listens for session status changes.
|
void |
close()
Explicitly closes the session and performs a clean up of all listeners.
|
void |
connect()
Connects to the XMPP server.
|
void |
connect(Jid from)
Connects to the XMPP server.
|
Marshaller |
createMarshaller()
Creates a marshaller, which can be used to create XML from objects.
|
Unmarshaller |
createUnmarshaller()
Creates a new unmarshaller, which can be used to unmarshal XML to objects.
|
Connection |
getActiveConnection()
Gets the actively used connection.
|
ChatManager |
getChatManager()
Deprecated.
Use
getManager(Class) instead. |
XmppSessionConfiguration |
getConfiguration()
Gets the configuration for this session.
|
Jid |
getConnectedResource()
Gets the connected resource, which is assigned by the server after resource binding.
|
List<Connection> |
getConnections()
Gets an unmodifiable list of connections, which this session will try during connecting.
|
XmppDebugger |
getDebugger()
Gets the debugger or null if no debugger class was specified in the configuration.
|
int |
getDefaultTimeout()
Deprecated.
|
String |
getDomain()
Gets the XMPP domain of the connected server.
|
<T extends ExtensionManager> |
getExtensionManager(Class<T> clazz)
Deprecated.
|
<T extends Manager> |
getManager(Class<T> clazz)
Gets an instance of the specified manager class.
|
PresenceManager |
getPresenceManager()
Deprecated.
Use
getManager(Class) instead. |
ReconnectionManager |
getReconnectionManager()
Deprecated.
Use
getManager(Class) instead. |
RosterManager |
getRosterManager()
Deprecated.
Use
getManager(Class) instead. |
XmppSession.Status |
getStatus()
Gets the status of the session.
|
StreamFeaturesManager |
getStreamFeaturesManager()
Deprecated.
Use
getManager(Class) instead. |
boolean |
handleElement(Object element)
Handles an XMPP element.
|
boolean |
isAnonymous()
Indicates whether the session has been logged in anonymously.
|
boolean |
isConnected()
Indicates, whether the session is connected.
|
void |
login(String authorizationId,
CallbackHandler callbackHandler,
String resource)
Authenticates against the server with a custom callback handler and binds a resource.
|
void |
login(String user,
String password)
Authenticates against the server and binds a random resource (assigned by the server).
|
void |
login(String user,
String password,
String resource)
Authenticates against the server with username/password credential and binds a resource.
|
void |
login(String authorizationId,
String user,
String password,
String resource)
Authenticates against the server with an authorization id and username/password credential and binds a resource.
|
void |
loginAnonymously()
Logs in anonymously and binds a resource.
|
void |
notifyException(Exception e)
Called if any unhandled exception is thrown during reading or writing.
|
IQ |
query(IQ iq)
Sends an
<iq/> stanza and waits for the response. |
IQ |
query(IQ iq,
long timeout)
Sends an
<iq/> stanza and waits for the response. |
void |
reconnect()
Deprecated.
Use
connect() which automatically performs a connect+login, if previously logged in. |
void |
removeInboundIQListener(IQListener iqListener)
Removes a previously added inbound IQ listener from the session.
|
void |
removeInboundMessageListener(MessageListener messageListener)
Removes a previously added inbound message listener from the session.
|
void |
removeInboundPresenceListener(PresenceListener presenceListener)
Removes a previously added inbound presence listener from the session.
|
void |
removeIQHandler(Class<?> type)
Removes an IQ handler.
|
void |
removeIQListener(IQListener iqListener)
|
void |
removeMessageListener(MessageListener messageListener)
|
void |
removeOutboundIQListener(IQListener iqListener)
Removes a previously added outbound IQ listener from the session.
|
void |
removeOutboundMessageListener(MessageListener messageListener)
Removes a previously added outbound message listener from the session.
|
void |
removeOutboundPresenceListener(PresenceListener presenceListener)
Removes a previously added outbound presence listener from the session.
|
void |
removePresenceListener(PresenceListener presenceListener)
|
void |
removeSessionStatusListener(SessionStatusListener sessionStatusListener)
Removes a previously added session listener.
|
void |
send(ClientStreamElement element)
Sends an XML element to the server, usually a stanza, i.e. a message, presence or IQ.
|
Message |
sendAndAwaitMessage(ClientStreamElement stanza,
StanzaFilter<Message> filter)
Sends a stanza and then waits for a message stanza to arrive.
|
Presence |
sendAndAwaitPresence(ClientStreamElement stanza,
StanzaFilter<Presence> filter)
Sends a stanza and then waits for a presence stanza to arrive.
|
void |
setXmppServiceDomain(String xmppServiceDomain)
Sets the XMPP service domain.
|
public XmppSession(String xmppServiceDomain, ConnectionConfiguration... connectionConfigurations)
xmppServiceDomain - The service domain.connectionConfigurations - The connection configurations.public XmppSession(String xmppServiceDomain, XmppSessionConfiguration configuration, ConnectionConfiguration... connectionConfigurations)
xmppServiceDomain - The service domain.configuration - The configuration.connectionConfigurations - The connection configurations.public final Connection getActiveConnection()
public final void setXmppServiceDomain(String xmppServiceDomain)
xmppServiceDomain - The XMPP service domain.@Deprecated public final void addMessageListener(MessageListener messageListener)
addInboundMessageListener(rocks.xmpp.core.stanza.MessageListener) or addOutboundMessageListener(rocks.xmpp.core.stanza.MessageListener)messageListener - The message listener.removeMessageListener(MessageListener)@Deprecated public final void removeMessageListener(MessageListener messageListener)
removeInboundMessageListener(rocks.xmpp.core.stanza.MessageListener) or removeOutboundMessageListener(rocks.xmpp.core.stanza.MessageListener)messageListener - The message listener.addMessageListener(MessageListener)public final void addInboundMessageListener(MessageListener messageListener)
messageListener - The message listener.removeInboundMessageListener(MessageListener)public final void removeInboundMessageListener(MessageListener messageListener)
messageListener - The message listener.addInboundMessageListener(MessageListener)public final void addOutboundMessageListener(MessageListener messageListener)
messageListener - The message listener.removeOutboundMessageListener(MessageListener)public final void removeOutboundMessageListener(MessageListener messageListener)
messageListener - The message listener.addOutboundMessageListener(MessageListener)@Deprecated public final void addPresenceListener(PresenceListener presenceListener)
addInboundPresenceListener(rocks.xmpp.core.stanza.PresenceListener) or addOutboundPresenceListener(rocks.xmpp.core.stanza.PresenceListener)presenceListener - The presence listener.removePresenceListener(PresenceListener)@Deprecated public final void removePresenceListener(PresenceListener presenceListener)
removeInboundPresenceListener(rocks.xmpp.core.stanza.PresenceListener) or removeOutboundPresenceListener(rocks.xmpp.core.stanza.PresenceListener)presenceListener - The presence listener.addPresenceListener(PresenceListener)public final void addInboundPresenceListener(PresenceListener presenceListener)
presenceListener - The presence listener.removeInboundPresenceListener(PresenceListener)public final void removeInboundPresenceListener(PresenceListener presenceListener)
presenceListener - The presence listener.addInboundPresenceListener(PresenceListener)public final void addOutboundPresenceListener(PresenceListener presenceListener)
presenceListener - The presence listener.removeOutboundPresenceListener(PresenceListener)public final void removeOutboundPresenceListener(PresenceListener presenceListener)
presenceListener - The presence listener.addOutboundPresenceListener(PresenceListener)@Deprecated public final void addIQListener(IQListener iqListener)
removeInboundIQListener(rocks.xmpp.core.stanza.IQListener) or removeOutboundIQListener(rocks.xmpp.core.stanza.IQListener)iqListener - The IQ listener.removeIQListener(IQListener)@Deprecated public final void removeIQListener(IQListener iqListener)
removeInboundIQListener(rocks.xmpp.core.stanza.IQListener) or removeOutboundIQListener(rocks.xmpp.core.stanza.IQListener)iqListener - The IQ listener.addIQListener(IQListener)public final void addInboundIQListener(IQListener iqListener)
iqListener - The IQ listener.removeInboundIQListener(IQListener)public final void removeInboundIQListener(IQListener iqListener)
iqListener - The IQ listener.addInboundIQListener(IQListener)public final void addOutboundIQListener(IQListener iqListener)
iqListener - The IQ listener.removeOutboundIQListener(IQListener)public final void removeOutboundIQListener(IQListener iqListener)
iqListener - The IQ listener.addOutboundIQListener(IQListener)public final void addIQHandler(Class<?> type, IQHandler iqHandler)
type - The payload type.iqHandler - The IQ handler.removeIQHandler(Class),
addIQHandler(Class, rocks.xmpp.core.stanza.IQHandler, boolean)public final void addIQHandler(Class<?> type, IQHandler iqHandler, boolean invokeAsync)
type - The payload type.iqHandler - The IQ handler.invokeAsync - True, if the handler should be processed asynchronously; false, if the handler should be processed asynchronously.removeIQHandler(Class)public final void removeIQHandler(Class<?> type)
type - The payload type.addIQHandler(Class, rocks.xmpp.core.stanza.IQHandler)public final void addSessionStatusListener(SessionStatusListener sessionStatusListener)
sessionStatusListener - The session listener.removeSessionStatusListener(SessionStatusListener)public final void removeSessionStatusListener(SessionStatusListener sessionStatusListener)
sessionStatusListener - The session listener.addSessionStatusListener(SessionStatusListener)public IQ query(IQ iq) throws XmppException
<iq/> stanza and waits for the response.
This method blocks until a result was received or a timeout occurred.
iq - The <iq/> stanza, which must be of type get or set.<iq/> stanza.StanzaException - If the entity returned a stanza error.NoResponseException - If the entity did not respond.XmppExceptionpublic IQ query(IQ iq, long timeout) throws XmppException
<iq/> stanza and waits for the response.
This method blocks until a result was received or a timeout occurred.
iq - The <iq/> stanza, which must be of type get or set.timeout - The timeout.<iq/> stanza.StanzaException - If the entity returned a stanza error.NoResponseException - If the entity did not respond.XmppExceptionpublic final Presence sendAndAwaitPresence(ClientStreamElement stanza, StanzaFilter<Presence> filter) throws XmppException
stanza - The stanza, which is sent.filter - The presence filter.StanzaException - If the entity returned a stanza error.NoResponseException - If the entity did not respond.XmppExceptionpublic final Message sendAndAwaitMessage(ClientStreamElement stanza, StanzaFilter<Message> filter) throws XmppException
stanza - The stanza, which is sent.filter - The message filter.StanzaException - If the entity returned a stanza error.NoResponseException - If the entity did not respond.XmppException@Deprecated public final void reconnect() throws XmppException
connect() which automatically performs a connect+login, if previously logged in.ConnectionException - If a connection error occurred on the transport layer, e.g. the socket could not connect.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.AuthenticationException - If the login failed, due to a SASL error reported by the server.XmppException - If any other XMPP exception occurs.public final void connect()
throws XmppException
ConnectionException - If a connection error occurred on the transport layer, e.g. the socket could not connect.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.XmppException - If any other XMPP exception occurs.IllegalStateException - If the session is in a wrong state, e.g. closed or already connected.public final void connect(Jid from) throws XmppException
from - The 'from' attribute.ConnectionException - If a connection error occurred on the transport layer, e.g. the socket could not connect.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.XmppException - If any other XMPP exception occurs.IllegalStateException - If the session is in a wrong state, e.g. closed or already connected.public void close()
throws XmppException
close in interface AutoCloseableXmppException - If an exception occurs while closing the connection, e.g. the underlying socket connection.public void send(ClientStreamElement element)
element - The XML element.public final void login(String user, String password) throws XmppException
user - The user name. Usually this is the local part of the user's JID. Must not be null.password - The password. Must not be null.AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding or roster retrieval.XmppException - If the login failed, due to another error.public final void login(String user, String password, String resource) throws XmppException
user - The user name. Usually this is the local part of the user's JID. Must not be null.password - The password. Must not be null.resource - The resource. If null or empty, the resource is randomly assigned by the server.AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding or roster retrieval.XmppException - If the login failed, due to another error.public final void login(String authorizationId, String user, String password, String resource) throws XmppException
authorizationId - The authorization id.user - The user name. Usually this is the local part of the user's JID. Must not be null.password - The password. Must not be null.resource - The resource. If null or empty, the resource is randomly assigned by the server.AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding or roster retrieval.XmppException - If the login failed, due to another error.public final void login(String authorizationId, CallbackHandler callbackHandler, String resource) throws XmppException
authorizationId - The authorization id.callbackHandler - The callback handler.resource - The resource. If null or empty, the resource is randomly assigned by the server.AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding or roster retrieval.XmppException - If the login failed, due to another error.public final void loginAnonymously()
throws XmppException
AuthenticationException - If the login failed, due to a SASL error reported by the server.StreamErrorException - If the server returned a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.NoResponseException - If the server didn't return a response during stream establishment.StanzaException - If the server returned a stanza error during resource binding.XmppException - If the login failed, due to another error.public final boolean handleElement(Object element) throws XmppException
This method should be called on the reader thread.
element - The XMPP element.StreamErrorException - If the element is a stream error.StreamNegotiationException - If any exception occurred during stream feature negotiation.XmppException - If any other XMPP exception occurs.public final void notifyException(Exception e)
This method will close the stream.
e - The exception. If an unrecoverable XMPP stream error occurred, the exception is a StreamError.@Deprecated public final <T extends ExtensionManager> T getExtensionManager(Class<T> clazz)
getManager(Class)XmppSession.T - The type.clazz - The class of the extension manager.public final <T extends Manager> T getManager(Class<T> clazz)
XmppSession.T - The type.clazz - The class of the manager.@Deprecated public final RosterManager getRosterManager()
getManager(Class) instead.@Deprecated public final PresenceManager getPresenceManager()
getManager(Class) instead.@Deprecated public final ReconnectionManager getReconnectionManager()
getManager(Class) instead.@Deprecated public final StreamFeaturesManager getStreamFeaturesManager()
getManager(Class) instead.@Deprecated public final ChatManager getChatManager()
getManager(Class) instead.public final Jid getConnectedResource()
After a client has bound a resource to the stream, it is referred to as a "connected resource".
public final String getDomain()
public final XmppSession.Status getStatus()
public final List<Connection> getConnections()
public final Unmarshaller createUnmarshaller()
Note that the returned unmarshaller is not thread-safe.
createMarshaller()public final Marshaller createMarshaller()
The returned marshaller is configured with Marshaller.JAXB_FRAGMENT = true, so that no XML header is written
(which is usually what we want in XMPP when writing stanzas).
Note that the returned unmarshaller is not thread-safe.
createUnmarshaller()@Deprecated public final int getDefaultTimeout()
XmppSessionConfiguration.getDefaultResponseTimeout().public final boolean isConnected()
XmppSession.Status.CONNECTED, XmppSession.Status.AUTHENTICATED or XmppSession.Status.AUTHENTICATING.getStatus()public final boolean isAnonymous()
public final XmppSessionConfiguration getConfiguration()
public final XmppDebugger getDebugger()
XmppSessionConfiguration.getDebugger()Copyright © 2014–2015 XMPP.rocks. All rights reserved.