Class AbstractXMPPConnection
- All Implemented Interfaces:
XMPPConnection
- Direct Known Subclasses:
ModularXmppClientToServerConnection
,XMPPBOSHConnection
,XMPPTCPConnection
connect()
, login()
and
disconnect()
(which are deliberately not provided by the XMPPConnection
interface).
Note: The default entry point to Smack's documentation is XMPPConnection
. If you are getting started
with Smack, then head over to XMPPConnection
and the come back here.
Parsing Exceptions
In case a Smack parser (Provider) throws those exceptions are handled over to the ParsingExceptionCallback
. A
common cause for a provider throwing is illegal input, for example a non-numeric String where only Integers are
allowed. Smack's default behavior follows the "fail-hard per default" principle leading to a
termination of the connection on parsing exceptions. This default was chosen to make users eventually aware that they
should configure their own callback and handle those exceptions to prevent the disconnect. Handle a parsing exception
could be as simple as using a non-throwing no-op callback, which would cause the faulty stream element to be taken
out of the stream, i.e., Smack behaves like that element was never received.
If the parsing exception is because Smack received illegal input, then please consider informing the authors of the originating entity about that. If it was thrown because of an bug in a Smack parser, then please consider filling a bug with Smack.
Managing the parsing exception callback
The "fail-hard per default" behavior is achieved by using the
ExceptionThrowingCallbackWithHint
as default parsing exception callback. You
can change the behavior using setParsingExceptionCallback(ParsingExceptionCallback)
to set a new callback.
Use SmackConfiguration.setDefaultParsingExceptionCallback(ParsingExceptionCallback)
to
set the default callback.
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Deprecated.protected static class
A wrapper class to associate a stanza filter with a listener.protected static enum
Nested classes/interfaces inherited from interface org.jivesoftware.smack.XMPPConnection
XMPPConnection.FromMode
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final AsyncButOrdered<AbstractXMPPConnection>
protected boolean
Flag that indicates if the user is currently authenticated with the server.protected ZonedDateTime
protected boolean
A synchronization point which is successful if this connection has received the closing stream element from the remote end-point, i.e.protected XMPPInputOutputStream
protected final ConnectionConfiguration
Holds the initial configuration used while creating the connection.protected boolean
protected final int
A number to uniquely identify connections that are created.protected final Set<ConnectionListener>
A collection of ConnectionListeners which listen for connection closing and reconnection events.protected final Lock
protected final SmackDebugger
The SmackDebugger allows to log and debug XML traffic.protected String
The used host to establish the connection toprotected boolean
Set totrue
if the last features stanza from the server has been parsed.protected XmlEnvironment
protected UInt16
The used port to establish the connection toprotected Reader
The Reader which is used for the debugger.protected boolean
Set totrue
if the SASL feature has been received.protected static final SmackReactor
protected final Map<QName,
XmlElement> protected String
The stream ID, see RFC 6120 § 4.7.3protected boolean
protected EntityFullJid
The full JID of the authenticated user, as returned by the resource binding response of the server.protected boolean
Flag that indicates if the user was authenticated with the server when the connection to the server was closed (abruptly or not).protected Writer
The Writer which is used for the debugger. -
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractXMPPConnection
(ConnectionConfiguration configuration) Create a new XMPPConnection to an XMPP server. -
Method Summary
Modifier and TypeMethodDescriptionaddAsyncStanzaListener
(StanzaListener packetListener, StanzaFilter packetFilter) Registers an asynchronous stanza listener with this connection.void
addConnectionListener
(ConnectionListener connectionListener) Adds a connection listener to this connection that will be notified when the connection closes or fails.void
addMessageInterceptor
(Consumer<MessageBuilder> messageInterceptor, Predicate<Message> messageFilter) Registers a stanza interceptor with this connection.void
addOneTimeSyncCallback
(StanzaListener callback, StanzaFilter packetFilter) Add a callback that is called exactly once and synchronously with the incoming stanza that matches the given stanza filter.void
addPresenceInterceptor
(Consumer<PresenceBuilder> presenceInterceptor, Predicate<Presence> presenceFilter) Registers a stanza interceptor with this connection.final ListenerHandle
addStanzaListener
(StanzaListener stanzaListener, StanzaFilter stanzaFilter) Registers a stanza listener with this connection.void
addStanzaSendingListener
(StanzaListener packetListener, StanzaFilter packetFilter) Registers a stanza listener with this connection.protected void
addStreamFeature
(XmlElement feature) addSyncStanzaListener
(StanzaListener packetListener, StanzaFilter packetFilter) Registers a synchronous stanza listener with this connection.protected void
protected void
Hook for subclasses right after successful SASL authentication.protected void
afterSuccessfulLogin
(boolean resumed) protected static void
protected void
asyncGoLimited
(Runnable runnable) protected final SASLMechanism
authenticate
(String username, String password, EntityBareJid authzid, SSLSession sslSession) Authenticate a connection.protected Resourcepart
bindResourceAndEstablishSession
(Resourcepart resource) protected final NonzaCallback.Builder
protected void
callConnectionAuthenticatedListener
(boolean resumed) protected void
protected void
connect()
Establishes a connection to the XMPP server.protected abstract void
Abstract method that concrete subclasses of XMPPConnection need to implement to perform their way of XMPP connection establishment.createStanzaCollector
(StanzaFilter packetFilter) Creates a new stanza collector for this connection.createStanzaCollector
(StanzaCollector.Configuration configuration) Create a new stanza collector with the given stanza collector configuration.createStanzaCollectorAndSend
(StanzaFilter packetFilter, Stanza packet) Creates a new stanza collector for this connection.createStanzaCollectorAndSend
(IQ packet) Creates a new stanza collector collecting IQ responses that are replies to the IQrequest
.void
Closes the connection by setting presence to unavailable then closing the connection to the XMPP server.void
disconnect
(Presence unavailablePresence) Closes the connection.protected void
firePacketSendingListeners
(TopLevelStreamElement sendTopLevelStreamElement) Process all stanza listeners for sending stanzas.final ZonedDateTime
Get the timestamp when the connection was the first time authenticated, i.e., when the first successful login was performed.Get the connection configuration used by this connection.int
Get the connection counter of this XMPPConnection instance.protected Lock
<F extends XmlElement>
FgetFeature
(QName qname) Get the feature stanza extensions for a given stream feature of the server, ornull
if the server doesn't support that feature.Get the currently active FromMode.getHost()
Returns the host name of the server where the XMPP server is running.long
Returns the timestamp in milliseconds when the last stanza was received.Get the current active parsing exception callback.int
getPort()
Returns the port number of the XMPP server for this connection.protected final SmackReactor
long
Returns the current value of the reply timeout in milliseconds for request for this XMPPConnection instance.protected final SmackTlsContext
final StanzaFactory
Returns the stream ID for this connection, which is the value set by the server when opening an XMPP stream.protected AbstractStreamOpen
getStreamOpen
(DomainBareJid to, CharSequence from, String id, String lang) final String
Get the name of the SASL mechanism that was used to authenticate this connection.final EntityFullJid
getUser()
Returns the full XMPP address of the user that is logged in to the connection ornull
if not logged in yet.Returns the XMPP Domain of the service provided by the XMPP server and used for this connection.protected final boolean
boolean
hasFeature
(QName qname) Return true if the server supports the given stream feature.protected void
Initialize thedebugger
.protected void
abstract void
Performs an unclean disconnect and shutdown of the connection.protected void
InvokeStanzaCollector.processStanza(Stanza)
for every StanzaCollector with the given packet.final boolean
Returns true if currently authenticated anonymously.final boolean
Returns true if currently authenticated by successfully calling the login method.final boolean
Returns true if currently connected to the XMPP server.protected final boolean
abstract boolean
Returns true if the connection to the server has successfully negotiated encryption.abstract boolean
Returns true if network traffic is being compressed.void
login()
Logs in to the server using the strongest SASL mechanism supported by the server.void
login
(CharSequence username, String password) Same aslogin(CharSequence, String, Resourcepart)
, but takes the resource from the connection configuration.void
login
(CharSequence username, String password, Resourcepart resource) Login with the given username (authorization identity).protected abstract void
loginInternal
(String username, String password, Resourcepart resource) protected final void
notifyConnectionError
(Exception exception) Sends out a notification that there was an error with the connection and closes the connection.protected final void
protected String
onStreamOpen
(XmlPullParser parser) Must be called when a XMPP stream open tag is encountered.protected final void
parseAndProcessNonza
(XmlPullParser parser) protected void
parseAndProcessStanza
(XmlPullParser parser) protected final void
parseFeatures
(XmlPullParser parser) protected final void
parseFeaturesAndNotify
(XmlPullParser parser) protected void
processStanza
(Stanza stanza) Processes a stanza after it's been fully parsed by looping through the installed stanza collectors and listeners and letting them examine the stanza to see if they are a match with the filter.registerIQRequestHandler
(IQRequestHandler iqRequestHandler) Register an IQ request handler with this connection.boolean
removeAsyncStanzaListener
(StanzaListener packetListener) Removes an asynchronous stanza listener for received stanzas from this connection.void
removeConnectionListener
(ConnectionListener connectionListener) Removes a connection listener from this connection.void
removeMessageInterceptor
(Consumer<MessageBuilder> messageInterceptor) Removes a message interceptor.void
removePresenceInterceptor
(Consumer<PresenceBuilder> presenceInterceptor) Removes a presence interceptor.void
removeStanzaCollector
(StanzaCollector collector) Remove a stanza collector of this connection.final boolean
removeStanzaListener
(StanzaListener stanzaListener) Removes a stanza listener for received stanzas from this connection.void
removeStanzaSendingListener
(StanzaListener packetListener) Removes a stanza listener for sending stanzas from this connection.boolean
removeSyncStanzaListener
(StanzaListener packetListener) Removes a stanza listener for received stanzas from this connection.protected static ScheduledAction
sendAndWaitForResponse
(Nonza nonza, Class<SN> successNonzaClass, Class<FN> failedNonzaClass) <S extends Stanza>
SmackFuture<S,Exception> sendAsync
(S stanza, StanzaFilter replyFilter) Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter.<S extends Stanza>
SmackFuture<S,Exception> sendAsync
(S stanza, StanzaFilter replyFilter, long timeout) Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter.protected abstract void
sendInternal
(TopLevelStreamElement element) <I extends IQ>
IsendIqRequestAndWaitForResponse
(IQ request) Send an IQ request and wait for the response.sendIqRequestAsync
(IQ request) Send an IQ request asynchronously.sendIqRequestAsync
(IQ request, long timeout) Send an IQ request asynchronously.protected abstract void
final void
Send a Nonza.final void
sendNonzaNonBlocking
(Nonza nonza) final void
sendStanza
(Stanza stanza) Sends the specified stanza to the server.final void
sendStanzaNonBlocking
(Stanza stanza) protected final void
protected final void
setCurrentConnectionExceptionAndNotify
(Exception exception) void
setFromMode
(XMPPConnection.FromMode fromMode) Set the FromMode for this connection instance.void
setMaxAsyncOperations
(int maxAsyncOperations) void
Install a parsing exception callback, which will be invoked once an exception is encountered while parsing a stanza.void
setReplyTimeout
(long timeout) Set the stanza reply timeout in milliseconds.void
setUnknownIqRequestReplyMode
(SmackConfiguration.UnknownIqRequestReplyMode unknownIqRequestReplyMode) Set how Smack behaves when an unknown IQ request has been received.protected void
Sets whether the connection has already logged in the server.protected abstract void
shutdown()
Shuts the current connection down.protected void
protected void
protected void
protected void
throwNotConnectedExceptionIfAppropriate
(String optionalHint) final String
toString()
boolean
trySendStanza
(Stanza stanza) Try to send the given stanza.boolean
trySendStanza
(Stanza stanza, long timeout, TimeUnit unit) Try to send the given stanza.unregisterIQRequestHandler
(String element, String namespace, IQ.Type type) Unregister an IQ request handler with this connection.final IQRequestHandler
unregisterIQRequestHandler
(IQRequestHandler iqRequestHandler) Convenience method forXMPPConnection.unregisterIQRequestHandler(String, String, org.jivesoftware.smack.packet.IQ.Type)
.protected void
protected final boolean
protected final boolean
protected final void
waitForConditionOrThrowConnectionException
(Supplier<Boolean> condition, String waitFor) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.jivesoftware.smack.XMPPConnection
getFeature, getLocalAddress, hasFeature
-
Field Details
-
SMACK_REACTOR
-
connectionListeners
A collection of ConnectionListeners which listen for connection closing and reconnection events. -
outgoingStreamXmlEnvironment
-
connectionLock
-
streamFeatures
-
user
The full JID of the authenticated user, as returned by the resource binding response of the server.It is important that we don't infer the user from the login() arguments and the configurations service name, as, for example, when SASL External is used, the username is not given to login but taken from the 'external' certificate.
-
connected
-
streamId
The stream ID, see RFC 6120 § 4.7.3 -
debugger
The SmackDebugger allows to log and debug XML traffic. -
reader
The Reader which is used for the debugger. -
writer
The Writer which is used for the debugger. -
tlsHandled
-
lastFeaturesReceived
Set totrue
if the last features stanza from the server has been parsed. A XMPP connection handshake can invoke multiple features stanzas, e.g. when TLS is activated a second feature stanza is send by the server. This is set to true once the last feature stanza has been parsed. -
saslFeatureReceived
Set totrue
if the SASL feature has been received. -
closingStreamReceived
A synchronization point which is successful if this connection has received the closing stream element from the remote end-point, i.e. the server. -
connectionCounterValue
A number to uniquely identify connections that are created. This is distinct from the connection ID, which is a value sent by the server once a connection is made. -
config
Holds the initial configuration used while creating the connection. -
compressionHandler
-
ASYNC_BUT_ORDERED
-
host
The used host to establish the connection to -
port
The used port to establish the connection to -
authenticated
Flag that indicates if the user is currently authenticated with the server. -
authenticatedConnectionInitiallyEstablishedTimestamp
-
wasAuthenticated
Flag that indicates if the user was authenticated with the server when the connection to the server was closed (abruptly or not).
-
-
Constructor Details
-
AbstractXMPPConnection
Create a new XMPPConnection to an XMPP server.- Parameters:
configuration
- The configuration which is used to establish the connection.
-
-
Method Details
-
getConfiguration
Get the connection configuration used by this connection.- Returns:
- the connection configuration.
-
getXMPPServiceDomain
Description copied from interface:XMPPConnection
Returns the XMPP Domain of the service provided by the XMPP server and used for this connection. After authenticating with the server the returned value may be different.- Specified by:
getXMPPServiceDomain
in interfaceXMPPConnection
- Returns:
- the XMPP domain of this XMPP session.
-
getHost
Description copied from interface:XMPPConnection
Returns the host name of the server where the XMPP server is running. This would be the IP address of the server or a name that may be resolved by a DNS server.- Specified by:
getHost
in interfaceXMPPConnection
- Returns:
- the host name of the server where the XMPP server is running or null if not yet connected.
-
getPort
Description copied from interface:XMPPConnection
Returns the port number of the XMPP server for this connection. The default port for normal connections is 5222.- Specified by:
getPort
in interfaceXMPPConnection
- Returns:
- the port number of the XMPP server or 0 if not yet connected.
-
isSecureConnection
Description copied from interface:XMPPConnection
Returns true if the connection to the server has successfully negotiated encryption.- Specified by:
isSecureConnection
in interfaceXMPPConnection
- Returns:
- true if a secure connection to the server.
-
sendInternal
protected abstract void sendInternal(TopLevelStreamElement element) throws SmackException.NotConnectedException, InterruptedException -
sendNonBlockingInternal
protected abstract void sendNonBlockingInternal(TopLevelStreamElement element) throws SmackException.NotConnectedException, SmackException.OutgoingQueueFullException -
trySendStanza
Description copied from interface:XMPPConnection
Try to send the given stanza. Returnstrue
if the stanza was successfully put into the outgoing stanza queue, otherwise, iffalse
is returned, the stanza could not be scheduled for sending (for example because the outgoing element queue is full). Note that this means that the stanza possibly was not put onto the wire, even iftrue
is returned, it just has been successfully scheduled for sending.Note: Implementations are not required to provide that functionality. In that case this method is mapped to
XMPPConnection.sendStanza(Stanza)
and will possibly block until the stanza could be scheduled for sending.- Specified by:
trySendStanza
in interfaceXMPPConnection
- Parameters:
stanza
- the stanza to send.- Returns:
true
if the stanza was successfully scheduled to be sent,false
otherwise.- Throws:
SmackException.NotConnectedException
- if the connection is not connected.
-
trySendStanza
public boolean trySendStanza(Stanza stanza, long timeout, TimeUnit unit) throws SmackException.NotConnectedException, InterruptedException Description copied from interface:XMPPConnection
Try to send the given stanza. Returnstrue
if the stanza was successfully put into the outgoing stanza queue within the given timeout period, otherwise, iffalse
is returned, the stanza could not be scheduled for sending (for example because the outgoing element queue is full). Note that this means that the stanza possibly was not put onto the wire, even iftrue
is returned, it just has been successfully scheduled for sending.Note: Implementations are not required to provide that functionality. In that case this method is mapped to
XMPPConnection.sendStanza(Stanza)
and will possibly block until the stanza could be scheduled for sending.- Specified by:
trySendStanza
in interfaceXMPPConnection
- Parameters:
stanza
- the stanza to send.timeout
- how long to wait before giving up, in units ofunit
.unit
- aTimeUnit
determining how to interpret thetimeout
parameter.- Returns:
true
if the stanza was successfully scheduled to be sent,false
otherwise.- Throws:
SmackException.NotConnectedException
- if the connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
sendNonza
public final void sendNonza(Nonza nonza) throws SmackException.NotConnectedException, InterruptedException Description copied from interface:XMPPConnection
Send a Nonza.This method is not meant for end-user usage! It allows sending plain stream elements, which should not be done by a user manually. Doing so may result in a unstable or unusable connection. Certain Smack APIs use this method to send plain stream elements.
- Specified by:
sendNonza
in interfaceXMPPConnection
- Parameters:
nonza
- the Nonza to send.- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
sendNonzaNonBlocking
public final void sendNonzaNonBlocking(Nonza nonza) throws SmackException.NotConnectedException, SmackException.OutgoingQueueFullException - Specified by:
sendNonzaNonBlocking
in interfaceXMPPConnection
- Throws:
SmackException.NotConnectedException
SmackException.OutgoingQueueFullException
-
isUsingCompression
Description copied from interface:XMPPConnection
Returns true if network traffic is being compressed. When using stream compression network traffic can be reduced up to 90%. Therefore, stream compression is ideal when using a slow speed network connection. However, the server will need to use more CPU time in order to un/compress network data so under high load the server performance might be affected.- Specified by:
isUsingCompression
in interfaceXMPPConnection
- Returns:
- true if network traffic is being compressed.
-
initState
-
connect
public AbstractXMPPConnection connect() throws SmackException, IOException, XMPPException, InterruptedExceptionEstablishes a connection to the XMPP server. It basically creates and maintains a connection to the server.Listeners will be preserved from a previous connection.
- Returns:
- a reference to this object, to chain
connect()
withlogin()
. - Throws:
XMPPException
- if an error occurs on the XMPP protocol level.SmackException
- if an error occurs somewhere else besides XMPP protocol level.IOException
- if an I/O error occurred.InterruptedException
- if the calling thread was interrupted.
-
connectInternal
protected abstract void connectInternal() throws SmackException, IOException, XMPPException, InterruptedExceptionAbstract method that concrete subclasses of XMPPConnection need to implement to perform their way of XMPP connection establishment. Implementations are required to perform an automatic login if the previous connection state was logged (authenticated).- Throws:
SmackException
- if Smack detected an exceptional situation.IOException
- if an I/O error occurred.XMPPException
- if an XMPP protocol error was received.InterruptedException
- if the calling thread was interrupted.
-
login
Logs in to the server using the strongest SASL mechanism supported by the server. If more than the connection's default stanza timeout elapses in each step of the authentication process without a response from the server, aSmackException.NoResponseException
will be thrown.Before logging in (i.e. authenticate) to the server the connection must be connected by calling
connect()
.It is possible to log in without sending an initial available presence by using
ConnectionConfiguration.Builder.setSendPresence(boolean)
. Finally, if you want to not pass a password and instead use a more advanced mechanism while using SASL then you may be interested in usingConnectionConfiguration.Builder.setCallbackHandler(javax.security.auth.callback.CallbackHandler)
. For more advanced login settings seeConnectionConfiguration
.- Throws:
XMPPException
- if an error occurs on the XMPP protocol level.SmackException
- if an error occurs somewhere else besides XMPP protocol level.IOException
- if an I/O error occurs during login.InterruptedException
- if the calling thread was interrupted.
-
login
public void login(CharSequence username, String password) throws XMPPException, SmackException, IOException, InterruptedException Same aslogin(CharSequence, String, Resourcepart)
, but takes the resource from the connection configuration.- Parameters:
username
- TODO javadoc me pleasepassword
- TODO javadoc me please- Throws:
XMPPException
- if an XMPP protocol error was received.SmackException
- if Smack detected an exceptional situation.IOException
- if an I/O error occurred.InterruptedException
- if the calling thread was interrupted.- See Also:
-
login
public void login(CharSequence username, String password, Resourcepart resource) throws XMPPException, SmackException, IOException, InterruptedException Login with the given username (authorization identity). You may omit the password if a callback handler is used. If resource is null, then the server will generate one.- Parameters:
username
- TODO javadoc me pleasepassword
- TODO javadoc me pleaseresource
- TODO javadoc me please- Throws:
XMPPException
- if an XMPP protocol error was received.SmackException
- if Smack detected an exceptional situation.IOException
- if an I/O error occurred.InterruptedException
- if the calling thread was interrupted.- See Also:
-
loginInternal
protected abstract void loginInternal(String username, String password, Resourcepart resource) throws XMPPException, SmackException, IOException, InterruptedException -
isConnected
Description copied from interface:XMPPConnection
Returns true if currently connected to the XMPP server.- Specified by:
isConnected
in interfaceXMPPConnection
- Returns:
- true if connected.
-
isAuthenticated
Description copied from interface:XMPPConnection
Returns true if currently authenticated by successfully calling the login method.- Specified by:
isAuthenticated
in interfaceXMPPConnection
- Returns:
- true if authenticated.
-
getUser
Description copied from interface:XMPPConnection
Returns the full XMPP address of the user that is logged in to the connection ornull
if not logged in yet. An XMPP address is in the form username@server/resource.- Specified by:
getUser
in interfaceXMPPConnection
- Returns:
- the full XMPP address of the user logged in.
-
getStreamId
Description copied from interface:XMPPConnection
Returns the stream ID for this connection, which is the value set by the server when opening an XMPP stream. This value will benull
if not connected to the server.- Specified by:
getStreamId
in interfaceXMPPConnection
- Returns:
- the ID of this connection returned from the XMPP server or
null
if not connected to the server. - See Also:
-
hasCurrentConnectionException
-
setCurrentConnectionExceptionAndNotify
-
notifyWaitingThreads
-
waitFor
- Throws:
InterruptedException
-
waitForConditionOrThrowConnectionException
protected final void waitForConditionOrThrowConnectionException(Supplier<Boolean> condition, String waitFor) throws InterruptedException, SmackException.SmackWrappedException, SmackException.NoResponseException -
bindResourceAndEstablishSession
protected Resourcepart bindResourceAndEstablishSession(Resourcepart resource) throws SmackException, InterruptedException, XMPPException -
afterSuccessfulLogin
protected void afterSuccessfulLogin(boolean resumed) throws SmackException.NotConnectedException, InterruptedException -
isAnonymous
Description copied from interface:XMPPConnection
Returns true if currently authenticated anonymously.- Specified by:
isAnonymous
in interfaceXMPPConnection
- Returns:
- true if authenticated anonymously.
-
getUsedSaslMechansism
Get the name of the SASL mechanism that was used to authenticate this connection. This returns the name of mechanism which was used the last time this connection was authenticated, and will returnnull
if this connection was not authenticated before.- Returns:
- the name of the used SASL mechanism.
- Since:
- 4.2
-
getConnectionLock
-
throwNotConnectedExceptionIfAppropriate
protected void throwNotConnectedExceptionIfAppropriate() throws SmackException.NotConnectedException -
throwNotConnectedExceptionIfAppropriate
protected void throwNotConnectedExceptionIfAppropriate(String optionalHint) throws SmackException.NotConnectedException -
throwAlreadyConnectedExceptionIfAppropriate
protected void throwAlreadyConnectedExceptionIfAppropriate() throws SmackException.AlreadyConnectedException -
throwAlreadyLoggedInExceptionIfAppropriate
protected void throwAlreadyLoggedInExceptionIfAppropriate() throws SmackException.AlreadyLoggedInException -
getStanzaFactory
- Specified by:
getStanzaFactory
in interfaceXMPPConnection
-
sendStanza
public final void sendStanza(Stanza stanza) throws SmackException.NotConnectedException, InterruptedException Description copied from interface:XMPPConnection
Sends the specified stanza to the server.- Specified by:
sendStanza
in interfaceXMPPConnection
- Parameters:
stanza
- the stanza to send.- Throws:
SmackException.NotConnectedException
- if the connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
sendStanzaNonBlocking
public final void sendStanzaNonBlocking(Stanza stanza) throws SmackException.NotConnectedException, SmackException.OutgoingQueueFullException - Specified by:
sendStanzaNonBlocking
in interfaceXMPPConnection
- Throws:
SmackException.NotConnectedException
SmackException.OutgoingQueueFullException
-
authenticate
protected final SASLMechanism authenticate(String username, String password, EntityBareJid authzid, SSLSession sslSession) throws XMPPException.XMPPErrorException, SASLErrorException, SmackException.SmackSaslException, SmackException.NotConnectedException, SmackException.NoResponseException, IOException, InterruptedException, SmackException.SmackWrappedException Authenticate a connection.- Parameters:
username
- the username that is authenticating with the server.password
- the password to send to the server.authzid
- the authorization identifier (typically null).sslSession
- the optional SSL/TLS session (if one was established)- Returns:
- the used SASLMechanism.
- Throws:
XMPPException.XMPPErrorException
- if there was an XMPP error returned.SASLErrorException
- if a SASL protocol error was returned.IOException
- if an I/O error occurred.InterruptedException
- if the calling thread was interrupted.SmackException.SmackSaslException
- if a SASL specific error occurred.SmackException.NotConnectedException
- if the XMPP connection is not connected.SmackException.NoResponseException
- if there was no response from the remote entity.SmackException.SmackWrappedException
- in case of an exception.- See Also:
-
SASLAuthentication.authenticate(String, String, EntityBareJid, SSLSession)
-
afterSaslAuthenticationSuccess
protected void afterSaslAuthenticationSuccess() throws SmackException.NotConnectedException, InterruptedException, SmackException.SmackWrappedExceptionHook for subclasses right after successful SASL authentication. RFC 6120 § 6.4.6. specifies a that the initiating entity, needs to initiate a new stream in this case. But some transports, like BOSH, requires a special handling.Note that we can not reset XMPPTCPConnection's parser here, because this method is invoked by the thread calling
login()
, but the parser reset has to be done within the reader thread.- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.SmackException.SmackWrappedException
- in case of an exception.
-
isSaslAuthenticated
-
disconnect
Closes the connection by setting presence to unavailable then closing the connection to the XMPP server. The XMPPConnection can still be used for connecting to the server again. -
disconnect
Closes the connection. A custom unavailable presence is sent to the server, followed by closing the stream. The XMPPConnection can still be used for connecting to the server again. A custom unavailable presence is useful for communicating offline presence information such as "On vacation". Typically, just the status text of the presence stanza is set with online information, but most XMPP servers will deliver the full presence stanza with whatever data is set.- Parameters:
unavailablePresence
- the optional presence stanza to send during shutdown.- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.
-
notifyConnectionError
Sends out a notification that there was an error with the connection and closes the connection.- Parameters:
exception
- the exception that causes the connection close event.
-
instantShutdown
Performs an unclean disconnect and shutdown of the connection. Does not send a closing stream stanza. -
shutdown
Shuts the current connection down. -
waitForClosingStreamTagFromServer
-
addConnectionListener
Description copied from interface:XMPPConnection
Adds a connection listener to this connection that will be notified when the connection closes or fails.- Specified by:
addConnectionListener
in interfaceXMPPConnection
- Parameters:
connectionListener
- a connection listener.
-
removeConnectionListener
Description copied from interface:XMPPConnection
Removes a connection listener from this connection.- Specified by:
removeConnectionListener
in interfaceXMPPConnection
- Parameters:
connectionListener
- a connection listener.
-
sendIqRequestAndWaitForResponse
public <I extends IQ> I sendIqRequestAndWaitForResponse(IQ request) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException Description copied from interface:XMPPConnection
Send an IQ request and wait for the response.- Specified by:
sendIqRequestAndWaitForResponse
in interfaceXMPPConnection
- Type Parameters:
I
- the type of the expected result IQ.- Parameters:
request
- the IQ request- Returns:
- an IQ with type 'result'
- Throws:
SmackException.NoResponseException
- if there was no response from the remote entity.XMPPException.XMPPErrorException
- if there was an XMPP error returned.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
createStanzaCollectorAndSend
public StanzaCollector createStanzaCollectorAndSend(IQ packet) throws SmackException.NotConnectedException, InterruptedException Description copied from interface:XMPPConnection
Creates a new stanza collector collecting IQ responses that are replies to the IQrequest
. Does also send therequest
IQ. The stanza filter for the collector is anIQReplyFilter
, guaranteeing that stanza id and JID in the 'from' address have expected values.- Specified by:
createStanzaCollectorAndSend
in interfaceXMPPConnection
- Parameters:
packet
- the IQ request to filter responses from- Returns:
- a new stanza collector.
- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
createStanzaCollectorAndSend
public StanzaCollector createStanzaCollectorAndSend(StanzaFilter packetFilter, Stanza packet) throws SmackException.NotConnectedException, InterruptedException Description copied from interface:XMPPConnection
Creates a new stanza collector for this connection. A stanza filter determines which stanzas will be accumulated by the collector. A StanzaCollector is more suitable to use than aStanzaListener
when you need to wait for a specific result.- Specified by:
createStanzaCollectorAndSend
in interfaceXMPPConnection
- Parameters:
packetFilter
- the stanza filter to use.packet
- the stanza to send right after the collector got created- Returns:
- a new stanza collector.
- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
createStanzaCollector
Description copied from interface:XMPPConnection
Creates a new stanza collector for this connection. A stanza filter determines which stanzas will be accumulated by the collector. A StanzaCollector is more suitable to use than aStanzaListener
when you need to wait for a specific result.Note: If you send a Stanza right after using this method, then consider using
XMPPConnection.createStanzaCollectorAndSend(StanzaFilter, Stanza)
instead. Otherwise make sure cancel the StanzaCollector in every case, e.g. even if an exception is thrown, or otherwise you may leak the StanzaCollector.- Specified by:
createStanzaCollector
in interfaceXMPPConnection
- Parameters:
packetFilter
- the stanza filter to use.- Returns:
- a new stanza collector.
-
createStanzaCollector
Description copied from interface:XMPPConnection
Create a new stanza collector with the given stanza collector configuration.Please make sure to cancel the collector when it is no longer required. See also
XMPPConnection.createStanzaCollector(StanzaFilter)
.- Specified by:
createStanzaCollector
in interfaceXMPPConnection
- Parameters:
configuration
- the stanza collector configuration.- Returns:
- a new stanza collector.
-
removeStanzaCollector
Description copied from interface:XMPPConnection
Remove a stanza collector of this connection.- Specified by:
removeStanzaCollector
in interfaceXMPPConnection
- Parameters:
collector
- a stanza collectors which was created for this connection.
-
addStanzaListener
public final ListenerHandle addStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter) Description copied from interface:XMPPConnection
Registers a stanza listener with this connection. The listener will be invoked when a (matching) incoming stanza is received. The stanza filter determines which stanzas will be delivered to the listener. It is guaranteed that the same listener will not be invoked concurrently and the order of invocation will reflect the order in which the stanzas have been received. If the same stanza listener is added again with a different filter, only the new filter will be used.- Specified by:
addStanzaListener
in interfaceXMPPConnection
- Parameters:
stanzaListener
- the stanza listener to notify of new received stanzas.stanzaFilter
- the stanza filter to use.- Returns:
- a handle which can be used in try-with-resources statements.
-
removeStanzaListener
Description copied from interface:XMPPConnection
Removes a stanza listener for received stanzas from this connection.- Specified by:
removeStanzaListener
in interfaceXMPPConnection
- Parameters:
stanzaListener
- the stanza listener to remove.- Returns:
- true if the stanza listener was removed.
-
addSyncStanzaListener
public ListenerHandle addSyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter) Description copied from interface:XMPPConnection
Registers a synchronous stanza listener with this connection. A stanza listener will be invoked only when an incoming stanza is received. A stanza filter determines which stanzas will be delivered to the listener. If the same stanza listener is added again with a different filter, only the new filter will be used.Important: This stanza listeners will be called in the same single thread that processes all incoming stanzas. Only use this kind of stanza filter if it does not perform any XMPP activity that waits for a response. Consider using
XMPPConnection.addAsyncStanzaListener(StanzaListener, StanzaFilter)
when possible, i.e. when the invocation order doesn't have to be the same as the order of the arriving stanzas. If the order of the arriving stanzas, consider using aStanzaCollector
when possible.- Specified by:
addSyncStanzaListener
in interfaceXMPPConnection
- Parameters:
packetListener
- the stanza listener to notify of new received stanzas.packetFilter
- the stanza filter to use.- Returns:
- a handle which can be used in try-with-resources statements.
-
removeSyncStanzaListener
Description copied from interface:XMPPConnection
Removes a stanza listener for received stanzas from this connection.- Specified by:
removeSyncStanzaListener
in interfaceXMPPConnection
- Parameters:
packetListener
- the stanza listener to remove.- Returns:
- true if the stanza listener was removed
-
addAsyncStanzaListener
public ListenerHandle addAsyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter) Description copied from interface:XMPPConnection
Registers an asynchronous stanza listener with this connection. A stanza listener will be invoked only when an incoming stanza is received. A stanza filter determines which stanzas will be delivered to the listener. If the same stanza listener is added again with a different filter, only the new filter will be used.Unlike
XMPPConnection.addAsyncStanzaListener(StanzaListener, StanzaFilter)
stanza listeners added with this method will be invoked asynchronously in their own thread. Use this method if the order of the stanza listeners must not depend on the order how the stanzas where received.- Specified by:
addAsyncStanzaListener
in interfaceXMPPConnection
- Parameters:
packetListener
- the stanza listener to notify of new received stanzas.packetFilter
- the stanza filter to use.- Returns:
- a handle which can be used in try-with-resources statements.
-
removeAsyncStanzaListener
Description copied from interface:XMPPConnection
Removes an asynchronous stanza listener for received stanzas from this connection.- Specified by:
removeAsyncStanzaListener
in interfaceXMPPConnection
- Parameters:
packetListener
- the stanza listener to remove.- Returns:
- true if the stanza listener was removed
-
addStanzaSendingListener
Description copied from interface:XMPPConnection
Registers a stanza listener with this connection. The listener will be notified of every stanza that this connection sends. A stanza filter determines which stanzas will be delivered to the listener. Note that the thread that writes stanzas will be used to invoke the listeners. Therefore, each stanza listener should complete all operations quickly or use a different thread for processing.- Specified by:
addStanzaSendingListener
in interfaceXMPPConnection
- Parameters:
packetListener
- the stanza listener to notify of sent stanzas.packetFilter
- the stanza filter to use.
-
removeStanzaSendingListener
Description copied from interface:XMPPConnection
Removes a stanza listener for sending stanzas from this connection.- Specified by:
removeStanzaSendingListener
in interfaceXMPPConnection
- Parameters:
packetListener
- the stanza listener to remove.
-
firePacketSendingListeners
Process all stanza listeners for sending stanzas.Compared to
firePacketInterceptors(Stanza)
, the listeners will be invoked in a new thread.- Parameters:
sendTopLevelStreamElement
- the top level stream element which just got send.
-
addMessageInterceptor
public void addMessageInterceptor(Consumer<MessageBuilder> messageInterceptor, Predicate<Message> messageFilter) Description copied from interface:XMPPConnection
Registers a stanza interceptor with this connection. The interceptor will be invoked every time a stanza is about to be sent by this connection. Interceptors may modify the stanza to be sent. A stanza filter determines which stanzas will be delivered to the interceptor.NOTE: For a similar functionality on incoming stanzas, see
XMPPConnection.addAsyncStanzaListener(StanzaListener, StanzaFilter)
.- Specified by:
addMessageInterceptor
in interfaceXMPPConnection
- Parameters:
messageInterceptor
- the stanza interceptor to notify of stanzas about to be sent.messageFilter
- the stanza filter to use.
-
removeMessageInterceptor
Description copied from interface:XMPPConnection
Removes a message interceptor.- Specified by:
removeMessageInterceptor
in interfaceXMPPConnection
- Parameters:
messageInterceptor
- the message interceptor to remove.
-
addPresenceInterceptor
public void addPresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor, Predicate<Presence> presenceFilter) Description copied from interface:XMPPConnection
Registers a stanza interceptor with this connection. The interceptor will be invoked every time a stanza is about to be sent by this connection. Interceptors may modify the stanza to be sent. A stanza filter determines which stanzas will be delivered to the interceptor.NOTE: For a similar functionality on incoming stanzas, see
XMPPConnection.addAsyncStanzaListener(StanzaListener, StanzaFilter)
.- Specified by:
addPresenceInterceptor
in interfaceXMPPConnection
- Parameters:
presenceInterceptor
- the stanza interceptor to notify of stanzas about to be sent.presenceFilter
- the stanza filter to use.
-
removePresenceInterceptor
Description copied from interface:XMPPConnection
Removes a presence interceptor.- Specified by:
removePresenceInterceptor
in interfaceXMPPConnection
- Parameters:
presenceInterceptor
- the stanza interceptor to remove.
-
initDebugger
Initialize thedebugger
. You can specify a customizedSmackDebugger
by setup the system propertysmack.debuggerClass
to the implementation.- Throws:
IllegalStateException
- if the reader or writer isn't yet initialized.IllegalArgumentException
- if the SmackDebugger can't be loaded.
-
getReplyTimeout
Description copied from interface:XMPPConnection
Returns the current value of the reply timeout in milliseconds for request for this XMPPConnection instance.- Specified by:
getReplyTimeout
in interfaceXMPPConnection
- Returns:
- the reply timeout in milliseconds
-
setReplyTimeout
Description copied from interface:XMPPConnection
Set the stanza reply timeout in milliseconds. In most cases, Smack will throw aSmackException.NoResponseException
if no reply to a request was received within the timeout period.- Specified by:
setReplyTimeout
in interfaceXMPPConnection
- Parameters:
timeout
- for a reply in milliseconds
-
setUnknownIqRequestReplyMode
public void setUnknownIqRequestReplyMode(SmackConfiguration.UnknownIqRequestReplyMode unknownIqRequestReplyMode) Set how Smack behaves when an unknown IQ request has been received.- Parameters:
unknownIqRequestReplyMode
- reply mode.
-
buildNonzaCallback
-
sendAndWaitForResponse
protected <SN extends Nonza,FN extends Nonza> SN sendAndWaitForResponse(Nonza nonza, Class<SN> successNonzaClass, Class<FN> failedNonzaClass) throws SmackException.NoResponseException, SmackException.NotConnectedException, InterruptedException, XMPPException.FailedNonzaException -
parseAndProcessNonza
protected final void parseAndProcessNonza(XmlPullParser parser) throws IOException, XmlPullParserException, SmackParsingException -
parseAndProcessStanza
protected void parseAndProcessStanza(XmlPullParser parser) throws XmlPullParserException, IOException, InterruptedException -
processStanza
Processes a stanza after it's been fully parsed by looping through the installed stanza collectors and listeners and letting them examine the stanza to see if they are a match with the filter.- Parameters:
stanza
- the stanza to process.- Throws:
InterruptedException
- if the calling thread was interrupted.
-
invokeStanzaCollectorsAndNotifyRecvListeners
InvokeStanzaCollector.processStanza(Stanza)
for every StanzaCollector with the given packet. Also notify the receive listeners with a matching stanza filter about the packet.This method will be invoked by the connections incoming processing thread which may be shared across multiple connections and thus it is important that no user code, e.g. in form of a callback, is invoked by this method. For the same reason, this method must not block for an extended period of time.
- Parameters:
packet
- the stanza to notify the StanzaCollectors and receive listeners about.
-
setWasAuthenticated
Sets whether the connection has already logged in the server. This method assures that thewasAuthenticated
flag is never reset once it has ever been set. -
callConnectionConnectingListener
-
callConnectionConnectedListener
-
callConnectionAuthenticatedListener
-
getConnectionCounter
Description copied from interface:XMPPConnection
Get the connection counter of this XMPPConnection instance. Those can be used as ID to identify the connection, but beware that the ID may not be unique if you create more then2*Integer.MAX_VALUE
instances as the counter could wrap.- Specified by:
getConnectionCounter
in interfaceXMPPConnection
- Returns:
- the connection counter of this XMPPConnection
-
setFromMode
Description copied from interface:XMPPConnection
Set the FromMode for this connection instance. Defines how the 'from' attribute of outgoing stanzas should be populated by Smack.- Specified by:
setFromMode
in interfaceXMPPConnection
- Parameters:
fromMode
- TODO javadoc me please
-
getFromMode
Description copied from interface:XMPPConnection
Get the currently active FromMode.- Specified by:
getFromMode
in interfaceXMPPConnection
- Returns:
- the currently active
XMPPConnection.FromMode
-
parseFeatures
protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException -
parseFeaturesAndNotify
- Throws:
Exception
-
afterFeaturesReceived
-
getFeature
Description copied from interface:XMPPConnection
Get the feature stanza extensions for a given stream feature of the server, ornull
if the server doesn't support that feature.- Specified by:
getFeature
in interfaceXMPPConnection
- Type Parameters:
F
-ExtensionElement
type of the feature.- Parameters:
qname
- the qualified name of the XML element of feature.- Returns:
- a stanza extensions of the feature or
null
-
hasFeature
Description copied from interface:XMPPConnection
Return true if the server supports the given stream feature.- Specified by:
hasFeature
in interfaceXMPPConnection
- Parameters:
qname
- the qualified name of the XML element of feature.- Returns:
- true if the server supports the stream feature.
-
addStreamFeature
-
sendIqRequestAsync
Description copied from interface:XMPPConnection
Send an IQ request asynchronously. The connection's default reply timeout will be used.- Specified by:
sendIqRequestAsync
in interfaceXMPPConnection
- Parameters:
request
- the IQ request to send.- Returns:
- a SmackFuture for the response.
-
sendIqRequestAsync
Description copied from interface:XMPPConnection
Send an IQ request asynchronously.- Specified by:
sendIqRequestAsync
in interfaceXMPPConnection
- Parameters:
request
- the IQ request to send.timeout
- the reply timeout in milliseconds.- Returns:
- a SmackFuture for the response.
-
sendAsync
Description copied from interface:XMPPConnection
Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter. The connection's default reply timeout will be used.- Specified by:
sendAsync
in interfaceXMPPConnection
- Type Parameters:
S
- the type of the stanza to send.- Parameters:
stanza
- the stanza to send.replyFilter
- the filter used for the response stanza.- Returns:
- a SmackFuture for the response.
-
sendAsync
public <S extends Stanza> SmackFuture<S,Exception> sendAsync(S stanza, StanzaFilter replyFilter, long timeout) Description copied from interface:XMPPConnection
Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter.- Specified by:
sendAsync
in interfaceXMPPConnection
- Type Parameters:
S
- the type of the stanza to send.- Parameters:
stanza
- the stanza to send.replyFilter
- the filter used for the response stanza.timeout
- the reply timeout in milliseconds.- Returns:
- a SmackFuture for the response.
-
addOneTimeSyncCallback
Description copied from interface:XMPPConnection
Add a callback that is called exactly once and synchronously with the incoming stanza that matches the given stanza filter.- Specified by:
addOneTimeSyncCallback
in interfaceXMPPConnection
- Parameters:
callback
- the callback invoked once the stanza filter matches a stanza.packetFilter
- the filter to match stanzas or null to match all.
-
registerIQRequestHandler
Description copied from interface:XMPPConnection
Register an IQ request handler with this connection.IQ request handler process incoming IQ requests, i.e. incoming IQ stanzas of type 'get' or 'set', and return a result.
- Specified by:
registerIQRequestHandler
in interfaceXMPPConnection
- Parameters:
iqRequestHandler
- the IQ request handler to register.- Returns:
- the previously registered IQ request handler or null.
-
unregisterIQRequestHandler
Description copied from interface:XMPPConnection
Convenience method forXMPPConnection.unregisterIQRequestHandler(String, String, org.jivesoftware.smack.packet.IQ.Type)
.- Specified by:
unregisterIQRequestHandler
in interfaceXMPPConnection
- Parameters:
iqRequestHandler
- TODO javadoc me please- Returns:
- the previously registered IQ request handler or null.
-
unregisterIQRequestHandler
Description copied from interface:XMPPConnection
Unregister an IQ request handler with this connection.- Specified by:
unregisterIQRequestHandler
in interfaceXMPPConnection
- Parameters:
element
- the IQ element the IQ request handler is responsible for.namespace
- the IQ namespace the IQ request handler is responsible for.type
- the IQ type the IQ request handler is responsible for.- Returns:
- the previously registered IQ request handler or null.
-
getLastStanzaReceived
Description copied from interface:XMPPConnection
Returns the timestamp in milliseconds when the last stanza was received.- Specified by:
getLastStanzaReceived
in interfaceXMPPConnection
- Returns:
- the timestamp in milliseconds
-
getAuthenticatedConnectionInitiallyEstablishedTimestamp
Get the timestamp when the connection was the first time authenticated, i.e., when the first successful login was performed. Note that this value is not reset on disconnect, so it represents the timestamp from the last authenticated connection. The value is also not reset on stream resumption.- Returns:
- the timestamp or
null
. - Since:
- 4.3.3
-
setParsingExceptionCallback
Install a parsing exception callback, which will be invoked once an exception is encountered while parsing a stanza.- Parameters:
callback
- the callback to install
-
getParsingExceptionCallback
Get the current active parsing exception callback.- Returns:
- the active exception callback or null if there is none
-
toString
-
asyncGoLimited
-
setMaxAsyncOperations
-
asyncGo
-
getReactor
-
schedule
-
onStreamOpen
Must be called when a XMPP stream open tag is encountered. Sets values like the stream ID and the incoming stream XML environment.This method also returns a matching stream close tag. For example if the stream open is
<stream …>
, then</stream>
is returned. But if it is<stream:stream>
, then</stream:stream>
is returned. Or if it is<foo:stream>
, then</foo:stream>
is returned.- Parameters:
parser
- an XML parser that is positioned at the start of the stream open.- Returns:
- a String representing the corresponding stream end tag.
-
sendStreamOpen
protected final void sendStreamOpen() throws SmackException.NotConnectedException, InterruptedException -
getStreamOpen
protected AbstractStreamOpen getStreamOpen(DomainBareJid to, CharSequence from, String id, String lang) -
updateOutgoingStreamXmlEnvironmentOnStreamOpen
-
getSmackTlsContext
-