Skip to content

Commit b8964d0

Browse files
authored
Merge pull request #141 from contentstack/development
Development
2 parents fd83465 + e955859 commit b8964d0

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## v2.0.1
4+
5+
### Date: 21-October-2024
6+
7+
-Github Issues fixed
8+
-Issue with field ordering in SDK response
9+
310
## v2.0.0
411

512
### Date: 27-August-2024

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.contentstack.sdk</groupId>
77
<artifactId>java</artifactId>
8-
<version>2.0.0</version>
8+
<version>2.0.1</version>
99
<packaging>jar</packaging>
1010
<name>contentstack-java</name>
1111
<description>Java SDK for Contentstack Content Delivery API</description>
@@ -17,7 +17,7 @@
1717
<maven.compiler.source>1.8</maven.compiler.source>
1818
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1919
<surefire-report-plugin.version>2.22.0</surefire-report-plugin.version>
20-
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
20+
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
2121
<maven-javadoc-plugin.version>3.4.1</maven-javadoc-plugin.version>
2222
<dotenv-source.version>3.0.0</dotenv-source.version>
2323
<rxjava-source.version>3.1.8</rxjava-source.version>
@@ -183,6 +183,12 @@
183183
<version>${json-simple-version}</version>
184184
<scope>compile</scope>
185185
</dependency>
186+
187+
<dependency>
188+
<groupId>com.fasterxml.jackson.core</groupId>
189+
<artifactId>jackson-databind</artifactId>
190+
<version>2.15.2</version>
191+
</dependency>
186192
</dependencies>
187193

188194
<build>

src/main/java/com/contentstack/sdk/CSHttpConnection.java

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import okhttp3.Request;
44
import okhttp3.ResponseBody;
5+
56
import org.json.JSONArray;
67
import org.json.JSONException;
78
import org.json.JSONObject;
9+
810
import retrofit2.Call;
911
import retrofit2.Response;
1012

1113
import java.io.IOException;
1214
import java.io.UnsupportedEncodingException;
1315
import java.net.URLEncoder;
16+
import java.net.SocketTimeoutException;
1417
import java.nio.charset.StandardCharsets;
1518
import java.util.HashMap;
1619
import java.util.Iterator;
@@ -19,6 +22,10 @@
1922
import java.util.logging.Level;
2023
import java.util.logging.Logger;
2124
import java.util.stream.IntStream;
25+
import com.fasterxml.jackson.databind.ObjectMapper; // Jackson for JSON parsing
26+
import com.fasterxml.jackson.databind.json.JsonMapper;
27+
import com.fasterxml.jackson.databind.node.ObjectNode;
28+
import com.fasterxml.jackson.databind.type.MapType;
2229

2330
import static com.contentstack.sdk.Constants.*;
2431

@@ -185,6 +192,14 @@ public void send() {
185192
}
186193
}
187194

195+
private JSONObject createOrderedJSONObject(Map<String, Object> map) {
196+
JSONObject json = new JSONObject();
197+
for (Map.Entry<String, Object> entry : map.entrySet()) {
198+
json.put(entry.getKey(), entry.getValue());
199+
}
200+
return json;
201+
}
202+
188203
private void getService(String requestUrl) throws IOException {
189204

190205
this.headers.put(X_USER_AGENT_KEY, "contentstack-delivery-java/" + SDK_VERSION);
@@ -202,22 +217,41 @@ private void getService(String requestUrl) throws IOException {
202217
requestUrl = request.url().toString();
203218
}
204219

205-
Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
206-
if (response.isSuccessful()) {
207-
assert response.body() != null;
208-
if (request != null) {
209-
response = pluginResponseImp(request, response);
210-
}
211-
responseJSON = new JSONObject(response.body().string());
212-
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
213-
handleJSONArray();
220+
try {
221+
Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
222+
if (response.isSuccessful()) {
223+
assert response.body() != null;
224+
if (request != null) {
225+
response = pluginResponseImp(request, response);
226+
}
227+
try {
228+
// Use Jackson to parse the JSON while preserving order
229+
ObjectMapper mapper = JsonMapper.builder().build();
230+
MapType type = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class,
231+
Object.class);
232+
Map<String, Object> responseMap = mapper.readValue(response.body().string(), type);
233+
234+
// Use the custom method to create an ordered JSONObject
235+
responseJSON = createOrderedJSONObject(responseMap);
236+
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
237+
handleJSONArray();
238+
}
239+
connectionRequest.onRequestFinished(CSHttpConnection.this);
240+
} catch (JSONException e) {
241+
// Handle non-JSON response
242+
setError("Invalid JSON response");
243+
}
244+
} else {
245+
assert response.errorBody() != null;
246+
setError(response.errorBody().string());
214247
}
215-
connectionRequest.onRequestFinished(CSHttpConnection.this);
216-
} else {
217-
assert response.errorBody() != null;
218-
setError(response.errorBody().string());
248+
} catch (SocketTimeoutException e) {
249+
// Handle timeout
250+
setError("Request timed out: " + e.getMessage());
251+
} catch (IOException e) {
252+
// Handle other IO exceptions
253+
setError("IO error occurred: " + e.getMessage());
219254
}
220-
221255
}
222256

223257
private Request pluginRequestImp(String requestUrl) {
@@ -261,7 +295,13 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
261295
}
262296

263297
void setError(String errResp) {
264-
responseJSON = new JSONObject(errResp); // Parse error string to JSONObject
298+
try {
299+
responseJSON = new JSONObject(errResp);
300+
} catch (JSONException e) {
301+
// If errResp is not valid JSON, create a new JSONObject with the error message
302+
responseJSON = new JSONObject();
303+
responseJSON.put(ERROR_MESSAGE, errResp);
304+
}
265305
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
266306
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));
267307
responseJSON.put(ERRORS, responseJSON.optString(ERRORS));

0 commit comments

Comments
 (0)