Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #141

Merged
merged 8 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## v2.0.1

### Date: 21-October-2024

-Github Issues fixed
-Issue with field ordering in SDK response

## v2.0.0

### Date: 27-August-2024
Expand Down
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.contentstack.sdk</groupId>
<artifactId>java</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
<packaging>jar</packaging>
<name>contentstack-java</name>
<description>Java SDK for Contentstack Content Delivery API</description>
Expand All @@ -17,7 +17,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefire-report-plugin.version>2.22.0</surefire-report-plugin.version>
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
<maven-javadoc-plugin.version>3.4.1</maven-javadoc-plugin.version>
<dotenv-source.version>3.0.0</dotenv-source.version>
<rxjava-source.version>3.1.8</rxjava-source.version>
Expand Down Expand Up @@ -183,6 +183,12 @@
<version>${json-simple-version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
</dependencies>

<build>
Expand Down
70 changes: 55 additions & 15 deletions src/main/java/com/contentstack/sdk/CSHttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import okhttp3.Request;
import okhttp3.ResponseBody;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -19,6 +22,10 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.IntStream;
import com.fasterxml.jackson.databind.ObjectMapper; // Jackson for JSON parsing
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.type.MapType;

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

Expand Down Expand Up @@ -185,6 +192,14 @@ public void send() {
}
}

private JSONObject createOrderedJSONObject(Map<String, Object> map) {
JSONObject json = new JSONObject();
for (Map.Entry<String, Object> entry : map.entrySet()) {
json.put(entry.getKey(), entry.getValue());
}
return json;
}

private void getService(String requestUrl) throws IOException {

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

Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
if (response.isSuccessful()) {
assert response.body() != null;
if (request != null) {
response = pluginResponseImp(request, response);
}
responseJSON = new JSONObject(response.body().string());
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
handleJSONArray();
try {
Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
if (response.isSuccessful()) {
assert response.body() != null;
if (request != null) {
response = pluginResponseImp(request, response);
}
try {
// Use Jackson to parse the JSON while preserving order
ObjectMapper mapper = JsonMapper.builder().build();
MapType type = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class,
Object.class);
Map<String, Object> responseMap = mapper.readValue(response.body().string(), type);

// Use the custom method to create an ordered JSONObject
responseJSON = createOrderedJSONObject(responseMap);
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
handleJSONArray();
}
connectionRequest.onRequestFinished(CSHttpConnection.this);
} catch (JSONException e) {
// Handle non-JSON response
setError("Invalid JSON response");
}
} else {
assert response.errorBody() != null;
setError(response.errorBody().string());
}
connectionRequest.onRequestFinished(CSHttpConnection.this);
} else {
assert response.errorBody() != null;
setError(response.errorBody().string());
} catch (SocketTimeoutException e) {
// Handle timeout
setError("Request timed out: " + e.getMessage());
} catch (IOException e) {
// Handle other IO exceptions
setError("IO error occurred: " + e.getMessage());
}

}

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

void setError(String errResp) {
responseJSON = new JSONObject(errResp); // Parse error string to JSONObject
try {
responseJSON = new JSONObject(errResp);
} catch (JSONException e) {
// If errResp is not valid JSON, create a new JSONObject with the error message
responseJSON = new JSONObject();
responseJSON.put(ERROR_MESSAGE, errResp);
}
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));
responseJSON.put(ERRORS, responseJSON.optString(ERRORS));
Expand Down
Loading