From 1ea10831b6e0041825f4467ed48d31e499c3a9b9 Mon Sep 17 00:00:00 2001 From: Marcel Heckel Date: Thu, 8 Nov 2018 13:36:45 +0100 Subject: [PATCH] Enables the HTTP compression in JBOSH Adds an extra parameter "compressionEnabled" to ConnectionConfiguration that is used to set the setCompressionEnabled() of BOSHClientConfig --- .../smack/bosh/XMPPBOSHConnection.java | 2 ++ .../smack/ConnectionConfiguration.java | 23 ++++++++++++-- .../tcp/XMPPTCPConnectionConfiguration.java | 30 ------------------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java b/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java index d5bf5c4a4..552cba9c2 100644 --- a/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java +++ b/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java @@ -157,6 +157,8 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection { if (config.isProxyEnabled()) { cfgBuilder.setProxy(config.getProxyAddress(), config.getProxyPort()); } + cfgBuilder.setCompressionEnabled(config.isCompressionEnabled()); + client = BOSHClient.create(cfgBuilder.build()); client.addBOSHClientConnListener(new BOSHConnectionListener()); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java b/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java index 8d8cae0ca..6a0d93d20 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java @@ -125,6 +125,8 @@ public abstract class ConnectionConfiguration { private final Set enabledSaslMechanisms; + private final boolean compressionEnabled; + protected ConnectionConfiguration(Builder builder) { authzid = builder.authzid; username = builder.username; @@ -162,6 +164,8 @@ public abstract class ConnectionConfiguration { allowNullOrEmptyUsername = builder.allowEmptyOrNullUsername; enabledSaslMechanisms = builder.enabledSaslMechanisms; + compressionEnabled = builder.compressionEnabled; + // If the enabledSaslmechanisms are set, then they must not be empty assert (enabledSaslMechanisms != null ? !enabledSaslMechanisms.isEmpty() : true); @@ -440,8 +444,7 @@ public abstract class ConnectionConfiguration { * @return true if the connection is going to use stream compression. */ public boolean isCompressionEnabled() { - // Compression for non-TCP connections is always disabled - return false; + return compressionEnabled; } /** @@ -513,6 +516,7 @@ public abstract class ConnectionConfiguration { private boolean saslMechanismsSealed; private Set enabledSaslMechanisms; private X509TrustManager customX509TrustManager; + private boolean compressionEnabled = false; protected Builder() { if (SmackConfiguration.DEBUG) { @@ -946,6 +950,21 @@ public abstract class ConnectionConfiguration { return getThis(); } + /** + * Sets if the connection is going to use compression (default false). + * + * Compression is only activated if the server offers compression. With compression network + * traffic can be reduced up to 90%. By default compression is disabled. + * + * @param compressionEnabled if the connection is going to use compression on the HTTP level. + * @return a reference to this object. + */ + public B setCompressionEnabled(boolean compressionEnabled) { + this.compressionEnabled = compressionEnabled; + return getThis(); + } + + public abstract C build(); protected abstract B getThis(); diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnectionConfiguration.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnectionConfiguration.java index 425c67ffc..9fb6b71b2 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnectionConfiguration.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnectionConfiguration.java @@ -41,8 +41,6 @@ public final class XMPPTCPConnectionConfiguration extends ConnectionConfiguratio */ public static int DEFAULT_CONNECT_TIMEOUT = 30000; - private final boolean compressionEnabled; - /** * How long the socket will wait until a TCP connection is established (in milliseconds). */ @@ -50,23 +48,9 @@ public final class XMPPTCPConnectionConfiguration extends ConnectionConfiguratio private XMPPTCPConnectionConfiguration(Builder builder) { super(builder); - compressionEnabled = builder.compressionEnabled; connectTimeout = builder.connectTimeout; } - /** - * 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. - */ - @Override - public boolean isCompressionEnabled() { - return compressionEnabled; - } - /** * How long the socket will wait until a TCP connection is established (in milliseconds). Defaults to {@link #DEFAULT_CONNECT_TIMEOUT}. * @@ -91,20 +75,6 @@ public final class XMPPTCPConnectionConfiguration extends ConnectionConfiguratio private Builder() { } - /** - * Sets 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. - * - * @param compressionEnabled if the connection is going to use stream compression. - * @return a reference to this object. - */ - public Builder setCompressionEnabled(boolean compressionEnabled) { - this.compressionEnabled = compressionEnabled; - return this; - } - /** * Set how long the socket will wait until a TCP connection is established (in milliseconds). *