From da5e6029ddbb68b694ab70a867eb9dc54cab1c5c Mon Sep 17 00:00:00 2001 From: f43nd1r Date: Thu, 14 Jun 2018 22:34:59 +0200 Subject: [PATCH 1/2] add gzip compression option --- .../java/org/acra/annotation/AcraHttpSender.java | 15 ++++++++------- .../main/java/org/acra/http/BaseHttpRequest.java | 13 ++++++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java b/acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java index b4feaa80e3..21a17b8753 100644 --- a/acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java +++ b/acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java @@ -18,19 +18,13 @@ import android.support.annotation.NonNull; import android.support.annotation.RawRes; - import org.acra.ACRAConstants; import org.acra.config.BaseHttpConfigurationBuilder; import org.acra.security.KeyStoreFactory; import org.acra.security.NoKeyStoreFactory; import org.acra.sender.HttpSender; -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; +import java.lang.annotation.*; /** * Http sender configuration @@ -136,4 +130,11 @@ * @since 5.0.0 */ @NonNull String certificateType() default ACRAConstants.DEFAULT_CERTIFICATE_TYPE; + + /** + * if the server request should be compressed using gzip + * + * @return if compression should be active + */ + boolean compress() default false; } diff --git a/acra-http/src/main/java/org/acra/http/BaseHttpRequest.java b/acra-http/src/main/java/org/acra/http/BaseHttpRequest.java index e6ab8581e1..a7e91bbd05 100644 --- a/acra-http/src/main/java/org/acra/http/BaseHttpRequest.java +++ b/acra-http/src/main/java/org/acra/http/BaseHttpRequest.java @@ -42,6 +42,7 @@ import java.security.GeneralSecurityException; import java.security.KeyStore; import java.util.Map; +import java.util.zip.GZIPOutputStream; import static org.acra.ACRA.LOG_TAG; @@ -60,6 +61,7 @@ public abstract class BaseHttpRequest implements HttpRequest { private final int connectionTimeOut; private final int socketTimeOut; private final Map headers; + private final HttpSenderConfiguration senderConfiguration; public BaseHttpRequest(@NonNull CoreConfiguration config, @NonNull Context context, @NonNull Method method, @Nullable String login, @Nullable String password, int connectionTimeOut, int socketTimeOut, @Nullable Map headers) { @@ -71,6 +73,7 @@ public BaseHttpRequest(@NonNull CoreConfiguration config, @NonNull Context conte this.connectionTimeOut = connectionTimeOut; this.socketTimeOut = socketTimeOut; this.headers = headers; + senderConfiguration = ConfigUtils.getPluginConfiguration(config, HttpSenderConfiguration.class); } @@ -104,7 +107,7 @@ public void send(@NonNull URL url, @NonNull T content) throws IOException { handleResponse(urlConnection.getResponseCode(), urlConnection.getResponseMessage()); urlConnection.disconnect(); } catch (SocketTimeoutException e) { - if (ConfigUtils.getPluginConfiguration(config, HttpSenderConfiguration.class).dropReportsOnTimeout()) { + if (senderConfiguration.dropReportsOnTimeout()) { Log.w(ACRA.LOG_TAG, "Dropped report due to timeout"); } else { throw e; @@ -155,6 +158,10 @@ protected void configureHeaders(@NonNull HttpURLConnection connection, @Nullable connection.setRequestProperty("Authorization", "Basic " + encoded); } + if (senderConfiguration.compress()) { + connection.setRequestProperty("Content-Encoding", "gzip"); + } + if (customHeaders != null) { for (final Map.Entry header : customHeaders.entrySet()) { connection.setRequestProperty(header.getKey(), header.getValue()); @@ -171,14 +178,14 @@ protected void writeContent(@NonNull HttpURLConnection connection, @NonNull Meth // write output - see http://developer.android.com/reference/java/net/HttpURLConnection.html connection.setRequestMethod(method.name()); connection.setDoOutput(true); - connection.setFixedLengthStreamingMode(contentAsBytes.length); // Disable ConnectionPooling because otherwise OkHttp ConnectionPool will try to start a Thread on #connect System.setProperty("http.keepAlive", "false"); connection.connect(); - final OutputStream outputStream = new BufferedOutputStream(connection.getOutputStream()); + final OutputStream outputStream = senderConfiguration.compress() ? new GZIPOutputStream(connection.getOutputStream(), ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES) + : new BufferedOutputStream(connection.getOutputStream()); try { outputStream.write(contentAsBytes); outputStream.flush(); From 049efe70f2c88c21ac26e1a62891199531e23762 Mon Sep 17 00:00:00 2001 From: f43nd1r Date: Fri, 15 Jun 2018 03:57:42 +0200 Subject: [PATCH 2/2] add since in javadoc --- acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java | 1 + 1 file changed, 1 insertion(+) diff --git a/acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java b/acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java index 21a17b8753..584e3ddcb8 100644 --- a/acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java +++ b/acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java @@ -135,6 +135,7 @@ * if the server request should be compressed using gzip * * @return if compression should be active + * @since 5.2.0 */ boolean compress() default false; }