public abstract class XmppSession extends Object implements AutoCloseable
To date there are three kinds of sessions:
XmppClient.This class provides the common functionality and abstract methods for connection establishment, sending and receiving XML stanzas, closing the session, etc.
Concrete implementations may have different concepts for authentication, e.g. normal C2S sessions use SASL, while the Jabber Component Protocol uses a different kind of handshake for authenticating.| Modifier and Type | Class and Description |
|---|---|
static class |
XmppSession.Status
Represents the session status.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addConnectionListener(Consumer<ConnectionEvent> connectionListener)
Adds a connection listener, which is triggered, when the connection is disconnected or reconnected.
|
void |
addInboundIQListener(Consumer<IQEvent> iqListener)
Adds an inbound IQ listener to the session, which will get notified, whenever an IQ stanza is received.
|
void |
addInboundMessageListener(Consumer<MessageEvent> messageListener)
Adds an inbound message listener to the session, which will get notified, whenever a message is received.
|
void |
addInboundPresenceListener(Consumer<PresenceEvent> 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 |
addOutboundIQListener(Consumer<IQEvent> iqListener)
Adds an outbound IQ listener to the session, which will get notified, whenever an IQ stanza is sent.
|
void |
addOutboundMessageListener(Consumer<MessageEvent> messageListener)
Adds an outbound message listener to the session, which will get notified, whenever a message is sent.
|
void |
addOutboundPresenceListener(Consumer<PresenceEvent> presenceListener)
Adds an outbound presence listener to the session, which will get notified, whenever a presence is sent.
|
void |
addSessionStatusListener(Consumer<SessionStatusEvent> 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.
|
abstract void |
connect(Jid from) |
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.
|
void |
disableFeature(Class<? extends Manager> managerClass)
Disables a feature by its manager class.
|
void |
disableFeature(String name)
Disables a feature by its name, usually a protocol namespace.
|
void |
enableFeature(Class<? extends Manager> managerClass)
Enables a feature by its manager class.
|
void |
enableFeature(String name)
Enables a feature by its name, usually a protocol namespace.
|
Connection |
getActiveConnection()
Gets the actively used connection.
|
XmppSessionConfiguration |
getConfiguration()
Gets the configuration for this session.
|
abstract Jid |
getConnectedResource()
The connected resource, which is assigned by the server.
|
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.
|
Jid |
getDomain()
Gets the XMPP domain of the connected server.
|
Set<String> |
getEnabledFeatures()
Gets the enabled features.
|
<T extends Manager> |
getManager(Class<T> clazz)
Gets an instance of the specified manager class.
|
XmppSession.Status |
getStatus()
Gets the status of the session.
|
boolean |
handleElement(Object element)
Handles an XMPP element.
|
boolean |
isConnected()
Indicates, whether the session is connected.
|
void |
notifyException(Throwable 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 |
removeConnectionListener(Consumer<ConnectionEvent> connectionListener)
Removes a previously added connection listener.
|
void |
removeInboundIQListener(Consumer<IQEvent> iqListener)
Removes a previously added inbound IQ listener from the session.
|
void |
removeInboundMessageListener(Consumer<MessageEvent> messageListener)
Removes a previously added inbound message listener from the session.
|
void |
removeInboundPresenceListener(Consumer<PresenceEvent> presenceListener)
Removes a previously added inbound presence listener from the session.
|
void |
removeIQHandler(Class<?> type)
Removes an IQ handler.
|
void |
removeOutboundIQListener(Consumer<IQEvent> iqListener)
Removes a previously added outbound IQ listener from the session.
|
void |
removeOutboundMessageListener(Consumer<MessageEvent> messageListener)
Removes a previously added outbound message listener from the session.
|
void |
removeOutboundPresenceListener(Consumer<PresenceEvent> presenceListener)
Removes a previously added outbound presence listener from the session.
|
void |
removeSessionStatusListener(Consumer<SessionStatusEvent> sessionStatusListener)
Removes a previously added session listener.
|
StreamElement |
send(StreamElement element)
Sends an XML element to the server, usually a stanza, i.e. a message, presence or IQ.
|
Message |
sendAndAwaitMessage(StreamElement stanza,
Predicate<Message> filter)
Sends a stanza and then waits for a message stanza to arrive.
|
Presence |
sendAndAwaitPresence(StreamElement stanza,
Predicate<Presence> filter)
Sends a stanza and then waits for a presence stanza to arrive.
|
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 abstract void connect(Jid from) throws XmppException
XmppExceptionpublic final void addInboundMessageListener(Consumer<MessageEvent> messageListener)
messageListener - The message listener.removeInboundMessageListener(Consumer)public final void removeInboundMessageListener(Consumer<MessageEvent> messageListener)
messageListener - The message listener.addInboundMessageListener(Consumer)public final void addOutboundMessageListener(Consumer<MessageEvent> messageListener)
messageListener - The message listener.removeOutboundMessageListener(Consumer)public final void removeOutboundMessageListener(Consumer<MessageEvent> messageListener)
messageListener - The message listener.addOutboundMessageListener(Consumer)public final void addInboundPresenceListener(Consumer<PresenceEvent> presenceListener)
presenceListener - The presence listener.removeInboundPresenceListener(Consumer)public final void removeInboundPresenceListener(Consumer<PresenceEvent> presenceListener)
presenceListener - The presence listener.addInboundPresenceListener(Consumer)public final void addOutboundPresenceListener(Consumer<PresenceEvent> presenceListener)
presenceListener - The presence listener.removeOutboundPresenceListener(Consumer)public final void removeOutboundPresenceListener(Consumer<PresenceEvent> presenceListener)
presenceListener - The presence listener.addOutboundPresenceListener(Consumer)public final void addInboundIQListener(Consumer<IQEvent> iqListener)
iqListener - The IQ listener.removeInboundIQListener(Consumer)public final void removeInboundIQListener(Consumer<IQEvent> iqListener)
iqListener - The IQ listener.addInboundIQListener(Consumer)public final void addOutboundIQListener(Consumer<IQEvent> iqListener)
iqListener - The IQ listener.removeOutboundIQListener(Consumer)public final void removeOutboundIQListener(Consumer<IQEvent> iqListener)
iqListener - The IQ listener.addOutboundIQListener(Consumer)public final void addIQHandler(Class<?> type, IQHandler iqHandler)
type - The payload type.iqHandler - The IQ handler.removeIQHandler(Class),
addIQHandler(Class, 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, IQHandler)public final void addSessionStatusListener(Consumer<SessionStatusEvent> sessionStatusListener)
sessionStatusListener - The session listener.removeSessionStatusListener(Consumer)public final void removeSessionStatusListener(Consumer<SessionStatusEvent> sessionStatusListener)
sessionStatusListener - The session listener.addSessionStatusListener(Consumer)public final void addConnectionListener(Consumer<ConnectionEvent> connectionListener)
connectionListener - The connection listener.removeConnectionListener(Consumer)public final void removeConnectionListener(Consumer<ConnectionEvent> connectionListener)
connectionListener - The connection listener.addConnectionListener(Consumer)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(StreamElement stanza, Predicate<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(StreamElement stanza, Predicate<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.XmppExceptionpublic final Connection getActiveConnection()
public StreamElement send(StreamElement element)
element - The XML element.Message is translated to a ClientMessage.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).
createUnmarshaller()public final boolean isConnected()
XmppSession.Status.CONNECTED, XmppSession.Status.AUTHENTICATED or XmppSession.Status.AUTHENTICATING.getStatus()public 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 <T extends Manager> T getManager(Class<T> clazz)
XmppSession.T - The type.clazz - The class of the manager.public final void close()
throws XmppException
close in interface AutoCloseableXmppException - If an exception occurs while closing the connection, e.g. the underlying socket connection.public void notifyException(Throwable e)
This method will close the stream.
e - The exception. If an unrecoverable XMPP stream error occurred, the exception is a StreamError.public final Jid getDomain()
public final XmppSessionConfiguration getConfiguration()
public final XmppDebugger getDebugger()
XmppSessionConfiguration.getDebugger()public final void enableFeature(String name)
name - The associated manager class.public final void disableFeature(String name)
name - The associated manager class.public final void enableFeature(Class<? extends Manager> managerClass)
managerClass - The associated manager class.public final void disableFeature(Class<? extends Manager> managerClass)
managerClass - The associated manager class.public final Set<String> getEnabledFeatures()
public abstract Jid getConnectedResource()
Copyright © 2014–2015 XMPP.rocks. All rights reserved.