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 used by this session is disconnected or reconnected.
|
static void |
addCreationListener(Consumer<XmppSession> listener)
Adds a listener, which is triggered, whenever a new session is created.
|
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 |
addMessageAcknowledgedListener(Consumer<MessageEvent> messageListener)
Adds a listener, which gets called, when the server acknowledges the receipt of a message.
|
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 |
addSendFailedListener(BiConsumer<StreamElement,Throwable> sendFailedListener)
Adds a listener, which gets called, whenever a stream element (e.g. message) has been sent unsuccessfully.
|
void |
addSendSucceededListener(Consumer<StreamElement> sendSucceededListener)
Adds a listener, which gets called, whenever a stream element (e.g. message) has been sent successfully.
|
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(rocks.xmpp.addr.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 rocks.xmpp.addr.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.
|
rocks.xmpp.addr.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.
|
Queue<Stanza> |
getUnacknowledgedStanzas() |
boolean |
handleElement(Object element)
Handles an XMPP element.
|
boolean |
isAuthenticated()
Returns true, if the session is authenticated.
|
boolean |
isConnected()
Indicates, whether the session is connected.
|
void |
markAcknowledged(Stanza acknowledgedStanza)
Marks a stanza as acknowledged.
|
void |
notifyException(Throwable e)
Called if any unhandled exception is thrown during reading or writing.
|
AsyncResult<IQ> |
query(IQ iq)
Sends an
<iq/> stanza and returns an async result, which can be used to wait for the response. |
<T> AsyncResult<T> |
query(IQ iq,
Class<T> clazz)
Sends an
<iq/> stanza and returns an async result, which can be used to wait for the response. |
AsyncResult<IQ> |
query(IQ iq,
Duration timeout)
Sends an
<iq/> stanza and returns an async result, which can be used to wait for the response. |
void |
removeConnectionListener(Consumer<ConnectionEvent> connectionListener)
Removes a previously added connection listener.
|
static void |
removeCreationListener(Consumer<XmppSession> listener)
Removes a previously added creation 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 |
removeMessageAcknowledgedListener(Consumer<MessageEvent> messageListener)
Removes a previously added message acknowledge listener.
|
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 |
removeSendFailedListener(BiConsumer<StreamElement,Throwable> sendFailedListener)
Removes a previously added send failed listener.
|
void |
removeSendSucceededListener(Consumer<StreamElement> sendSucceededListener)
Removes a previously added send succeeded listener.
|
void |
removeSessionStatusListener(Consumer<SessionStatusEvent> sessionStatusListener)
Removes a previously added session listener.
|
Future<Void> |
send(StreamElement element)
Sends an XML element to the server, usually a stanza, i.e. a message, presence or IQ.
|
AsyncResult<Message> |
sendAndAwaitMessage(Message stanza,
Predicate<Message> filter)
Sends a stanza and returns an async result which can wait for the message stanza, which matches the predicate, to arrive.
|
AsyncResult<Presence> |
sendAndAwaitPresence(Presence stanza,
Predicate<Presence> filter)
Sends a stanza and returns an async result which can wait for the presence stanza, which matches the predicate, to arrive.
|
SendTask<IQ> |
sendIQ(IQ iq)
Sends an IQ.
|
SendTask<Message> |
sendMessage(Message message)
Sends a message.
|
SendTask<Presence> |
sendPresence(Presence presence)
Sends a presence.
|
public static void addCreationListener(Consumer<XmppSession> listener)
listener - The listener.removeCreationListener(Consumer)public static void removeCreationListener(Consumer<XmppSession> listener)
listener - The listener.addCreationListener(Consumer)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(rocks.xmpp.addr.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 addMessageAcknowledgedListener(Consumer<MessageEvent> messageListener)
messageListener - The message listener.removeMessageAcknowledgedListener(Consumer)public final void removeMessageAcknowledgedListener(Consumer<MessageEvent> messageListener)
messageListener - The message listener.addMessageAcknowledgedListener(Consumer)public final void addSendSucceededListener(Consumer<StreamElement> sendSucceededListener)
sendSucceededListener - The listener.removeSendSucceededListener(Consumer)public final void removeSendSucceededListener(Consumer<StreamElement> sendSucceededListener)
sendSucceededListener - The listener.addSendSucceededListener(Consumer)public final void addSendFailedListener(BiConsumer<StreamElement,Throwable> sendFailedListener)
sendFailedListener - The listener.removeSendFailedListener(BiConsumer)public final void removeSendFailedListener(BiConsumer<StreamElement,Throwable> sendFailedListener)
sendFailedListener - The listener.addSendFailedListener(BiConsumer),
addSendSucceededListener(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 final AsyncResult<IQ> query(IQ iq)
<iq/> stanza and returns an async result, which can be used to wait for the response. The result is completed if the response IQ has arrived or the default timeout has exceeded, in which case the result completes with a NoResponseException.
public final AsyncResult<IQ> query(IQ iq, Duration timeout)
<iq/> stanza and returns an async result, which can be used to wait for the response. The result is completed if the response IQ has arrived or the timeout has exceeded, in which case the result completes with a NoResponseException.public final <T> AsyncResult<T> query(IQ iq, Class<T> clazz)
<iq/> stanza and returns an async result, which can be used to wait for the response. The result is completed if the response IQ has arrived or the default timeout has exceeded, in which case the result completes with a NoResponseException.
The payload of the response IQ is returned in the async result class.public final AsyncResult<Presence> sendAndAwaitPresence(Presence stanza, Predicate<Presence> filter)
stanza - The stanza, which is sent.filter - The presence filter.public final AsyncResult<Message> sendAndAwaitMessage(Message stanza, Predicate<Message> filter)
stanza - The stanza, which is sent.filter - The message filter.public final Connection getActiveConnection()
public Future<Void> send(StreamElement element)
element - The XML element.Message is translated to a ClientMessage.public SendTask<IQ> sendIQ(IQ iq)
iq - The IQ.public SendTask<Message> sendMessage(Message message)
message - The message.public SendTask<Presence> sendPresence(Presence presence)
presence - The presence.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 final boolean isAuthenticated()
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 rocks.xmpp.addr.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 rocks.xmpp.addr.Jid getConnectedResource()
public final void markAcknowledged(Stanza acknowledgedStanza)
acknowledgedStanza - The acknowledged stanza.addMessageAcknowledgedListener(Consumer)Copyright © 2014–2017 XMPP.rocks. All rights reserved.