null
*/
public HostnameVerifier getHostnameVerifier() {
if (hostnameVerifier != null)
return hostnameVerifier;
return SmackConfiguration.getDefaultHostnameVerifier();
}
/**
* Returns true if the new connection about to be establish is going to be debugged. By
* default the value of {@link SmackConfiguration#DEBUG} is used.
*
* @return true if the new connection about to be establish is going to be debugged.
*/
public boolean isDebuggerEnabled() {
return debuggerEnabled;
}
/**
* Returns true if a {@link Session} will be requested on login if the server
* supports it. Although this was mandatory on RFC 3921, RFC 6120/6121 don't
* even mention this part of the protocol.
*
* @return true if a session has to be requested when logging in.
* @deprecated Smack processes the 'optional' element of the session stream feature.
* @see Builder#setLegacySessionDisabled(boolean)
*/
@Deprecated
public boolean isLegacySessionDisabled() {
return legacySessionDisabled;
}
/**
* Returns a CallbackHandler to obtain information, such as the password or
* principal information during the SASL authentication. A CallbackHandler
* will be used ONLY if no password was specified during the login while
* using SASL authentication.
*
* @return a CallbackHandler to obtain information, such as the password or
* principal information during the SASL authentication.
*/
public CallbackHandler getCallbackHandler() {
return callbackHandler;
}
/**
* Returns the socket factory used to create new xmppConnection sockets.
* This is useful when connecting through SOCKS5 proxies.
*
* @return socketFactory used to create new sockets.
*/
public SocketFactory getSocketFactory() {
return this.socketFactory;
}
/**
* Get the configured proxy information (if any).
*
* @return the configured proxy information or null
.
*/
public ProxyInfo getProxyInfo() {
return proxy;
}
/**
* An enumeration for TLS security modes that are available when making a connection
* to the XMPP server.
*/
public static enum SecurityMode {
/**
* Security via TLS encryption is required in order to connect. If the server
* does not offer TLS or if the TLS negotiation fails, the connection to the server
* will fail.
*/
required,
/**
* Security via TLS encryption is used whenever it's available. This is the
* default setting.
* * Do not use this setting unless you can't use {@link #required}. An attacker could easily perform a * Man-in-the-middle attack and prevent TLS from being used, leaving you with an unencrypted (and * unauthenticated) connection. *
*/ ifpossible, /** * Security via TLS encryption is disabled and only un-encrypted connections will * be used. If only TLS encryption is available from the server, the connection * will fail. */ disabled } /** * Returns the username to use when trying to reconnect to the server. * * @return the username to use when trying to reconnect to the server. */ public CharSequence getUsername() { return this.username; } /** * Returns the password to use when trying to reconnect to the server. * * @return the password to use when trying to reconnect to the server. */ public String getPassword() { return this.password; } /** * Returns the resource to use when trying to reconnect to the server. * * @return the resource to use when trying to reconnect to the server. */ public Resourcepart getResource() { return resource; } /** * Returns the optional XMPP address to be requested as the SASL authorization identity. * * @return the authorization identifier. * @see RFC 6120 § 6.3.8. Authorization Identity * @since 4.2 */ public EntityBareJid getAuthzid() { return authzid; } /** * Returns true if an available presence should be sent when logging in while reconnecting. * * @return true if an available presence should be sent when logging in while reconnecting */ public boolean isSendPresence() { return sendPresence; } /** * Returns true if the connection is going to use stream compression. Stream compression * will be requested after TLS was established (if TLS was enabled) and only if the server * offered stream compression. With stream compression network traffic can be reduced * up to 90%. By default compression is disabled. * * @return true if the connection is going to use stream compression. */ public boolean isCompressionEnabled() { // Compression for non-TCP connections is always disabled return false; } /** * Check if the given SASL mechansism is enabled in this connection configuration. * * @param saslMechanism * @return true if the given SASL mechanism is enabled, false otherwise. */ public boolean isEnabledSaslMechanism(String saslMechanism) { // If enabledSaslMechanisms is not set, then all mechanisms are enabled per default if (enabledSaslMechanisms == null) { return true; } return enabledSaslMechanisms.contains(saslMechanism); } public Set* This is an abstract class that uses the builder design pattern and the "getThis() trick" to recover the type of * the builder in a class hierarchies with a self-referential generic supertype. Otherwise chaining of build * instructions from the superclasses followed by build instructions of a sublcass would not be possible, because * the superclass build instructions would return the builder of the superclass and not the one of the subclass. You * can read more about it a Angelika Langer's Generics FAQ, especially the entry What is the * "getThis()" trick?. *
* * @param the builder type parameter. * @param* The username is usually the localpart of the clients JID. But some SASL mechanisms or services may require a different * format (e.g. the full JID) as used authorization identity. *
* * @param username the username or authorization identity * @param password the password or token used to authenticate * @return a reference to this builder. */ public B setUsernameAndPassword(CharSequence username, String password) { this.username = username; this.password = password; return getThis(); } /** * Set the service name of this XMPP service (i.e., the XMPP domain). * * @param serviceName the service name * @return a reference to this builder. * @deprecated use {@link #setXmppDomain(DomainBareJid)} instead. */ @Deprecated public B setServiceName(DomainBareJid serviceName) { return setXmppDomain(serviceName); } /** * Set the service name of this XMPP service (i.e., the XMPP domain). * * @param xmppServiceDomain the service name * @return a reference to this builder. */ public B setXmppDomain(DomainBareJid xmppServiceDomain) { this.xmppServiceDomain = xmppServiceDomain; return getThis(); } /** * Set the resource to use. *
* If resource
is null
, then the server will automatically create a resource for the
* client. Default resource is "Smack".
*
* For more information on how to create a SSLContext see Java Secure Socket Extension (JSEE) Reference Guide: Creating Your Own X509TrustManager * * @param context the custom SSLContext for new sockets. * @return a reference to this builder. */ public B setCustomSSLContext(SSLContext context) { this.customSSLContext = Objects.requireNonNull(context, "The SSLContext must not be null"); return getThis(); } /** * Set the enabled SSL/TLS protocols. * * @param enabledSSLProtocols * @return a reference to this builder. */ public B setEnabledSSLProtocols(String[] enabledSSLProtocols) { this.enabledSSLProtocols = enabledSSLProtocols; return getThis(); } /** * Set the enabled SSL/TLS ciphers. * * @param enabledSSLCiphers the enabled SSL/TLS ciphers * @return a reference to this builder. */ public B setEnabledSSLCiphers(String[] enabledSSLCiphers) { this.enabledSSLCiphers = enabledSSLCiphers; return getThis(); } /** * Set the HostnameVerifier used to verify the hostname of SSLSockets used by XMPP connections * created with this ConnectionConfiguration. * * @param verifier * @return a reference to this builder. */ public B setHostnameVerifier(HostnameVerifier verifier) { hostnameVerifier = verifier; return getThis(); } /** * Sets if a {@link Session} will be requested on login if the server supports * it. Although this was mandatory on RFC 3921, RFC 6120/6121 don't even * mention this part of the protocol. *
* Deprecation notice: This setting is no longer required in most cases because Smack processes the 'optional' * element eventually found in the session stream feature. See also Here Lies Extensible Messaging and Presence * Protocol (XMPP) Session Establishment *
* * @param legacySessionDisabled if a session has to be requested when logging in. * @return a reference to this builder. * @deprecated Smack processes the 'optional' element of the session stream feature. */ @Deprecated public B setLegacySessionDisabled(boolean legacySessionDisabled) { this.legacySessionDisabled = legacySessionDisabled; return getThis(); } /** * Sets if an initial available presence will be sent to the server. By default * an available presence will be sent to the server indicating that this presence * is not online and available to receive messages. If you want to log in without * being 'noticed' then pass a false value. * * @param sendPresence true if an initial available presence will be sent while logging in. * @return a reference to this builder. */ public B setSendPresence(boolean sendPresence) { this.sendPresence = sendPresence; return getThis(); } /** * Sets if the new connection about to be establish is going to be debugged. By * default the value of {@link SmackConfiguration#DEBUG} is used. * * @param debuggerEnabled if the new connection about to be establish is going to be debugged. * @return a reference to this builder. */ public B setDebuggerEnabled(boolean debuggerEnabled) { this.debuggerEnabled = debuggerEnabled; return getThis(); } /** * Sets the socket factory used to create new xmppConnection sockets. * This is useful when connecting through SOCKS5 proxies. * * @param socketFactory used to create new sockets. * @return a reference to this builder. */ public B setSocketFactory(SocketFactory socketFactory) { this.socketFactory = socketFactory; return getThis(); } /** * Set the information about the Proxy used for the connection. * * @param proxyInfo the Proxy information. * @return a reference to this builder. */ public B setProxyInfo(ProxyInfo proxyInfo) { this.proxy = proxyInfo; return getThis(); } /** * Allownull
or the empty String as username.
*
* Some SASL mechanisms (e.g. SASL External) may also signal the username (as "authorization identity"), in
* which case Smack should not throw an IllegalArgumentException when the username is not set.
*
* @return a reference to this builder.
*/
public B allowEmptyOrNullUsernames() {
allowEmptyOrNullUsername = true;
return getThis();
}
/**
* Perform anonymous authentication using SASL ANONYMOUS. Your XMPP service must support this authentication
* mechanism. This method also calls {@link #addEnabledSaslMechanism(String)} with "ANONYMOUS" as argument.
*
* @return a reference to this builder.
*/
public B performSaslAnonymousAuthentication() {
if (!SASLAuthentication.isSaslMechanismRegistered(SASLAnonymous.NAME)) {
throw new IllegalArgumentException("SASL " + SASLAnonymous.NAME + " is not registered");
}
throwIfEnabledSaslMechanismsSet();
allowEmptyOrNullUsernames();
addEnabledSaslMechanism(SASLAnonymous.NAME);
saslMechanismsSealed = true;
return getThis();
}
/**
* Perform authentication using SASL EXTERNAL. Your XMPP service must support this
* authentication mechanism. This method also calls {@link #addEnabledSaslMechanism(String)} with "EXTERNAL" as
* argument. It also calls {@link #allowEmptyOrNullUsernames()} and {@link #setSecurityMode(ConnectionConfiguration.SecurityMode)} to
* {@link SecurityMode#required}.
*
* @return a reference to this builder.
*/
public B performSaslExternalAuthentication(SSLContext sslContext) {
if (!SASLAuthentication.isSaslMechanismRegistered(SASLMechanism.EXTERNAL)) {
throw new IllegalArgumentException("SASL " + SASLMechanism.EXTERNAL + " is not registered");
}
setCustomSSLContext(sslContext);
throwIfEnabledSaslMechanismsSet();
allowEmptyOrNullUsernames();
setSecurityMode(SecurityMode.required);
addEnabledSaslMechanism(SASLMechanism.EXTERNAL);
saslMechanismsSealed = true;
return getThis();
}
private void throwIfEnabledSaslMechanismsSet() {
if (enabledSaslMechanisms != null) {
throw new IllegalStateException("Enabled SASL mechanisms found");
}
}
/**
* Add the given mechanism to the enabled ones. See {@link #addEnabledSaslMechanism(Collection)} for a discussion about enabled SASL mechanisms.
*
* @param saslMechanism the name of the mechanism to enable.
* @return a reference to this builder.
*/
public B addEnabledSaslMechanism(String saslMechanism) {
return addEnabledSaslMechanism(Arrays.asList(StringUtils.requireNotNullOrEmpty(saslMechanism,
"saslMechanism must not be null or empty")));
}
/**
* Enable the given SASL mechanisms. If you never add a mechanism to the set of enabled ones, all mechanisms
* known to Smack will be enabled. Only explicitly enable particular SASL mechanisms if you want to limit
* the used mechanisms to the enabled ones.
*
* @param saslMechanisms a collection of names of mechanisms to enable.
* @return a reference to this builder.
*/
public B addEnabledSaslMechanism(Collection* In XMPP, authorization identities are bare jids. In general, callers should allow the server to select the * authorization identifier automatically, and not call this. Note that setting the authzid does not set the XMPP * service domain, which should typically match. * Calling this will also SASL CRAM, since this mechanism does not support authzid. *
* * @param authzid The BareJid to be requested as the authorization identifier. * @return a reference to this builder. * @see RFC 6120 § 6.3.8. Authorization Identity * @since 4.2 */ public B setAuthzid(EntityBareJid authzid) { this.authzid = authzid; return getThis(); } public abstract C build(); protected abstract B getThis(); } }