Class XmppClient

  • All Implemented Interfaces:
    AutoCloseable, Session, StreamHandler

    public final class XmppClient
    extends XmppSession
    The base class for establishing an XMPP session with a server, i.e. client-to-server sessions.

    Establishing an XMPP Session

    The following example shows the most simple way to establish a session:
    
     XmppClient xmppClient = XmppClient.create("domain");
     xmppClient.connect();
     xmppClient.login("username", "password");
     
    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.

    Sending Messages

    Once connected, you can send messages:
    
     xmppClient.sendMessage(new Message(Jid.of("juliet@example.net"), Message.Type.CHAT));
     

    Closing the Session

    
     xmppClient.close();
     

    Listening for Messages and Presence

    Note: Adding the following listeners should be added before logging in, otherwise they might not trigger.
    
     // Listen for messages
     xmppClient.addInboundMessageListener(e ->
         // Handle inbound message.
     );
     // Listen for presence changes
     xmppClient.addInboundPresenceListener(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.
    See Also:
    XmppSessionConfiguration, TcpConnectionConfiguration, BoshConnectionConfiguration
    • Method Detail

      • create

        public static XmppClient create​(String xmppServiceDomain,
                                        ClientConnectionConfiguration... connectionConfigurations)
        Creates a new XMPP client instance. Any registered creation listeners are triggered.
        Parameters:
        xmppServiceDomain - The XMPP service domain.
        connectionConfigurations - The connection methods, which are used to connect.
        Returns:
        The XMPP client.
      • create

        public static XmppClient create​(String xmppServiceDomain,
                                        XmppSessionConfiguration configuration,
                                        ClientConnectionConfiguration... connectionConfigurations)
        Creates a new XMPP client instance. Any registered creation listeners are triggered.
        Parameters:
        xmppServiceDomain - The XMPP service domain.
        configuration - The configuration.
        connectionConfigurations - The connection methods, which are used to connect.
        Returns:
        The XMPP client.
      • login

        public final byte[] login​(String user,
                                  String password)
                           throws XmppException
        Authenticates against the server and binds a random resource (assigned by the server).
        Parameters:
        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.
        Returns:
        The additional data with success, i.e. the data returned upon successful authentication.
        Throws:
        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.
        StanzaErrorException - If the server returned a stanza error during resource binding or roster retrieval.
        XmppException - If the login failed, due to another error.
      • login

        public final byte[] login​(String user,
                                  String password,
                                  String resource)
                           throws XmppException
        Authenticates against the server with username/password credential and binds a resource.
        Parameters:
        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.
        Returns:
        The additional data with success, i.e. the data returned upon successful authentication.
        Throws:
        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.
        StanzaErrorException - If the server returned a stanza error during resource binding or roster retrieval.
        XmppException - If the login failed, due to another error.
      • login

        public final byte[] login​(String authorizationId,
                                  String user,
                                  String password,
                                  String resource)
                           throws XmppException
        Authenticates against the server with an authorization id and username/password credential and binds a resource.
        Parameters:
        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.
        Returns:
        The additional data with success, i.e. the data returned upon successful authentication.
        Throws:
        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.
        StanzaErrorException - If the server returned a stanza error during resource binding or roster retrieval.
        XmppException - If the login failed, due to another error.
      • login

        public final byte[] login​(String authorizationId,
                                  CallbackHandler callbackHandler,
                                  String resource)
                           throws XmppException
        Authenticates against the server with a custom callback handler and binds a resource.
        Parameters:
        authorizationId - The authorization id.
        callbackHandler - The callback handler.
        resource - The resource. If null or empty, the resource is randomly assigned by the server.
        Returns:
        The additional data with success, i.e. the data returned upon successful authentication.
        Throws:
        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.
        StanzaErrorException - If the server returned a stanza error during resource binding or roster retrieval.
        XmppException - If the login failed, due to another error.
      • loginAnonymously

        public final byte[] loginAnonymously()
                                      throws XmppException
        Logs in anonymously and binds a resource.
        Returns:
        The additional data with success, i.e. the data returned upon successful authentication.
        Throws:
        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.
        StanzaErrorException - If the server returned a stanza error during resource binding.
        XmppException - If the login failed, due to another error.
      • isAnonymous

        public final boolean isAnonymous()
        Indicates whether the session has been logged in anonymously. If never logged in at all, returns false.
        Returns:
        True, if the session is anonymous.