From 78b171b451989b5366c578c393d0bfa4ee127573 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Tue, 8 Oct 2024 17:48:21 +0530 Subject: [PATCH 1/7] chore: github issues resolved --- .../contentstack/sdk/CSHttpConnection.java | 51 +++++++++++++------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/contentstack/sdk/CSHttpConnection.java b/src/main/java/com/contentstack/sdk/CSHttpConnection.java index 9eda41c0..95ea130b 100644 --- a/src/main/java/com/contentstack/sdk/CSHttpConnection.java +++ b/src/main/java/com/contentstack/sdk/CSHttpConnection.java @@ -11,6 +11,7 @@ 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; @@ -202,22 +203,35 @@ private void getService(String requestUrl) throws IOException { requestUrl = request.url().toString(); } - Response 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 response = this.service.getRequest(requestUrl, this.headers).execute(); + if (response.isSuccessful()) { + assert response.body() != null; + if (request != null) { + response = pluginResponseImp(request, response); + } + String responseBody = response.body().string(); + try { + responseJSON = new JSONObject(responseBody); + 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: " + responseBody); + } + } 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) { @@ -261,7 +275,14 @@ 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_CODE, "unknown"); + } responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE)); responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE)); responseJSON.put(ERRORS, responseJSON.optString(ERRORS)); From 8a6b9d14311f132c27dbb6baa1898753b7d22dbd Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Tue, 8 Oct 2024 18:44:28 +0530 Subject: [PATCH 2/7] chore: removed error code part --- src/main/java/com/contentstack/sdk/CSHttpConnection.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/contentstack/sdk/CSHttpConnection.java b/src/main/java/com/contentstack/sdk/CSHttpConnection.java index 95ea130b..e3c44d65 100644 --- a/src/main/java/com/contentstack/sdk/CSHttpConnection.java +++ b/src/main/java/com/contentstack/sdk/CSHttpConnection.java @@ -281,7 +281,6 @@ void setError(String errResp) { // 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_CODE, "unknown"); } responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE)); responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE)); From 06e35281b1629d651722beb2054fc8df019929d5 Mon Sep 17 00:00:00 2001 From: Vikram Kalta Date: Sun, 13 Oct 2024 21:29:53 +0100 Subject: [PATCH 3/7] fix: preserve order of response object --- pom.xml | 14 +++++++--- .../contentstack/sdk/CSHttpConnection.java | 26 ++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 8fa801f7..7fc95873 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.contentstack.sdk java - 2.0.0 + 2.0.1 jar contentstack-java Java SDK for Contentstack Content Delivery API @@ -17,7 +17,7 @@ 1.8 UTF-8 2.22.0 - 2.2.1 + 3.3.1 3.4.1 3.0.0 3.1.8 @@ -183,6 +183,12 @@ ${json-simple-version} compile + + + com.fasterxml.jackson.core + jackson-databind + 2.15.2 + @@ -267,9 +273,9 @@ sign-artifacts verify - + --pinentry-mode diff --git a/src/main/java/com/contentstack/sdk/CSHttpConnection.java b/src/main/java/com/contentstack/sdk/CSHttpConnection.java index e3c44d65..4a7c1b23 100644 --- a/src/main/java/com/contentstack/sdk/CSHttpConnection.java +++ b/src/main/java/com/contentstack/sdk/CSHttpConnection.java @@ -2,9 +2,11 @@ import okhttp3.Request; import okhttp3.ResponseBody; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; + import retrofit2.Call; import retrofit2.Response; @@ -20,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.*; @@ -186,6 +192,14 @@ public void send() { } } + private JSONObject createOrderedJSONObject(Map map) { + JSONObject json = new JSONObject(); + for (Map.Entry 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); @@ -210,16 +224,22 @@ private void getService(String requestUrl) throws IOException { if (request != null) { response = pluginResponseImp(request, response); } - String responseBody = response.body().string(); try { - responseJSON = new JSONObject(responseBody); + // 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 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: " + responseBody); + setError("Invalid JSON response"); } } else { assert response.errorBody() != null; From cfd2495833f2f1e13f99565bee7625e54f5bbf52 Mon Sep 17 00:00:00 2001 From: Vikram Kalta Date: Sun, 13 Oct 2024 21:31:47 +0100 Subject: [PATCH 4/7] fix: minor change --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 7fc95873..be4c8527 100644 --- a/pom.xml +++ b/pom.xml @@ -273,9 +273,9 @@ sign-artifacts verify - + --pinentry-mode From b32607d32fd40acc43583d638e2f173eda8c3daa Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Mon, 14 Oct 2024 11:59:27 +0530 Subject: [PATCH 5/7] changelog updated --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01f7715d..fd7b1417 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From cdd97a272d1b76f31b28131e42baafadadcb5afb Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 23 Oct 2024 17:50:01 +0530 Subject: [PATCH 6/7] fixed semgrep --- src/main/overview.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/overview.html b/src/main/overview.html index 81ed5a6e..8abe8a00 100644 --- a/src/main/overview.html +++ b/src/main/overview.html @@ -20,7 +20,7 @@

Java SDK for Contentstack

resources to get started with our Java SDK.

Prerequisite

You will need JDK installed on your machine. You can install it from here.

+ href="https://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html">here.

Setup and Installation

To use the Contentstack Java SDK to your existing project, perform the steps given below:

Group id: com.contentstack.sdk

From f94cbef6a6713bb6eeba100664ecf9f927659d7f Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Thu, 24 Oct 2024 16:10:52 +0530 Subject: [PATCH 7/7] Maven publish added --- .github/workflows/maven--package-publish.yml | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/maven--package-publish.yml diff --git a/.github/workflows/maven--package-publish.yml b/.github/workflows/maven--package-publish.yml new file mode 100644 index 00000000..ed2ad1d8 --- /dev/null +++ b/.github/workflows/maven--package-publish.yml @@ -0,0 +1,31 @@ +name: Publishing to Maven Packages +#on: [ push ] # Trigger the workflow when a push (commit) event occurs +on: + release: + types: [ created ] +jobs: + publish-maven: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v3 + - name: Set up Maven Central Repository + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + server-id: ossrh + server-username: MAVEN_USERNAME + server-password: MAVEN_PASSWORD + gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} + gpg-passphrase: GPG_PASSPHRASE + - name: Publish to Maven Central Repository + run: mvn --batch-mode -Dgpg.passphrase=${{ secrets.GPG_PASSPHRASE }} deploy + env: + MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} + MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} + GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} + +# run: mvn --batch-mode deploy \ No newline at end of file