Skip to content

Commit da33f1c

Browse files
Abhi591rohitesh-wingify
authored andcommitted
fix: handle gzip response in v2-settings
1 parent 7f31df6 commit da33f1c

6 files changed

Lines changed: 67 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.12.1] - 2025-09-09
9+
10+
### Fixed
11+
- Handle case where we are getting Gzip-compressed response for v2-settings
12+
813
## [1.12.0] - 2025-09-01
914

1015
### Fixed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License. -->
1919

2020
<groupId>com.vwo.sdk</groupId>
2121
<artifactId>vwo-fme-java-sdk</artifactId>
22-
<version>1.12.0</version>
22+
<version>1.12.1</version>
2323
<packaging>jar</packaging>
2424

2525
<name>VWO FME Java SDK</name>

src/main/java/com/vwo/constants/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Constants {
2828
public static final int DEFAULT_REQUEST_TIME_INTERVAL = 600; // 10 * 60(secs) = 600 secs i.e. 10 minutes
2929
public static final int DEFAULT_EVENTS_PER_REQUEST = 100;
3030
public static final String SDK_NAME = "vwo-fme-java-sdk";
31-
public static final String SDK_VERSION = "1.12.0";
31+
public static final String SDK_VERSION = "1.12.1";
3232
public static final long SETTINGS_EXPIRY = 10000000;
3333
public static final long SETTINGS_TIMEOUT = 50000;
3434

src/main/java/com/vwo/packages/network_layer/client/NetworkClient.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
import com.vwo.packages.network_layer.models.RequestModel;
2121
import com.vwo.packages.network_layer.models.ResponseModel;
2222

23+
import java.io.BufferedInputStream;
2324
import java.io.BufferedReader;
25+
import java.io.ByteArrayInputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
2428
import java.io.InputStreamReader;
2529
import java.io.OutputStream;
2630
import java.net.HttpURLConnection;
@@ -29,6 +33,7 @@
2933
import java.util.HashMap;
3034
import java.util.LinkedHashMap;
3135
import java.util.Map;
36+
import java.util.zip.GZIPInputStream;
3237

3338
public class NetworkClient implements NetworkClientInterface {
3439

@@ -41,6 +46,40 @@ public static String constructUrl(Map<String, Object> networkOptions) {
4146
return networkOptions.get("scheme").toString().toLowerCase() + "://" + hostname + path;
4247
}
4348

49+
/**
50+
* Magic Number Detection - Check first two bytes for gzip signature (0x1f, 0x8b)
51+
* This is the most reliable method as it checks the actual content
52+
*/
53+
private boolean isGzippedByMagicNumber(InputStream inputStream) throws IOException {
54+
if (!inputStream.markSupported()) {
55+
return false;
56+
}
57+
58+
inputStream.mark(2);
59+
int byte1 = inputStream.read();
60+
int byte2 = inputStream.read();
61+
inputStream.reset();
62+
63+
return (byte1 == 0x1f && byte2 == 0x8b);
64+
}
65+
66+
/**
67+
* This method is used to get the input stream from the connection
68+
* @param connection The HttpURLConnection object
69+
* @return The InputStream object
70+
* @throws IOException If an I/O error occurs
71+
*/
72+
private InputStream getInputStream(HttpURLConnection connection) throws IOException {
73+
InputStream rawInputStream = connection.getInputStream();
74+
75+
BufferedInputStream bufferedStream = new BufferedInputStream(rawInputStream);
76+
if (isGzippedByMagicNumber(bufferedStream)) {
77+
return new GZIPInputStream(bufferedStream);
78+
}
79+
80+
return bufferedStream;
81+
}
82+
4483
/**
4584
* Performs a GET request using the provided RequestModel.
4685
* @param requestModel The model containing request options.
@@ -71,7 +110,12 @@ public ResponseModel GET(RequestModel requestModel){
71110
return responseModel;
72111
}
73112

74-
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
113+
// Use the comprehensive gzip detection method
114+
InputStream inputStream = getInputStream(connection);
115+
if (inputStream instanceof GZIPInputStream) {
116+
responseModel.setIsGzipped(true);
117+
}
118+
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
75119
String inputLine;
76120
StringBuilder response = new StringBuilder();
77121

src/main/java/com/vwo/packages/network_layer/models/ResponseModel.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class ResponseModel {
2323
private Exception error;
2424
private Map<String, String> headers;
2525
private String data;
26+
private boolean isGzipped;
2627

2728
public void setStatusCode(int statusCode) {
2829
this.statusCode = statusCode;
@@ -55,4 +56,12 @@ public int getStatusCode() {
5556
public Exception getError() {
5657
return error;
5758
}
59+
60+
public void setIsGzipped(boolean isGzipped) {
61+
this.isGzipped = isGzipped;
62+
}
63+
64+
public boolean getIsGzipped() {
65+
return isGzipped;
66+
}
5867
}

src/main/java/com/vwo/services/SettingsManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.vwo.packages.network_layer.manager.NetworkManager;
3030
import com.vwo.packages.network_layer.models.RequestModel;
3131
import com.vwo.packages.network_layer.models.ResponseModel;
32+
import com.vwo.utils.LogMessageUtil;
3233
import com.vwo.utils.NetworkUtil;
3334

3435
// public class SettingsManager implements ISettingsManager {
@@ -150,6 +151,11 @@ public String fetchSettings(Boolean isViaWebhook) {
150151
});
151152
return null;
152153
}
154+
if (response.getIsGzipped()) {
155+
try {
156+
LogMessageUtil.sendLogToVWO("Got Gzip response from the server for v2-settings", LogLevelEnum.DEBUG.name(), this);
157+
} catch (Exception e) {}
158+
}
153159
this.settingsFetchTime = System.currentTimeMillis() - startTime;
154160
return response.getData();
155161
} catch (Exception e) {

0 commit comments

Comments
 (0)